blob: 40732885c74b27c9444a6a9a460801fc6b25f580 [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é-Gonnard1c330572012-11-24 12:05:44 +0100471 * Check that a point is valid as a public key (SEC1 3.2.3.1)
472 */
473int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
474{
475 int ret;
476 mpi YY, RHS;
477
478 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
479 return( POLARSSL_ERR_ECP_GENERIC );
480
481 /*
482 * pt coordinates must be normalized for our checks
483 */
484 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
485 return( POLARSSL_ERR_ECP_GENERIC );
486
487 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
488 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
489 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
490 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
491 return( POLARSSL_ERR_ECP_GENERIC );
492
493 mpi_init( &YY ); mpi_init( &RHS );
494
495 /*
496 * YY = Y^2
497 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
498 */
499 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
500 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
501 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
502 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
503 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
504
505 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
506 ret = POLARSSL_ERR_ECP_GENERIC;
507
508cleanup:
509
510 mpi_free( &YY ); mpi_free( &RHS );
511
512 return( ret );
513}
514
515/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100516 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100517 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100518static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100519{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100520 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100521 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100522
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100523 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100524 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100525
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100526 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100527
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100528 /*
529 * X = X / Z^2 mod p
530 */
531 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
532 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
533 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100534
535 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100536 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100537 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100538 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
539 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100540
541 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100542 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100543 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100544 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100545
546cleanup:
547
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100548 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100549
550 return( ret );
551}
552
553/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100554 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100555 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100556 * (See for example Cohen's "A Course in Computational Algebraic Number
557 * Theory", Algorithm 10.3.4.)
558 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100559 * Warning: fails if one of the points is zero!
560 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100561 */
562static int ecp_normalize_many( const ecp_group *grp,
563 ecp_point T[], size_t t_len )
564{
565 int ret;
566 size_t i;
567 mpi *c, u, Zi, ZZi;
568
569 if( t_len < 2 )
570 return( ecp_normalize( grp, T ) );
571
572 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
573 return( POLARSSL_ERR_ECP_GENERIC );
574
575 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
576 for( i = 0; i < t_len; i++ )
577 mpi_init( &c[i] );
578
579 /*
580 * c[i] = Z_0 * ... * Z_i
581 */
582 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
583 for( i = 1; i < t_len; i++ )
584 {
585 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
586 MOD_MUL( c[i] );
587 }
588
589 /*
590 * u = 1 / (Z_0 * ... * Z_n) mod P
591 */
592 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
593
594 for( i = t_len - 1; ; i-- )
595 {
596 /*
597 * Zi = 1 / Z_i mod p
598 * u = 1 / (Z_0 * ... * Z_i) mod P
599 */
600 if( i == 0 ) {
601 MPI_CHK( mpi_copy( &Zi, &u ) );
602 }
603 else
604 {
605 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
606 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
607 }
608
609 /*
610 * proceed as in normalize()
611 */
612 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
613 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
614 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
615 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
616 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
617
618 if( i == 0 )
619 break;
620 }
621
622cleanup:
623
624 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
625 for( i = 0; i < t_len; i++ )
626 mpi_free( &c[i] );
627 free( c );
628
629 return( ret );
630}
631
632
633/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100634 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
635 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100636static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
637 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100638{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100639 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100640 mpi T1, T2, T3, X, Y, Z;
641
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100642#if defined(POLARSSL_SELF_TEST)
643 dbl_count++;
644#endif
645
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100646 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100647 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100648
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100649 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
650 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
651
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100652 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
653 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
654 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
655 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
656 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
657 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
658 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
659 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
660 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
661 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100662
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100663 /*
664 * For Y = Y / 2 mod p, we must make sure that Y is even before
665 * using right-shift. No need to reduce mod p afterwards.
666 */
667 if( mpi_get_bit( &Y, 0 ) == 1 )
668 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
669 MPI_CHK( mpi_shift_r( &Y, 1 ) );
670
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100671 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
672 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
673 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
674 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
675 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
676 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100677
678 MPI_CHK( mpi_copy( &R->X, &X ) );
679 MPI_CHK( mpi_copy( &R->Y, &Y ) );
680 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100681
682cleanup:
683
684 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
685 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
686
687 return( ret );
688}
689
690/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100691 * Addition or subtraction: R = P + Q or R = P + Q,
692 * mixed affine-Jacobian coordinates (GECC 3.22)
693 *
694 * The coordinates of Q must be normalized (= affine),
695 * but those of P don't need to. R is not normalized.
696 *
697 * If sign >= 0, perform addition, otherwise perform subtraction,
698 * taking advantage of the fact that, for Q != 0, we have
699 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100700 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100701static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100702 const ecp_point *P, const ecp_point *Q,
703 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100704{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100705 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100706 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100707
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100708#if defined(POLARSSL_SELF_TEST)
709 add_count++;
710#endif
711
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100712 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100713 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100714 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100715 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100716 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
717 return( ecp_copy( R, P ) );
718
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100719 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
720 {
721 ret = ecp_copy( R, Q );
722
723 /*
724 * -R.Y mod P = P - R.Y unless R.Y == 0
725 */
726 if( ret == 0 && sign < 0)
727 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
728 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
729
730 return( ret );
731 }
732
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100733 /*
734 * Make sure Q coordinates are normalized
735 */
736 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
737 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100738
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100739 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
740 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100741
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100742 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
743 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
744 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
745 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100746
747 /*
748 * For subtraction, -Q.Y should have been used instead of Q.Y,
749 * so we replace T2 by -T2, which is P - T2 mod P
750 */
751 if( sign < 0 )
752 {
753 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
754 MOD_SUB( T2 );
755 }
756
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100757 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
758 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100759
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100760 if( mpi_cmp_int( &T1, 0 ) == 0 )
761 {
762 if( mpi_cmp_int( &T2, 0 ) == 0 )
763 {
764 ret = ecp_double_jac( grp, R, P );
765 goto cleanup;
766 }
767 else
768 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100769 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100770 goto cleanup;
771 }
772 }
773
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100774 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
775 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
776 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
777 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
778 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
779 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
780 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
781 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
782 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
783 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
784 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
785 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100786
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100787 MPI_CHK( mpi_copy( &R->X, &X ) );
788 MPI_CHK( mpi_copy( &R->Y, &Y ) );
789 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100790
791cleanup:
792
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100793 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
794 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100795
796 return( ret );
797}
798
799/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100800 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100801 */
802int ecp_add( const ecp_group *grp, ecp_point *R,
803 const ecp_point *P, const ecp_point *Q )
804{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100805 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100806
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100807 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
808 MPI_CHK( ecp_normalize( grp, R ) );
809
810cleanup:
811 return( ret );
812}
813
814/*
815 * Subtraction: R = P - Q, result's coordinates normalized
816 */
817int ecp_sub( const ecp_group *grp, ecp_point *R,
818 const ecp_point *P, const ecp_point *Q )
819{
820 int ret;
821
822 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100823 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100824
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100825cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100826 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100827}
828
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100829/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100830 * Compute a modified width-w non-adjacent form (NAF) of a number,
831 * with a fixed pattern for resistance to SPA/timing attacks,
832 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
833 * (The resulting multiplication algorithm can also been seen as a
834 * modification of 2^w-ary multiplication, with signed coefficients,
835 * all of them odd.)
836 *
837 * Input:
838 * m must be an odd positive mpi less than w * k bits long
839 * x must be an array of k elements
840 * w must be less than a certain maximum (currently 8)
841 *
842 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
843 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
844 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
845 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
846 *
847 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
848 * p. 335 of the cited reference, here we return only u, not d_w since
849 * it is known that the other d_w[j] will be 0. Moreover, the returned
850 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
851 * that u_i is odd. Also, since we always select a positive value for d
852 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
853 * does. Finally, there is an off-by-one error in the reference: the
854 * last index should be k-1, not k.
855 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100856static int ecp_w_naf_fixed( signed char x[], size_t k,
857 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100858{
859 int ret;
860 unsigned int i, u, mask, carry;
861 mpi M;
862
863 mpi_init( &M );
864
865 MPI_CHK( mpi_copy( &M, m ) );
866 mask = ( 1 << w ) - 1;
867 carry = 1 << ( w - 1 );
868
869 for( i = 0; i < k; i++ )
870 {
871 u = M.p[0] & mask;
872
873 if( ( u & 1 ) == 0 && i > 0 )
874 x[i - 1] -= carry;
875
876 x[i] = u >> 1;
877 mpi_shift_r( &M, w );
878 }
879
880 /*
881 * We should have consumed all the bits now
882 */
883 if( mpi_cmp_int( &M, 0 ) != 0 )
884 ret = POLARSSL_ERR_ECP_GENERIC;
885
886cleanup:
887
888 mpi_free( &M );
889
890 return( ret );
891}
892
893/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100894 * Precompute odd multiples of P up to (2 * t_len - 1) P.
895 * The table is filled with T[i] = (2 * i + 1) P.
896 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100897static int ecp_precompute( const ecp_group *grp,
898 ecp_point T[], size_t t_len,
899 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100900{
901 int ret;
902 size_t i;
903 ecp_point PP;
904
905 ecp_point_init( &PP );
906
907 MPI_CHK( ecp_add( grp, &PP, P, P ) );
908
909 MPI_CHK( ecp_copy( &T[0], P ) );
910
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100911 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100912 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
913
914 /*
915 * T[0] = P already has normalized coordinates
916 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100917 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100918
919cleanup:
920
921 ecp_point_free( &PP );
922
923 return( ret );
924}
925
926/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100927 * Maximum length of the precomputed table
928 */
929#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
930
931/*
932 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
933 * (that is: grp->nbits / w + 1)
934 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
935 */
936#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
937
938/*
939 * Integer multiplication: R = m * P
940 *
941 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
942 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
943 *
944 * This function executes a fixed number of operations for
945 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100946 */
947int ecp_mul( const ecp_group *grp, ecp_point *R,
948 const mpi *m, const ecp_point *P )
949{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100950 int ret;
951 unsigned char w, m_is_odd;
952 size_t pre_len, naf_len, i, j;
953 signed char naf[ MAX_NAF_LEN ];
954 ecp_point Q, T[ MAX_PRE_LEN ];
955 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100956
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100957 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +0100958 return( POLARSSL_ERR_ECP_GENERIC );
959
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100960 w = grp->nbits >= 521 ? 6 :
961 grp->nbits >= 224 ? 5 :
962 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100963
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100964 /*
965 * Make sure w is within the limits.
966 * The last test ensures that none of the precomputed points is zero,
967 * which wouldn't be handled correctly by ecp_normalize_many().
968 * It is only useful for small curves, as used in the test suite.
969 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100970 if( w > POLARSSL_ECP_WINDOW_SIZE )
971 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100972 if( w < 2 || w >= grp->nbits )
973 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100974
975 pre_len = 1 << ( w - 1 );
976 naf_len = grp->nbits / w + 1;
977
978 mpi_init( &M );
979 ecp_point_init( &Q );
980 for( i = 0; i < pre_len; i++ )
981 ecp_point_init( &T[i] );
982
983 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
984
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100985 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100986 * Make sure M is odd:
987 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100988 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100989 MPI_CHK( mpi_copy( &M, m ) );
990 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100991
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100992 /*
993 * Compute the fixed-pattern NAF and precompute odd multiples
994 */
995 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
996 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100997
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100998 /*
999 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1000 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1001 *
1002 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1003 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1004 * == T[ - naf[i] - 1 ]
1005 */
1006 MPI_CHK( ecp_set_zero( &Q ) );
1007 i = naf_len - 1;
1008 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001009 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001010 if( naf[i] < 0 )
1011 {
1012 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1013 }
1014 else
1015 {
1016 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1017 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001018
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001019 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001020 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001021 i--;
1022
1023 for( j = 0; j < w; j++ )
1024 {
1025 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1026 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001027 }
1028
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001029 /*
1030 * Now get m * P from M * P.
1031 * Since we don't need T[] any more, we can recycle it:
1032 * we already have T[0] = P, now set T[1] = 2 * P.
1033 */
1034 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1035 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001036
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001037
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001038cleanup:
1039
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001040 mpi_free( &M );
1041 ecp_point_free( &Q );
1042 for( i = 0; i < pre_len; i++ )
1043 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001044
1045 return( ret );
1046}
1047
1048
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001049#if defined(POLARSSL_SELF_TEST)
1050
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001051/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001052 * Checkup routine
1053 */
1054int ecp_self_test( int verbose )
1055{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001056 int ret;
1057 size_t i;
1058 ecp_group grp;
1059 ecp_point R;
1060 mpi m;
1061 unsigned long add_c_prev, dbl_c_prev;
1062 char *exponents[] =
1063 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001064 "000000000000000000000000000000000000000000000000", /* zero */
1065 "000000000000000000000000000000000000000000000001", /* one */
1066 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1067 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001068 "400000000000000000000000000000000000000000000000",
1069 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1070 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001071 };
1072
1073 ecp_group_init( &grp );
1074 ecp_point_init( &R );
1075 mpi_init( &m );
1076
1077 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1078
1079 if( verbose != 0 )
1080 printf( " ECP test #1 (SPA resistance): " );
1081
1082 add_count = 0;
1083 dbl_count = 0;
1084 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1085 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1086
1087 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1088 {
1089 add_c_prev = add_count;
1090 dbl_c_prev = dbl_count;
1091 add_count = 0;
1092 dbl_count = 0;
1093
1094 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1095 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1096
1097 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1098 {
1099 if( verbose != 0 )
1100 printf( "failed (%zu)\n", i );
1101
1102 ret = 1;
1103 goto cleanup;
1104 }
1105 }
1106
1107 if( verbose != 0 )
1108 printf( "passed\n" );
1109
1110cleanup:
1111
1112 if( ret < 0 && verbose != 0 )
1113 printf( "Unexpected error, return code = %08X\n", ret );
1114
1115 ecp_group_free( &grp );
1116 ecp_point_free( &R );
1117 mpi_free( &m );
1118
1119 if( verbose != 0 )
1120 printf( "\n" );
1121
1122 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001123}
1124
1125#endif
1126
1127#endif