blob: 3bef06c1544e76edfbc9c6ef687d30b7b131c485 [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,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100510 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100511 * (See for example Cohen's "A Course in Computational Algebraic Number
512 * Theory", Algorithm 10.3.4.)
513 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100514 * Warning: fails if one of the points is zero!
515 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100516 */
517static int ecp_normalize_many( const ecp_group *grp,
518 ecp_point T[], size_t t_len )
519{
520 int ret;
521 size_t i;
522 mpi *c, u, Zi, ZZi;
523
524 if( t_len < 2 )
525 return( ecp_normalize( grp, T ) );
526
527 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
528 return( POLARSSL_ERR_ECP_GENERIC );
529
530 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
531 for( i = 0; i < t_len; i++ )
532 mpi_init( &c[i] );
533
534 /*
535 * c[i] = Z_0 * ... * Z_i
536 */
537 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
538 for( i = 1; i < t_len; i++ )
539 {
540 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
541 MOD_MUL( c[i] );
542 }
543
544 /*
545 * u = 1 / (Z_0 * ... * Z_n) mod P
546 */
547 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
548
549 for( i = t_len - 1; ; i-- )
550 {
551 /*
552 * Zi = 1 / Z_i mod p
553 * u = 1 / (Z_0 * ... * Z_i) mod P
554 */
555 if( i == 0 ) {
556 MPI_CHK( mpi_copy( &Zi, &u ) );
557 }
558 else
559 {
560 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
561 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
562 }
563
564 /*
565 * proceed as in normalize()
566 */
567 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
568 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
569 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
570 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
571 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
572
573 if( i == 0 )
574 break;
575 }
576
577cleanup:
578
579 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
580 for( i = 0; i < t_len; i++ )
581 mpi_free( &c[i] );
582 free( c );
583
584 return( ret );
585}
586
587
588/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100589 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
590 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100591static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
592 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100593{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100594 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100595 mpi T1, T2, T3, X, Y, Z;
596
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100597#if defined(POLARSSL_SELF_TEST)
598 dbl_count++;
599#endif
600
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100601 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100602 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100603
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100604 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
605 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
606
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100607 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
608 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
609 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
610 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
611 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
612 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
613 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
614 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
615 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
616 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100617
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100618 /*
619 * For Y = Y / 2 mod p, we must make sure that Y is even before
620 * using right-shift. No need to reduce mod p afterwards.
621 */
622 if( mpi_get_bit( &Y, 0 ) == 1 )
623 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
624 MPI_CHK( mpi_shift_r( &Y, 1 ) );
625
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100626 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
627 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
628 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
629 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
630 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
631 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100632
633 MPI_CHK( mpi_copy( &R->X, &X ) );
634 MPI_CHK( mpi_copy( &R->Y, &Y ) );
635 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100636
637cleanup:
638
639 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
640 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
641
642 return( ret );
643}
644
645/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100646 * Addition or subtraction: R = P + Q or R = P + Q,
647 * mixed affine-Jacobian coordinates (GECC 3.22)
648 *
649 * The coordinates of Q must be normalized (= affine),
650 * but those of P don't need to. R is not normalized.
651 *
652 * If sign >= 0, perform addition, otherwise perform subtraction,
653 * taking advantage of the fact that, for Q != 0, we have
654 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100655 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100656static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100657 const ecp_point *P, const ecp_point *Q,
658 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100659{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100660 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100661 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100662
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100663#if defined(POLARSSL_SELF_TEST)
664 add_count++;
665#endif
666
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100667 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100668 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100669 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100670 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100671 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
672 return( ecp_copy( R, P ) );
673
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100674 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
675 {
676 ret = ecp_copy( R, Q );
677
678 /*
679 * -R.Y mod P = P - R.Y unless R.Y == 0
680 */
681 if( ret == 0 && sign < 0)
682 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
683 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
684
685 return( ret );
686 }
687
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100688 /*
689 * Make sure Q coordinates are normalized
690 */
691 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
692 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100693
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100694 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
695 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100696
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100697 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
698 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
699 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
700 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100701
702 /*
703 * For subtraction, -Q.Y should have been used instead of Q.Y,
704 * so we replace T2 by -T2, which is P - T2 mod P
705 */
706 if( sign < 0 )
707 {
708 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
709 MOD_SUB( T2 );
710 }
711
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100712 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
713 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100714
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100715 if( mpi_cmp_int( &T1, 0 ) == 0 )
716 {
717 if( mpi_cmp_int( &T2, 0 ) == 0 )
718 {
719 ret = ecp_double_jac( grp, R, P );
720 goto cleanup;
721 }
722 else
723 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100724 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100725 goto cleanup;
726 }
727 }
728
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100729 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
730 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
731 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
732 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
733 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
734 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
735 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
736 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
737 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
738 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
739 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
740 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100741
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100742 MPI_CHK( mpi_copy( &R->X, &X ) );
743 MPI_CHK( mpi_copy( &R->Y, &Y ) );
744 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100745
746cleanup:
747
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100748 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
749 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100750
751 return( ret );
752}
753
754/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100755 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100756 */
757int ecp_add( const ecp_group *grp, ecp_point *R,
758 const ecp_point *P, const ecp_point *Q )
759{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100760 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100761
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100762 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
763 MPI_CHK( ecp_normalize( grp, R ) );
764
765cleanup:
766 return( ret );
767}
768
769/*
770 * Subtraction: R = P - Q, result's coordinates normalized
771 */
772int ecp_sub( const ecp_group *grp, ecp_point *R,
773 const ecp_point *P, const ecp_point *Q )
774{
775 int ret;
776
777 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100778 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100779
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100780cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100781 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100782}
783
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100784/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100785 * Compute a modified width-w non-adjacent form (NAF) of a number,
786 * with a fixed pattern for resistance to SPA/timing attacks,
787 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
788 * (The resulting multiplication algorithm can also been seen as a
789 * modification of 2^w-ary multiplication, with signed coefficients,
790 * all of them odd.)
791 *
792 * Input:
793 * m must be an odd positive mpi less than w * k bits long
794 * x must be an array of k elements
795 * w must be less than a certain maximum (currently 8)
796 *
797 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
798 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
799 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
800 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
801 *
802 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
803 * p. 335 of the cited reference, here we return only u, not d_w since
804 * it is known that the other d_w[j] will be 0. Moreover, the returned
805 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
806 * that u_i is odd. Also, since we always select a positive value for d
807 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
808 * does. Finally, there is an off-by-one error in the reference: the
809 * last index should be k-1, not k.
810 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100811static int ecp_w_naf_fixed( signed char x[], size_t k,
812 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100813{
814 int ret;
815 unsigned int i, u, mask, carry;
816 mpi M;
817
818 mpi_init( &M );
819
820 MPI_CHK( mpi_copy( &M, m ) );
821 mask = ( 1 << w ) - 1;
822 carry = 1 << ( w - 1 );
823
824 for( i = 0; i < k; i++ )
825 {
826 u = M.p[0] & mask;
827
828 if( ( u & 1 ) == 0 && i > 0 )
829 x[i - 1] -= carry;
830
831 x[i] = u >> 1;
832 mpi_shift_r( &M, w );
833 }
834
835 /*
836 * We should have consumed all the bits now
837 */
838 if( mpi_cmp_int( &M, 0 ) != 0 )
839 ret = POLARSSL_ERR_ECP_GENERIC;
840
841cleanup:
842
843 mpi_free( &M );
844
845 return( ret );
846}
847
848/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100849 * Precompute odd multiples of P up to (2 * t_len - 1) P.
850 * The table is filled with T[i] = (2 * i + 1) P.
851 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100852static int ecp_precompute( const ecp_group *grp,
853 ecp_point T[], size_t t_len,
854 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100855{
856 int ret;
857 size_t i;
858 ecp_point PP;
859
860 ecp_point_init( &PP );
861
862 MPI_CHK( ecp_add( grp, &PP, P, P ) );
863
864 MPI_CHK( ecp_copy( &T[0], P ) );
865
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100866 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100867 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
868
869 /*
870 * T[0] = P already has normalized coordinates
871 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100872 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100873
874cleanup:
875
876 ecp_point_free( &PP );
877
878 return( ret );
879}
880
881/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100882 * Maximum length of the precomputed table
883 */
884#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
885
886/*
887 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
888 * (that is: grp->nbits / w + 1)
889 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
890 */
891#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
892
893/*
894 * Integer multiplication: R = m * P
895 *
896 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
897 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
898 *
899 * This function executes a fixed number of operations for
900 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100901 */
902int ecp_mul( const ecp_group *grp, ecp_point *R,
903 const mpi *m, const ecp_point *P )
904{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100905 int ret;
906 unsigned char w, m_is_odd;
907 size_t pre_len, naf_len, i, j;
908 signed char naf[ MAX_NAF_LEN ];
909 ecp_point Q, T[ MAX_PRE_LEN ];
910 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100911
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100912 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +0100913 return( POLARSSL_ERR_ECP_GENERIC );
914
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100915 w = grp->nbits >= 521 ? 6 :
916 grp->nbits >= 224 ? 5 :
917 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100918
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100919 /*
920 * Make sure w is within the limits.
921 * The last test ensures that none of the precomputed points is zero,
922 * which wouldn't be handled correctly by ecp_normalize_many().
923 * It is only useful for small curves, as used in the test suite.
924 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100925 if( w > POLARSSL_ECP_WINDOW_SIZE )
926 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100927 if( w < 2 || w >= grp->nbits )
928 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100929
930 pre_len = 1 << ( w - 1 );
931 naf_len = grp->nbits / w + 1;
932
933 mpi_init( &M );
934 ecp_point_init( &Q );
935 for( i = 0; i < pre_len; i++ )
936 ecp_point_init( &T[i] );
937
938 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
939
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100940 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100941 * Make sure M is odd:
942 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100943 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100944 MPI_CHK( mpi_copy( &M, m ) );
945 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100946
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100947 /*
948 * Compute the fixed-pattern NAF and precompute odd multiples
949 */
950 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
951 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100952
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100953 /*
954 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
955 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
956 *
957 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
958 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
959 * == T[ - naf[i] - 1 ]
960 */
961 MPI_CHK( ecp_set_zero( &Q ) );
962 i = naf_len - 1;
963 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100964 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100965 if( naf[i] < 0 )
966 {
967 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
968 }
969 else
970 {
971 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
972 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100973
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100974 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100975 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100976 i--;
977
978 for( j = 0; j < w; j++ )
979 {
980 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
981 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100982 }
983
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100984 /*
985 * Now get m * P from M * P.
986 * Since we don't need T[] any more, we can recycle it:
987 * we already have T[0] = P, now set T[1] = 2 * P.
988 */
989 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
990 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100991
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100992
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100993cleanup:
994
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100995 mpi_free( &M );
996 ecp_point_free( &Q );
997 for( i = 0; i < pre_len; i++ )
998 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100999
1000 return( ret );
1001}
1002
1003
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001004#if defined(POLARSSL_SELF_TEST)
1005
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001006/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001007 * Checkup routine
1008 */
1009int ecp_self_test( int verbose )
1010{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001011 int ret;
1012 size_t i;
1013 ecp_group grp;
1014 ecp_point R;
1015 mpi m;
1016 unsigned long add_c_prev, dbl_c_prev;
1017 char *exponents[] =
1018 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001019 "000000000000000000000000000000000000000000000000", /* zero */
1020 "000000000000000000000000000000000000000000000001", /* one */
1021 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1022 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001023 "400000000000000000000000000000000000000000000000",
1024 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1025 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001026 };
1027
1028 ecp_group_init( &grp );
1029 ecp_point_init( &R );
1030 mpi_init( &m );
1031
1032 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1033
1034 if( verbose != 0 )
1035 printf( " ECP test #1 (SPA resistance): " );
1036
1037 add_count = 0;
1038 dbl_count = 0;
1039 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1040 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1041
1042 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1043 {
1044 add_c_prev = add_count;
1045 dbl_c_prev = dbl_count;
1046 add_count = 0;
1047 dbl_count = 0;
1048
1049 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1050 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1051
1052 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1053 {
1054 if( verbose != 0 )
1055 printf( "failed (%zu)\n", i );
1056
1057 ret = 1;
1058 goto cleanup;
1059 }
1060 }
1061
1062 if( verbose != 0 )
1063 printf( "passed\n" );
1064
1065cleanup:
1066
1067 if( ret < 0 && verbose != 0 )
1068 printf( "Unexpected error, return code = %08X\n", ret );
1069
1070 ecp_group_free( &grp );
1071 ecp_point_free( &R );
1072 mpi_free( &m );
1073
1074 if( verbose != 0 )
1075 printf( "\n" );
1076
1077 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001078}
1079
1080#endif
1081
1082#endif