blob: 6501fbe9f81ccf1092e2691ace4721fd0e3a11e3 [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é-Gonnard37d218a2012-11-24 15:19:55 +0100177 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100178 */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100179int ecp_write_binary( const ecp_group *grp, const ecp_point *P, int format,
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100180 size_t *olen, unsigned char *buf, size_t buflen )
181{
182 int ret;
183 size_t plen;
184
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100185 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
186 format != POLARSSL_ECP_PF_COMPRESSED )
187 return( POLARSSL_ERR_ECP_GENERIC );
188
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100189 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100190 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100191 */
192 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
193 {
194 if( buflen < 1 )
195 return( POLARSSL_ERR_ECP_GENERIC );
196
197 buf[0] = 0x00;
198 *olen = 1;
199
200 return( 0 );
201 }
202
203 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100204
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100205 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
206 {
207 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100208
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100209 if( buflen < *olen )
210 return( POLARSSL_ERR_ECP_GENERIC );
211
212 buf[0] = 0x04;
213 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
214 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
215 }
216 else if( format == POLARSSL_ECP_PF_COMPRESSED )
217 {
218 *olen = plen + 1;
219
220 if( buflen < *olen )
221 return( POLARSSL_ERR_ECP_GENERIC );
222
223 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
224 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
225 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100226
227cleanup:
228 return( ret );
229}
230
231/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100232 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
233 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100234 */
235static int ecp_modp( mpi *N, const ecp_group *grp )
236{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100237 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100238
239 if( grp->modp == NULL )
240 return( mpi_mod_mpi( N, N, &grp->P ) );
241
242 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
243 return( POLARSSL_ERR_ECP_GENERIC );
244
245 MPI_CHK( grp->modp( N ) );
246
247 while( mpi_cmp_int( N, 0 ) < 0 )
248 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
249
250 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
251 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
252
253cleanup:
254 return( ret );
255}
256
257/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100258 * 192 bits in terms of t_uint
259 */
260#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
261
262/*
263 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
264 * -1 means let this chunk be 0
265 * a positive value i means A_i.
266 */
267#define P192_CHUNKS 3
268#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
269#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
270
271const signed char p192_tbl[][P192_CHUNKS] = {
272 { -1, 3, 3 }, /* S1 */
273 { 4, 4, -1 }, /* S2 */
274 { 5, 5, 5 }, /* S3 */
275};
276
277/*
278 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
279 */
280static int ecp_mod_p192( mpi *N )
281{
282 int ret;
283 unsigned char i, j, offset;
284 signed char chunk;
285 mpi tmp, acc;
286 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
287
288 tmp.s = 1;
289 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
290 tmp.p = tmp_p;
291
292 acc.s = 1;
293 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
294 acc.p = acc_p;
295
296 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
297
298 /*
299 * acc = T
300 */
301 memset( acc_p, 0, sizeof( acc_p ) );
302 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
303
304 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
305 {
306 /*
307 * tmp = S_i
308 */
309 memset( tmp_p, 0, sizeof( tmp_p ) );
310 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
311 {
312 chunk = p192_tbl[i][j];
313 if( chunk >= 0 )
314 memcpy( tmp_p + offset * P192_CHUNK_INT,
315 N->p + chunk * P192_CHUNK_INT,
316 P192_CHUNK_CHAR );
317 }
318
319 /*
320 * acc += tmp
321 */
322 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
323 }
324
325 MPI_CHK( mpi_copy( N, &acc ) );
326
327cleanup:
328 return( ret );
329}
330
331/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100332 * Size of p521 in terms of t_uint
333 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100334#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100335
336/*
337 * Bits to keep in the most significant t_uint
338 */
339#if defined(POLARSS_HAVE_INT8)
340#define P521_MASK 0x01
341#else
342#define P521_MASK 0x01FF
343#endif
344
345/*
346 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100347 */
348static int ecp_mod_p521( mpi *N )
349{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100350 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100351 t_uint Mp[P521_SIZE_INT];
352 mpi M;
353
354 if( N->n < P521_SIZE_INT )
355 return( 0 );
356
357 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
358 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
359 Mp[P521_SIZE_INT - 1] &= P521_MASK;
360
361 M.s = 1;
362 M.n = P521_SIZE_INT;
363 M.p = Mp;
364
365 MPI_CHK( mpi_shift_r( N, 521 ) );
366
367 MPI_CHK( mpi_add_abs( N, N, &M ) );
368
369cleanup:
370 return( ret );
371}
372
373/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100374 * Domain parameters for secp192r1
375 */
376#define SECP192R1_P \
377 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
378#define SECP192R1_B \
379 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
380#define SECP192R1_GX \
381 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
382#define SECP192R1_GY \
383 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
384#define SECP192R1_N \
385 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
386
387/*
388 * Domain parameters for secp224r1
389 */
390#define SECP224R1_P \
391 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
392#define SECP224R1_B \
393 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
394#define SECP224R1_GX \
395 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
396#define SECP224R1_GY \
397 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
398#define SECP224R1_N \
399 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
400
401/*
402 * Domain parameters for secp256r1
403 */
404#define SECP256R1_P \
405 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
406#define SECP256R1_B \
407 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
408#define SECP256R1_GX \
409 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
410#define SECP256R1_GY \
411 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
412#define SECP256R1_N \
413 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
414
415/*
416 * Domain parameters for secp384r1
417 */
418#define SECP384R1_P \
419 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
420 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
421#define SECP384R1_B \
422 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
423 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
424#define SECP384R1_GX \
425 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
426 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
427#define SECP384R1_GY \
428 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
429 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
430#define SECP384R1_N \
431 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
432 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
433
434/*
435 * Domain parameters for secp521r1
436 */
437#define SECP521R1_P \
438 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
439 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
440 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
441#define SECP521R1_B \
442 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
443 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
444 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
445#define SECP521R1_GX \
446 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
447 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
448 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
449#define SECP521R1_GY \
450 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
451 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
452 "3FAD0761353C7086A272C24088BE94769FD16650"
453#define SECP521R1_N \
454 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
455 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
456 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
457
458/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100459 * Set a group using well-known domain parameters
460 */
461int ecp_use_known_dp( ecp_group *grp, size_t index )
462{
463 switch( index )
464 {
465 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100466 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100467 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100468 SECP192R1_P, SECP192R1_B,
469 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
470
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100471 case POLARSSL_ECP_DP_SECP224R1:
472 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100473 SECP224R1_P, SECP224R1_B,
474 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
475
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100476 case POLARSSL_ECP_DP_SECP256R1:
477 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100478 SECP256R1_P, SECP256R1_B,
479 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
480
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100481 case POLARSSL_ECP_DP_SECP384R1:
482 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100483 SECP384R1_P, SECP384R1_B,
484 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
485
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100486 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100487 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100488 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100489 SECP521R1_P, SECP521R1_B,
490 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100491 }
492
493 return( POLARSSL_ERR_ECP_GENERIC );
494}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100495
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100496/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100497 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100498 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100499 * In order to guarantee that, we need to ensure that operands of
500 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100501 * bring the result back to this range.
502 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100503 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100504 */
505
506/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100507 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
508 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100509#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100510
511/*
512 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
513 */
514#define MOD_SUB( N ) \
515 while( mpi_cmp_int( &N, 0 ) < 0 ) \
516 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
517
518/*
519 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
520 */
521#define MOD_ADD( N ) \
522 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
523 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
524
525/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100526 * Check that a point is valid as a public key (SEC1 3.2.3.1)
527 */
528int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
529{
530 int ret;
531 mpi YY, RHS;
532
533 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
534 return( POLARSSL_ERR_ECP_GENERIC );
535
536 /*
537 * pt coordinates must be normalized for our checks
538 */
539 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
540 return( POLARSSL_ERR_ECP_GENERIC );
541
542 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
543 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
544 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
545 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
546 return( POLARSSL_ERR_ECP_GENERIC );
547
548 mpi_init( &YY ); mpi_init( &RHS );
549
550 /*
551 * YY = Y^2
552 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
553 */
554 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
555 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
556 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
557 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
558 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
559
560 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
561 ret = POLARSSL_ERR_ECP_GENERIC;
562
563cleanup:
564
565 mpi_free( &YY ); mpi_free( &RHS );
566
567 return( ret );
568}
569
570/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100571 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100572 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100573static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100574{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100575 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100576 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100577
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100578 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100579 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100580
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100581 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100582
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100583 /*
584 * X = X / Z^2 mod p
585 */
586 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
587 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
588 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100589
590 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100591 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100592 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100593 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
594 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100595
596 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100597 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100598 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100599 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100600
601cleanup:
602
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100603 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100604
605 return( ret );
606}
607
608/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100609 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100610 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100611 * (See for example Cohen's "A Course in Computational Algebraic Number
612 * Theory", Algorithm 10.3.4.)
613 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100614 * Warning: fails if one of the points is zero!
615 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100616 */
617static int ecp_normalize_many( const ecp_group *grp,
618 ecp_point T[], size_t t_len )
619{
620 int ret;
621 size_t i;
622 mpi *c, u, Zi, ZZi;
623
624 if( t_len < 2 )
625 return( ecp_normalize( grp, T ) );
626
627 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
628 return( POLARSSL_ERR_ECP_GENERIC );
629
630 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
631 for( i = 0; i < t_len; i++ )
632 mpi_init( &c[i] );
633
634 /*
635 * c[i] = Z_0 * ... * Z_i
636 */
637 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
638 for( i = 1; i < t_len; i++ )
639 {
640 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
641 MOD_MUL( c[i] );
642 }
643
644 /*
645 * u = 1 / (Z_0 * ... * Z_n) mod P
646 */
647 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
648
649 for( i = t_len - 1; ; i-- )
650 {
651 /*
652 * Zi = 1 / Z_i mod p
653 * u = 1 / (Z_0 * ... * Z_i) mod P
654 */
655 if( i == 0 ) {
656 MPI_CHK( mpi_copy( &Zi, &u ) );
657 }
658 else
659 {
660 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
661 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
662 }
663
664 /*
665 * proceed as in normalize()
666 */
667 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
668 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
669 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
670 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
671 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
672
673 if( i == 0 )
674 break;
675 }
676
677cleanup:
678
679 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
680 for( i = 0; i < t_len; i++ )
681 mpi_free( &c[i] );
682 free( c );
683
684 return( ret );
685}
686
687
688/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100689 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
690 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100691static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
692 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100693{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100694 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100695 mpi T1, T2, T3, X, Y, Z;
696
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100697#if defined(POLARSSL_SELF_TEST)
698 dbl_count++;
699#endif
700
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100701 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100702 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100703
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100704 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
705 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
706
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100707 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
708 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
709 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
710 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
711 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
712 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
713 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
714 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
715 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
716 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100717
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100718 /*
719 * For Y = Y / 2 mod p, we must make sure that Y is even before
720 * using right-shift. No need to reduce mod p afterwards.
721 */
722 if( mpi_get_bit( &Y, 0 ) == 1 )
723 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
724 MPI_CHK( mpi_shift_r( &Y, 1 ) );
725
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100726 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
727 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
728 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
729 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
730 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
731 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100732
733 MPI_CHK( mpi_copy( &R->X, &X ) );
734 MPI_CHK( mpi_copy( &R->Y, &Y ) );
735 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100736
737cleanup:
738
739 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
740 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
741
742 return( ret );
743}
744
745/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100746 * Addition or subtraction: R = P + Q or R = P + Q,
747 * mixed affine-Jacobian coordinates (GECC 3.22)
748 *
749 * The coordinates of Q must be normalized (= affine),
750 * but those of P don't need to. R is not normalized.
751 *
752 * If sign >= 0, perform addition, otherwise perform subtraction,
753 * taking advantage of the fact that, for Q != 0, we have
754 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100755 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100756static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100757 const ecp_point *P, const ecp_point *Q,
758 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100759{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100760 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100761 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100762
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100763#if defined(POLARSSL_SELF_TEST)
764 add_count++;
765#endif
766
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100767 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100768 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100769 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100770 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100771 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
772 return( ecp_copy( R, P ) );
773
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100774 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
775 {
776 ret = ecp_copy( R, Q );
777
778 /*
779 * -R.Y mod P = P - R.Y unless R.Y == 0
780 */
781 if( ret == 0 && sign < 0)
782 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
783 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
784
785 return( ret );
786 }
787
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100788 /*
789 * Make sure Q coordinates are normalized
790 */
791 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
792 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100793
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100794 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
795 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100796
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100797 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
798 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
799 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
800 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100801
802 /*
803 * For subtraction, -Q.Y should have been used instead of Q.Y,
804 * so we replace T2 by -T2, which is P - T2 mod P
805 */
806 if( sign < 0 )
807 {
808 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
809 MOD_SUB( T2 );
810 }
811
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100812 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
813 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100814
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100815 if( mpi_cmp_int( &T1, 0 ) == 0 )
816 {
817 if( mpi_cmp_int( &T2, 0 ) == 0 )
818 {
819 ret = ecp_double_jac( grp, R, P );
820 goto cleanup;
821 }
822 else
823 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100824 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100825 goto cleanup;
826 }
827 }
828
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100829 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
830 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
831 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
832 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
833 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
834 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
835 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
836 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
837 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
838 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
839 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
840 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100841
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100842 MPI_CHK( mpi_copy( &R->X, &X ) );
843 MPI_CHK( mpi_copy( &R->Y, &Y ) );
844 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100845
846cleanup:
847
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100848 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
849 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100850
851 return( ret );
852}
853
854/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100855 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100856 */
857int ecp_add( const ecp_group *grp, ecp_point *R,
858 const ecp_point *P, const ecp_point *Q )
859{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100860 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100861
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100862 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
863 MPI_CHK( ecp_normalize( grp, R ) );
864
865cleanup:
866 return( ret );
867}
868
869/*
870 * Subtraction: R = P - Q, result's coordinates normalized
871 */
872int ecp_sub( const ecp_group *grp, ecp_point *R,
873 const ecp_point *P, const ecp_point *Q )
874{
875 int ret;
876
877 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100878 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100879
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100880cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100881 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100882}
883
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100884/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100885 * Compute a modified width-w non-adjacent form (NAF) of a number,
886 * with a fixed pattern for resistance to SPA/timing attacks,
887 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
888 * (The resulting multiplication algorithm can also been seen as a
889 * modification of 2^w-ary multiplication, with signed coefficients,
890 * all of them odd.)
891 *
892 * Input:
893 * m must be an odd positive mpi less than w * k bits long
894 * x must be an array of k elements
895 * w must be less than a certain maximum (currently 8)
896 *
897 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
898 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
899 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
900 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
901 *
902 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
903 * p. 335 of the cited reference, here we return only u, not d_w since
904 * it is known that the other d_w[j] will be 0. Moreover, the returned
905 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
906 * that u_i is odd. Also, since we always select a positive value for d
907 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
908 * does. Finally, there is an off-by-one error in the reference: the
909 * last index should be k-1, not k.
910 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100911static int ecp_w_naf_fixed( signed char x[], size_t k,
912 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100913{
914 int ret;
915 unsigned int i, u, mask, carry;
916 mpi M;
917
918 mpi_init( &M );
919
920 MPI_CHK( mpi_copy( &M, m ) );
921 mask = ( 1 << w ) - 1;
922 carry = 1 << ( w - 1 );
923
924 for( i = 0; i < k; i++ )
925 {
926 u = M.p[0] & mask;
927
928 if( ( u & 1 ) == 0 && i > 0 )
929 x[i - 1] -= carry;
930
931 x[i] = u >> 1;
932 mpi_shift_r( &M, w );
933 }
934
935 /*
936 * We should have consumed all the bits now
937 */
938 if( mpi_cmp_int( &M, 0 ) != 0 )
939 ret = POLARSSL_ERR_ECP_GENERIC;
940
941cleanup:
942
943 mpi_free( &M );
944
945 return( ret );
946}
947
948/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100949 * Precompute odd multiples of P up to (2 * t_len - 1) P.
950 * The table is filled with T[i] = (2 * i + 1) P.
951 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100952static int ecp_precompute( const ecp_group *grp,
953 ecp_point T[], size_t t_len,
954 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100955{
956 int ret;
957 size_t i;
958 ecp_point PP;
959
960 ecp_point_init( &PP );
961
962 MPI_CHK( ecp_add( grp, &PP, P, P ) );
963
964 MPI_CHK( ecp_copy( &T[0], P ) );
965
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100966 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100967 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
968
969 /*
970 * T[0] = P already has normalized coordinates
971 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100972 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100973
974cleanup:
975
976 ecp_point_free( &PP );
977
978 return( ret );
979}
980
981/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100982 * Maximum length of the precomputed table
983 */
984#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
985
986/*
987 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
988 * (that is: grp->nbits / w + 1)
989 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
990 */
991#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
992
993/*
994 * Integer multiplication: R = m * P
995 *
996 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
997 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
998 *
999 * This function executes a fixed number of operations for
1000 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001001 */
1002int ecp_mul( const ecp_group *grp, ecp_point *R,
1003 const mpi *m, const ecp_point *P )
1004{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001005 int ret;
1006 unsigned char w, m_is_odd;
1007 size_t pre_len, naf_len, i, j;
1008 signed char naf[ MAX_NAF_LEN ];
1009 ecp_point Q, T[ MAX_PRE_LEN ];
1010 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001011
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001012 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001013 return( POLARSSL_ERR_ECP_GENERIC );
1014
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001015 w = grp->nbits >= 521 ? 6 :
1016 grp->nbits >= 224 ? 5 :
1017 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001018
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001019 /*
1020 * Make sure w is within the limits.
1021 * The last test ensures that none of the precomputed points is zero,
1022 * which wouldn't be handled correctly by ecp_normalize_many().
1023 * It is only useful for small curves, as used in the test suite.
1024 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001025 if( w > POLARSSL_ECP_WINDOW_SIZE )
1026 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001027 if( w < 2 || w >= grp->nbits )
1028 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001029
1030 pre_len = 1 << ( w - 1 );
1031 naf_len = grp->nbits / w + 1;
1032
1033 mpi_init( &M );
1034 ecp_point_init( &Q );
1035 for( i = 0; i < pre_len; i++ )
1036 ecp_point_init( &T[i] );
1037
1038 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1039
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001040 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001041 * Make sure M is odd:
1042 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001043 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001044 MPI_CHK( mpi_copy( &M, m ) );
1045 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001046
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001047 /*
1048 * Compute the fixed-pattern NAF and precompute odd multiples
1049 */
1050 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1051 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001052
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001053 /*
1054 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1055 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1056 *
1057 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1058 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1059 * == T[ - naf[i] - 1 ]
1060 */
1061 MPI_CHK( ecp_set_zero( &Q ) );
1062 i = naf_len - 1;
1063 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001064 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001065 if( naf[i] < 0 )
1066 {
1067 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1068 }
1069 else
1070 {
1071 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1072 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001073
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001074 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001075 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001076 i--;
1077
1078 for( j = 0; j < w; j++ )
1079 {
1080 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1081 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001082 }
1083
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001084 /*
1085 * Now get m * P from M * P.
1086 * Since we don't need T[] any more, we can recycle it:
1087 * we already have T[0] = P, now set T[1] = 2 * P.
1088 */
1089 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1090 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001091
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001092
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001093cleanup:
1094
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001095 mpi_free( &M );
1096 ecp_point_free( &Q );
1097 for( i = 0; i < pre_len; i++ )
1098 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001099
1100 return( ret );
1101}
1102
1103
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001104#if defined(POLARSSL_SELF_TEST)
1105
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001106/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001107 * Checkup routine
1108 */
1109int ecp_self_test( int verbose )
1110{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001111 int ret;
1112 size_t i;
1113 ecp_group grp;
1114 ecp_point R;
1115 mpi m;
1116 unsigned long add_c_prev, dbl_c_prev;
1117 char *exponents[] =
1118 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001119 "000000000000000000000000000000000000000000000000", /* zero */
1120 "000000000000000000000000000000000000000000000001", /* one */
1121 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1122 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001123 "400000000000000000000000000000000000000000000000",
1124 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1125 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001126 };
1127
1128 ecp_group_init( &grp );
1129 ecp_point_init( &R );
1130 mpi_init( &m );
1131
1132 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1133
1134 if( verbose != 0 )
1135 printf( " ECP test #1 (SPA resistance): " );
1136
1137 add_count = 0;
1138 dbl_count = 0;
1139 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1140 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1141
1142 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1143 {
1144 add_c_prev = add_count;
1145 dbl_c_prev = dbl_count;
1146 add_count = 0;
1147 dbl_count = 0;
1148
1149 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1150 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1151
1152 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1153 {
1154 if( verbose != 0 )
1155 printf( "failed (%zu)\n", i );
1156
1157 ret = 1;
1158 goto cleanup;
1159 }
1160 }
1161
1162 if( verbose != 0 )
1163 printf( "passed\n" );
1164
1165cleanup:
1166
1167 if( ret < 0 && verbose != 0 )
1168 printf( "Unexpected error, return code = %08X\n", ret );
1169
1170 ecp_group_free( &grp );
1171 ecp_point_free( &R );
1172 mpi_free( &m );
1173
1174 if( verbose != 0 )
1175 printf( "\n" );
1176
1177 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001178}
1179
1180#endif
1181
1182#endif