blob: 802f532f14ca5973aa0a2b9ef2d60a747d6d5ea7 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
4 * Copyright (C) 2012, Brainspark B.V.
5 *
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é-Gonnard39d2adb2012-10-31 09:26:55 +010032 */
33
34#include "polarssl/config.h"
35
36#if defined(POLARSSL_ECP_C)
37
38#include "polarssl/ecp.h"
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010039#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010040#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010041
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010042#if defined(POLARSSL_SELF_TEST)
43/*
44 * Counts of point addition and doubling operations.
45 * Used to test resistance of point multiplication to SPA/timing attacks.
46 */
47unsigned long add_count, dbl_count;
48#endif
49
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010050/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010051 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010052 */
53void ecp_point_init( ecp_point *pt )
54{
55 if( pt == NULL )
56 return;
57
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010058 mpi_init( &pt->X );
59 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010060 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010061}
62
63/*
64 * Initialize (the components of) a group
65 */
66void ecp_group_init( ecp_group *grp )
67{
68 if( grp == NULL )
69 return;
70
71 mpi_init( &grp->P );
72 mpi_init( &grp->B );
73 ecp_point_init( &grp->G );
74 mpi_init( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010075
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010076 grp->pbits = 0;
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010077 grp->nbits = 0;
78
79 grp->modp = NULL;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010080}
81
82/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010083 * Unallocate (the components of) a point
84 */
85void ecp_point_free( ecp_point *pt )
86{
87 if( pt == NULL )
88 return;
89
90 mpi_free( &( pt->X ) );
91 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010092 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010093}
94
95/*
96 * Unallocate (the components of) a group
97 */
98void ecp_group_free( ecp_group *grp )
99{
100 if( grp == NULL )
101 return;
102
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100103 mpi_free( &grp->P );
104 mpi_free( &grp->B );
105 ecp_point_free( &grp->G );
106 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100107}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100108
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100109/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100110 * Set point to zero
111 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100112int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100113{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100114 int ret;
115
116 MPI_CHK( mpi_lset( &pt->X , 1 ) );
117 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
118 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
119
120cleanup:
121 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100122}
123
124/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100125 * Copy the contents of Q into P
126 */
127int ecp_copy( ecp_point *P, const ecp_point *Q )
128{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100129 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100130
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100131 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
132 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100133 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100134
135cleanup:
136 return( ret );
137}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100138
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100139/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100140 * Import a non-zero point from ASCII strings
141 */
142int ecp_point_read_string( ecp_point *P, int radix,
143 const char *x, const char *y )
144{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100145 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100146
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100147 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
148 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100149 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100150
151cleanup:
152 return( ret );
153}
154
155/*
156 * Import an ECP group from ASCII strings
157 */
158int ecp_group_read_string( ecp_group *grp, int radix,
159 const char *p, const char *b,
160 const char *gx, const char *gy, const char *n)
161{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100162 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100163
164 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
165 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
166 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
167 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
168
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100169 grp->pbits = mpi_msb( &grp->P );
170 grp->nbits = mpi_msb( &grp->N );
171
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100172cleanup:
173 return( ret );
174}
175
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100176/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100177 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
178 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100179 */
180static int ecp_modp( mpi *N, const ecp_group *grp )
181{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100182 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100183
184 if( grp->modp == NULL )
185 return( mpi_mod_mpi( N, N, &grp->P ) );
186
187 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
188 return( POLARSSL_ERR_ECP_GENERIC );
189
190 MPI_CHK( grp->modp( N ) );
191
192 while( mpi_cmp_int( N, 0 ) < 0 )
193 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
194
195 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
196 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
197
198cleanup:
199 return( ret );
200}
201
202/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100203 * 192 bits in terms of t_uint
204 */
205#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
206
207/*
208 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
209 * -1 means let this chunk be 0
210 * a positive value i means A_i.
211 */
212#define P192_CHUNKS 3
213#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
214#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
215
216const signed char p192_tbl[][P192_CHUNKS] = {
217 { -1, 3, 3 }, /* S1 */
218 { 4, 4, -1 }, /* S2 */
219 { 5, 5, 5 }, /* S3 */
220};
221
222/*
223 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
224 */
225static int ecp_mod_p192( mpi *N )
226{
227 int ret;
228 unsigned char i, j, offset;
229 signed char chunk;
230 mpi tmp, acc;
231 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
232
233 tmp.s = 1;
234 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
235 tmp.p = tmp_p;
236
237 acc.s = 1;
238 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
239 acc.p = acc_p;
240
241 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
242
243 /*
244 * acc = T
245 */
246 memset( acc_p, 0, sizeof( acc_p ) );
247 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
248
249 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
250 {
251 /*
252 * tmp = S_i
253 */
254 memset( tmp_p, 0, sizeof( tmp_p ) );
255 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
256 {
257 chunk = p192_tbl[i][j];
258 if( chunk >= 0 )
259 memcpy( tmp_p + offset * P192_CHUNK_INT,
260 N->p + chunk * P192_CHUNK_INT,
261 P192_CHUNK_CHAR );
262 }
263
264 /*
265 * acc += tmp
266 */
267 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
268 }
269
270 MPI_CHK( mpi_copy( N, &acc ) );
271
272cleanup:
273 return( ret );
274}
275
276/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100277 * Size of p521 in terms of t_uint
278 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100279#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100280
281/*
282 * Bits to keep in the most significant t_uint
283 */
284#if defined(POLARSS_HAVE_INT8)
285#define P521_MASK 0x01
286#else
287#define P521_MASK 0x01FF
288#endif
289
290/*
291 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100292 */
293static int ecp_mod_p521( mpi *N )
294{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100295 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100296 t_uint Mp[P521_SIZE_INT];
297 mpi M;
298
299 if( N->n < P521_SIZE_INT )
300 return( 0 );
301
302 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
303 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
304 Mp[P521_SIZE_INT - 1] &= P521_MASK;
305
306 M.s = 1;
307 M.n = P521_SIZE_INT;
308 M.p = Mp;
309
310 MPI_CHK( mpi_shift_r( N, 521 ) );
311
312 MPI_CHK( mpi_add_abs( N, N, &M ) );
313
314cleanup:
315 return( ret );
316}
317
318/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100319 * Domain parameters for secp192r1
320 */
321#define SECP192R1_P \
322 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
323#define SECP192R1_B \
324 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
325#define SECP192R1_GX \
326 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
327#define SECP192R1_GY \
328 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
329#define SECP192R1_N \
330 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
331
332/*
333 * Domain parameters for secp224r1
334 */
335#define SECP224R1_P \
336 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
337#define SECP224R1_B \
338 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
339#define SECP224R1_GX \
340 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
341#define SECP224R1_GY \
342 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
343#define SECP224R1_N \
344 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
345
346/*
347 * Domain parameters for secp256r1
348 */
349#define SECP256R1_P \
350 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
351#define SECP256R1_B \
352 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
353#define SECP256R1_GX \
354 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
355#define SECP256R1_GY \
356 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
357#define SECP256R1_N \
358 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
359
360/*
361 * Domain parameters for secp384r1
362 */
363#define SECP384R1_P \
364 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
365 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
366#define SECP384R1_B \
367 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
368 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
369#define SECP384R1_GX \
370 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
371 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
372#define SECP384R1_GY \
373 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
374 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
375#define SECP384R1_N \
376 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
377 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
378
379/*
380 * Domain parameters for secp521r1
381 */
382#define SECP521R1_P \
383 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
384 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
385 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
386#define SECP521R1_B \
387 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
388 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
389 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
390#define SECP521R1_GX \
391 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
392 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
393 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
394#define SECP521R1_GY \
395 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
396 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
397 "3FAD0761353C7086A272C24088BE94769FD16650"
398#define SECP521R1_N \
399 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
400 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
401 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
402
403/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100404 * Set a group using well-known domain parameters
405 */
406int ecp_use_known_dp( ecp_group *grp, size_t index )
407{
408 switch( index )
409 {
410 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100411 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100412 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100413 SECP192R1_P, SECP192R1_B,
414 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
415
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100416 case POLARSSL_ECP_DP_SECP224R1:
417 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100418 SECP224R1_P, SECP224R1_B,
419 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
420
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100421 case POLARSSL_ECP_DP_SECP256R1:
422 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100423 SECP256R1_P, SECP256R1_B,
424 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
425
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100426 case POLARSSL_ECP_DP_SECP384R1:
427 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100428 SECP384R1_P, SECP384R1_B,
429 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
430
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100431 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100432 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100433 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100434 SECP521R1_P, SECP521R1_B,
435 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100436 }
437
438 return( POLARSSL_ERR_ECP_GENERIC );
439}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100440
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100441/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100442 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100443 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100444 * In order to guarantee that, we need to ensure that operands of
445 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100446 * bring the result back to this range.
447 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100448 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100449 */
450
451/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100452 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
453 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100454#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100455
456/*
457 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
458 */
459#define MOD_SUB( N ) \
460 while( mpi_cmp_int( &N, 0 ) < 0 ) \
461 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
462
463/*
464 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
465 */
466#define MOD_ADD( N ) \
467 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
468 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
469
470/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100471 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100472 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100473static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100474{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100475 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100476 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100477
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100478 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100479 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100480
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100481 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100482
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100483 /*
484 * X = X / Z^2 mod p
485 */
486 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
487 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
488 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100489
490 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100491 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100492 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100493 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
494 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100495
496 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100497 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100498 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100499 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100500
501cleanup:
502
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100503 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100504
505 return( ret );
506}
507
508/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100509 * Normalize jacobian coordinates of an array of points,
510 * using Montgomery's trick to perform only one division.
511 * (See for example Cohen's "A Course in Computational Algebraic Number
512 * Theory", Algorithm 10.3.4.)
513 *
514 * FIXME: assumes all points are non zero
515 */
516static int ecp_normalize_many( const ecp_group *grp,
517 ecp_point T[], size_t t_len )
518{
519 int ret;
520 size_t i;
521 mpi *c, u, Zi, ZZi;
522
523 if( t_len < 2 )
524 return( ecp_normalize( grp, T ) );
525
526 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
527 return( POLARSSL_ERR_ECP_GENERIC );
528
529 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
530 for( i = 0; i < t_len; i++ )
531 mpi_init( &c[i] );
532
533 /*
534 * c[i] = Z_0 * ... * Z_i
535 */
536 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
537 for( i = 1; i < t_len; i++ )
538 {
539 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
540 MOD_MUL( c[i] );
541 }
542
543 /*
544 * u = 1 / (Z_0 * ... * Z_n) mod P
545 */
546 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
547
548 for( i = t_len - 1; ; i-- )
549 {
550 /*
551 * Zi = 1 / Z_i mod p
552 * u = 1 / (Z_0 * ... * Z_i) mod P
553 */
554 if( i == 0 ) {
555 MPI_CHK( mpi_copy( &Zi, &u ) );
556 }
557 else
558 {
559 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
560 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
561 }
562
563 /*
564 * proceed as in normalize()
565 */
566 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
567 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
568 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
569 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
570 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
571
572 if( i == 0 )
573 break;
574 }
575
576cleanup:
577
578 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
579 for( i = 0; i < t_len; i++ )
580 mpi_free( &c[i] );
581 free( c );
582
583 return( ret );
584}
585
586
587/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100588 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
589 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100590static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
591 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100592{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100593 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100594 mpi T1, T2, T3, X, Y, Z;
595
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100596#if defined(POLARSSL_SELF_TEST)
597 dbl_count++;
598#endif
599
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100600 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100601 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100602
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100603 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
604 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
605
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100606 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
607 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
608 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
609 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
610 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
611 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
612 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
613 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
614 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
615 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100616
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100617 /*
618 * For Y = Y / 2 mod p, we must make sure that Y is even before
619 * using right-shift. No need to reduce mod p afterwards.
620 */
621 if( mpi_get_bit( &Y, 0 ) == 1 )
622 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
623 MPI_CHK( mpi_shift_r( &Y, 1 ) );
624
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100625 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
626 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
627 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
628 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
629 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
630 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100631
632 MPI_CHK( mpi_copy( &R->X, &X ) );
633 MPI_CHK( mpi_copy( &R->Y, &Y ) );
634 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100635
636cleanup:
637
638 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
639 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
640
641 return( ret );
642}
643
644/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100645 * Addition or subtraction: R = P + Q or R = P + Q,
646 * mixed affine-Jacobian coordinates (GECC 3.22)
647 *
648 * The coordinates of Q must be normalized (= affine),
649 * but those of P don't need to. R is not normalized.
650 *
651 * If sign >= 0, perform addition, otherwise perform subtraction,
652 * taking advantage of the fact that, for Q != 0, we have
653 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100654 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100655static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100656 const ecp_point *P, const ecp_point *Q,
657 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100658{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100659 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100660 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100661
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100662#if defined(POLARSSL_SELF_TEST)
663 add_count++;
664#endif
665
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100666 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100667 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100668 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100669 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100670 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
671 return( ecp_copy( R, P ) );
672
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100673 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
674 {
675 ret = ecp_copy( R, Q );
676
677 /*
678 * -R.Y mod P = P - R.Y unless R.Y == 0
679 */
680 if( ret == 0 && sign < 0)
681 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
682 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
683
684 return( ret );
685 }
686
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100687 /*
688 * Make sure Q coordinates are normalized
689 */
690 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
691 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100692
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100693 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
694 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100695
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100696 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
697 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
698 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
699 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100700
701 /*
702 * For subtraction, -Q.Y should have been used instead of Q.Y,
703 * so we replace T2 by -T2, which is P - T2 mod P
704 */
705 if( sign < 0 )
706 {
707 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
708 MOD_SUB( T2 );
709 }
710
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100711 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
712 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100713
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100714 if( mpi_cmp_int( &T1, 0 ) == 0 )
715 {
716 if( mpi_cmp_int( &T2, 0 ) == 0 )
717 {
718 ret = ecp_double_jac( grp, R, P );
719 goto cleanup;
720 }
721 else
722 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100723 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100724 goto cleanup;
725 }
726 }
727
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100728 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
729 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
730 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
731 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
732 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
733 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
734 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
735 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
736 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
737 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
738 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
739 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100740
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100741 MPI_CHK( mpi_copy( &R->X, &X ) );
742 MPI_CHK( mpi_copy( &R->Y, &Y ) );
743 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100744
745cleanup:
746
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100747 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
748 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100749
750 return( ret );
751}
752
753/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100754 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100755 */
756int ecp_add( const ecp_group *grp, ecp_point *R,
757 const ecp_point *P, const ecp_point *Q )
758{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100759 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100760
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100761 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
762 MPI_CHK( ecp_normalize( grp, R ) );
763
764cleanup:
765 return( ret );
766}
767
768/*
769 * Subtraction: R = P - Q, result's coordinates normalized
770 */
771int ecp_sub( const ecp_group *grp, ecp_point *R,
772 const ecp_point *P, const ecp_point *Q )
773{
774 int ret;
775
776 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100777 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100778
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100779cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100780 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100781}
782
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100783/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100784 * Compute a modified width-w non-adjacent form (NAF) of a number,
785 * with a fixed pattern for resistance to SPA/timing attacks,
786 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
787 * (The resulting multiplication algorithm can also been seen as a
788 * modification of 2^w-ary multiplication, with signed coefficients,
789 * all of them odd.)
790 *
791 * Input:
792 * m must be an odd positive mpi less than w * k bits long
793 * x must be an array of k elements
794 * w must be less than a certain maximum (currently 8)
795 *
796 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
797 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
798 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
799 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
800 *
801 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
802 * p. 335 of the cited reference, here we return only u, not d_w since
803 * it is known that the other d_w[j] will be 0. Moreover, the returned
804 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
805 * that u_i is odd. Also, since we always select a positive value for d
806 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
807 * does. Finally, there is an off-by-one error in the reference: the
808 * last index should be k-1, not k.
809 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100810static int ecp_w_naf_fixed( signed char x[], size_t k,
811 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100812{
813 int ret;
814 unsigned int i, u, mask, carry;
815 mpi M;
816
817 mpi_init( &M );
818
819 MPI_CHK( mpi_copy( &M, m ) );
820 mask = ( 1 << w ) - 1;
821 carry = 1 << ( w - 1 );
822
823 for( i = 0; i < k; i++ )
824 {
825 u = M.p[0] & mask;
826
827 if( ( u & 1 ) == 0 && i > 0 )
828 x[i - 1] -= carry;
829
830 x[i] = u >> 1;
831 mpi_shift_r( &M, w );
832 }
833
834 /*
835 * We should have consumed all the bits now
836 */
837 if( mpi_cmp_int( &M, 0 ) != 0 )
838 ret = POLARSSL_ERR_ECP_GENERIC;
839
840cleanup:
841
842 mpi_free( &M );
843
844 return( ret );
845}
846
847/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100848 * Precompute odd multiples of P up to (2 * t_len - 1) P.
849 * The table is filled with T[i] = (2 * i + 1) P.
850 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100851static int ecp_precompute( const ecp_group *grp,
852 ecp_point T[], size_t t_len,
853 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100854{
855 int ret;
856 size_t i;
857 ecp_point PP;
858
859 ecp_point_init( &PP );
860
861 MPI_CHK( ecp_add( grp, &PP, P, P ) );
862
863 MPI_CHK( ecp_copy( &T[0], P ) );
864
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100865 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100866 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
867
868 /*
869 * T[0] = P already has normalized coordinates
870 */
871 ecp_normalize_many( grp, T + 1, t_len - 1 );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100872
873cleanup:
874
875 ecp_point_free( &PP );
876
877 return( ret );
878}
879
880/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100881 * Maximum length of the precomputed table
882 */
883#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
884
885/*
886 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
887 * (that is: grp->nbits / w + 1)
888 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
889 */
890#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
891
892/*
893 * Integer multiplication: R = m * P
894 *
895 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
896 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
897 *
898 * This function executes a fixed number of operations for
899 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100900 */
901int ecp_mul( const ecp_group *grp, ecp_point *R,
902 const mpi *m, const ecp_point *P )
903{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100904 int ret;
905 unsigned char w, m_is_odd;
906 size_t pre_len, naf_len, i, j;
907 signed char naf[ MAX_NAF_LEN ];
908 ecp_point Q, T[ MAX_PRE_LEN ];
909 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100910
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100911 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +0100912 return( POLARSSL_ERR_ECP_GENERIC );
913
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100914 w = 5; // TODO: find optimal values once precompute() is optimized
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100915
916 if( w < 2 )
917 w = 2;
918 if( w > POLARSSL_ECP_WINDOW_SIZE )
919 w = POLARSSL_ECP_WINDOW_SIZE;
920
921 pre_len = 1 << ( w - 1 );
922 naf_len = grp->nbits / w + 1;
923
924 mpi_init( &M );
925 ecp_point_init( &Q );
926 for( i = 0; i < pre_len; i++ )
927 ecp_point_init( &T[i] );
928
929 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
930
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100931 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100932 * Make sure M is odd:
933 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100934 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100935 MPI_CHK( mpi_copy( &M, m ) );
936 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100937
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100938 /*
939 * Compute the fixed-pattern NAF and precompute odd multiples
940 */
941 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
942 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100943
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100944 /*
945 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
946 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
947 *
948 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
949 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
950 * == T[ - naf[i] - 1 ]
951 */
952 MPI_CHK( ecp_set_zero( &Q ) );
953 i = naf_len - 1;
954 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100955 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100956 if( naf[i] < 0 )
957 {
958 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
959 }
960 else
961 {
962 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
963 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100964
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100965 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100966 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100967 i--;
968
969 for( j = 0; j < w; j++ )
970 {
971 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
972 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100973 }
974
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100975 /*
976 * Now get m * P from M * P.
977 * Since we don't need T[] any more, we can recycle it:
978 * we already have T[0] = P, now set T[1] = 2 * P.
979 */
980 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
981 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100982
983cleanup:
984
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100985 mpi_free( &M );
986 ecp_point_free( &Q );
987 for( i = 0; i < pre_len; i++ )
988 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100989
990 return( ret );
991}
992
993
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100994#if defined(POLARSSL_SELF_TEST)
995
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100996/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100997 * Checkup routine
998 */
999int ecp_self_test( int verbose )
1000{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001001 int ret;
1002 size_t i;
1003 ecp_group grp;
1004 ecp_point R;
1005 mpi m;
1006 unsigned long add_c_prev, dbl_c_prev;
1007 char *exponents[] =
1008 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001009 "000000000000000000000000000000000000000000000000", /* zero */
1010 "000000000000000000000000000000000000000000000001", /* one */
1011 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1012 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001013 "400000000000000000000000000000000000000000000000",
1014 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1015 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001016 };
1017
1018 ecp_group_init( &grp );
1019 ecp_point_init( &R );
1020 mpi_init( &m );
1021
1022 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1023
1024 if( verbose != 0 )
1025 printf( " ECP test #1 (SPA resistance): " );
1026
1027 add_count = 0;
1028 dbl_count = 0;
1029 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1030 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1031
1032 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1033 {
1034 add_c_prev = add_count;
1035 dbl_c_prev = dbl_count;
1036 add_count = 0;
1037 dbl_count = 0;
1038
1039 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1040 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1041
1042 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1043 {
1044 if( verbose != 0 )
1045 printf( "failed (%zu)\n", i );
1046
1047 ret = 1;
1048 goto cleanup;
1049 }
1050 }
1051
1052 if( verbose != 0 )
1053 printf( "passed\n" );
1054
1055cleanup:
1056
1057 if( ret < 0 && verbose != 0 )
1058 printf( "Unexpected error, return code = %08X\n", ret );
1059
1060 ecp_group_free( &grp );
1061 ecp_point_free( &R );
1062 mpi_free( &m );
1063
1064 if( verbose != 0 )
1065 printf( "\n" );
1066
1067 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001068}
1069
1070#endif
1071
1072#endif