blob: b8b0dfc7431576e5455270c204a0ae466cd57ea0 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
Paul Bakkercf4365f2013-01-16 17:00:43 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-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é-Gonnard6545ca72013-01-26 16:05:22 +0100125 * Tell if a point is zero
126 */
127int ecp_is_zero( ecp_point *pt )
128{
129 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
130}
131
132/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100133 * Copy the contents of Q into P
134 */
135int ecp_copy( ecp_point *P, const ecp_point *Q )
136{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100137 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100138
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100139 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
140 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100141 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100142
143cleanup:
144 return( ret );
145}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100146
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100147/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100148 * Import a non-zero point from ASCII strings
149 */
150int ecp_point_read_string( ecp_point *P, int radix,
151 const char *x, const char *y )
152{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100153 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100154
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100155 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
156 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100157 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100158
159cleanup:
160 return( ret );
161}
162
163/*
164 * Import an ECP group from ASCII strings
165 */
166int ecp_group_read_string( ecp_group *grp, int radix,
167 const char *p, const char *b,
168 const char *gx, const char *gy, const char *n)
169{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100170 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100171
172 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
173 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
174 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
175 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
176
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100177 grp->pbits = mpi_msb( &grp->P );
178 grp->nbits = mpi_msb( &grp->N );
179
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100180cleanup:
181 return( ret );
182}
183
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100184/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100185 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100186 */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100187int ecp_write_binary( const ecp_group *grp, const ecp_point *P, int format,
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100188 size_t *olen, unsigned char *buf, size_t buflen )
189{
190 int ret;
191 size_t plen;
192
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100193 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
194 format != POLARSSL_ECP_PF_COMPRESSED )
195 return( POLARSSL_ERR_ECP_GENERIC );
196
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100197 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100198 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100199 */
200 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
201 {
202 if( buflen < 1 )
203 return( POLARSSL_ERR_ECP_GENERIC );
204
205 buf[0] = 0x00;
206 *olen = 1;
207
208 return( 0 );
209 }
210
211 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100212
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100213 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
214 {
215 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100216
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100217 if( buflen < *olen )
218 return( POLARSSL_ERR_ECP_GENERIC );
219
220 buf[0] = 0x04;
221 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
222 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
223 }
224 else if( format == POLARSSL_ECP_PF_COMPRESSED )
225 {
226 *olen = plen + 1;
227
228 if( buflen < *olen )
229 return( POLARSSL_ERR_ECP_GENERIC );
230
231 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
232 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
233 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100234
235cleanup:
236 return( ret );
237}
238
239/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100240 * Import a point from unsigned binary data (SEC1 2.3.4)
241 */
242int ecp_read_binary( const ecp_group *grp, ecp_point *P, int format,
243 const unsigned char *buf, size_t ilen ) {
244 int ret;
245 size_t plen;
246
247 if( format != POLARSSL_ECP_PF_UNCOMPRESSED )
248 return( POLARSSL_ERR_ECP_GENERIC );
249
250 if( ilen == 1 && buf[0] == 0x00 )
251 return( ecp_set_zero( P ) );
252
253 plen = mpi_size( &grp-> P );
254
255 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
256 return( POLARSSL_ERR_ECP_GENERIC );
257
258 MPI_CHK( mpi_read_binary( &P->X, buf + 1, plen ) );
259 MPI_CHK( mpi_read_binary( &P->Y, buf + 1 + plen, plen ) );
260 MPI_CHK( mpi_lset( &P->Z, 1 ) );
261
262cleanup:
263 return( ret );
264}
265
266/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100267 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
268 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100269 */
270static int ecp_modp( mpi *N, const ecp_group *grp )
271{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100272 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100273
274 if( grp->modp == NULL )
275 return( mpi_mod_mpi( N, N, &grp->P ) );
276
277 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
278 return( POLARSSL_ERR_ECP_GENERIC );
279
280 MPI_CHK( grp->modp( N ) );
281
282 while( mpi_cmp_int( N, 0 ) < 0 )
283 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
284
285 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
286 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
287
288cleanup:
289 return( ret );
290}
291
292/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100293 * 192 bits in terms of t_uint
294 */
295#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
296
297/*
298 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
299 * -1 means let this chunk be 0
300 * a positive value i means A_i.
301 */
302#define P192_CHUNKS 3
303#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
304#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
305
306const signed char p192_tbl[][P192_CHUNKS] = {
307 { -1, 3, 3 }, /* S1 */
308 { 4, 4, -1 }, /* S2 */
309 { 5, 5, 5 }, /* S3 */
310};
311
312/*
313 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
314 */
315static int ecp_mod_p192( mpi *N )
316{
317 int ret;
318 unsigned char i, j, offset;
319 signed char chunk;
320 mpi tmp, acc;
321 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
322
323 tmp.s = 1;
324 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
325 tmp.p = tmp_p;
326
327 acc.s = 1;
328 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
329 acc.p = acc_p;
330
331 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
332
333 /*
334 * acc = T
335 */
336 memset( acc_p, 0, sizeof( acc_p ) );
337 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
338
339 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
340 {
341 /*
342 * tmp = S_i
343 */
344 memset( tmp_p, 0, sizeof( tmp_p ) );
345 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
346 {
347 chunk = p192_tbl[i][j];
348 if( chunk >= 0 )
349 memcpy( tmp_p + offset * P192_CHUNK_INT,
350 N->p + chunk * P192_CHUNK_INT,
351 P192_CHUNK_CHAR );
352 }
353
354 /*
355 * acc += tmp
356 */
357 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
358 }
359
360 MPI_CHK( mpi_copy( N, &acc ) );
361
362cleanup:
363 return( ret );
364}
365
366/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100367 * Size of p521 in terms of t_uint
368 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100369#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100370
371/*
372 * Bits to keep in the most significant t_uint
373 */
374#if defined(POLARSS_HAVE_INT8)
375#define P521_MASK 0x01
376#else
377#define P521_MASK 0x01FF
378#endif
379
380/*
381 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100382 */
383static int ecp_mod_p521( mpi *N )
384{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100385 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100386 t_uint Mp[P521_SIZE_INT];
387 mpi M;
388
389 if( N->n < P521_SIZE_INT )
390 return( 0 );
391
392 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
393 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
394 Mp[P521_SIZE_INT - 1] &= P521_MASK;
395
396 M.s = 1;
397 M.n = P521_SIZE_INT;
398 M.p = Mp;
399
400 MPI_CHK( mpi_shift_r( N, 521 ) );
401
402 MPI_CHK( mpi_add_abs( N, N, &M ) );
403
404cleanup:
405 return( ret );
406}
407
408/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100409 * Domain parameters for secp192r1
410 */
411#define SECP192R1_P \
412 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
413#define SECP192R1_B \
414 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
415#define SECP192R1_GX \
416 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
417#define SECP192R1_GY \
418 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
419#define SECP192R1_N \
420 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
421
422/*
423 * Domain parameters for secp224r1
424 */
425#define SECP224R1_P \
426 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
427#define SECP224R1_B \
428 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
429#define SECP224R1_GX \
430 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
431#define SECP224R1_GY \
432 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
433#define SECP224R1_N \
434 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
435
436/*
437 * Domain parameters for secp256r1
438 */
439#define SECP256R1_P \
440 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
441#define SECP256R1_B \
442 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
443#define SECP256R1_GX \
444 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
445#define SECP256R1_GY \
446 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
447#define SECP256R1_N \
448 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
449
450/*
451 * Domain parameters for secp384r1
452 */
453#define SECP384R1_P \
454 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
455 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
456#define SECP384R1_B \
457 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
458 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
459#define SECP384R1_GX \
460 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
461 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
462#define SECP384R1_GY \
463 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
464 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
465#define SECP384R1_N \
466 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
467 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
468
469/*
470 * Domain parameters for secp521r1
471 */
472#define SECP521R1_P \
473 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
474 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
475 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
476#define SECP521R1_B \
477 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
478 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
479 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
480#define SECP521R1_GX \
481 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
482 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
483 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
484#define SECP521R1_GY \
485 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
486 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
487 "3FAD0761353C7086A272C24088BE94769FD16650"
488#define SECP521R1_N \
489 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
490 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
491 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
492
493/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100494 * Set a group using well-known domain parameters
495 */
496int ecp_use_known_dp( ecp_group *grp, size_t index )
497{
498 switch( index )
499 {
500 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100501 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100502 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100503 SECP192R1_P, SECP192R1_B,
504 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
505
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100506 case POLARSSL_ECP_DP_SECP224R1:
507 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100508 SECP224R1_P, SECP224R1_B,
509 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
510
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100511 case POLARSSL_ECP_DP_SECP256R1:
512 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100513 SECP256R1_P, SECP256R1_B,
514 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
515
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100516 case POLARSSL_ECP_DP_SECP384R1:
517 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100518 SECP384R1_P, SECP384R1_B,
519 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
520
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100521 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100522 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100523 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100524 SECP521R1_P, SECP521R1_B,
525 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100526 }
527
528 return( POLARSSL_ERR_ECP_GENERIC );
529}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100530
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100531/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100532 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100533 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100534 * In order to guarantee that, we need to ensure that operands of
535 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100536 * bring the result back to this range.
537 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100538 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100539 */
540
541/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100542 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
543 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100544#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100545
546/*
547 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
548 */
549#define MOD_SUB( N ) \
550 while( mpi_cmp_int( &N, 0 ) < 0 ) \
551 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
552
553/*
554 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
555 */
556#define MOD_ADD( N ) \
557 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
558 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
559
560/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100561 * Check that a point is valid as a public key (SEC1 3.2.3.1)
562 */
563int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
564{
565 int ret;
566 mpi YY, RHS;
567
568 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
569 return( POLARSSL_ERR_ECP_GENERIC );
570
571 /*
572 * pt coordinates must be normalized for our checks
573 */
574 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
575 return( POLARSSL_ERR_ECP_GENERIC );
576
577 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
578 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
579 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
580 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
581 return( POLARSSL_ERR_ECP_GENERIC );
582
583 mpi_init( &YY ); mpi_init( &RHS );
584
585 /*
586 * YY = Y^2
587 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
588 */
589 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
590 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
591 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
592 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
593 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
594
595 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
596 ret = POLARSSL_ERR_ECP_GENERIC;
597
598cleanup:
599
600 mpi_free( &YY ); mpi_free( &RHS );
601
602 return( ret );
603}
604
605/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100606 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100607 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100608static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100609{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100610 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100611 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100612
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100613 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100614 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100615
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100616 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100617
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100618 /*
619 * X = X / Z^2 mod p
620 */
621 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
622 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
623 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100624
625 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100626 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100627 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100628 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
629 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100630
631 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100632 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100633 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100634 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100635
636cleanup:
637
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100638 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100639
640 return( ret );
641}
642
643/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100644 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100645 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100646 * (See for example Cohen's "A Course in Computational Algebraic Number
647 * Theory", Algorithm 10.3.4.)
648 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100649 * Warning: fails if one of the points is zero!
650 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100651 */
652static int ecp_normalize_many( const ecp_group *grp,
653 ecp_point T[], size_t t_len )
654{
655 int ret;
656 size_t i;
657 mpi *c, u, Zi, ZZi;
658
659 if( t_len < 2 )
660 return( ecp_normalize( grp, T ) );
661
662 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
663 return( POLARSSL_ERR_ECP_GENERIC );
664
665 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
666 for( i = 0; i < t_len; i++ )
667 mpi_init( &c[i] );
668
669 /*
670 * c[i] = Z_0 * ... * Z_i
671 */
672 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
673 for( i = 1; i < t_len; i++ )
674 {
675 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
676 MOD_MUL( c[i] );
677 }
678
679 /*
680 * u = 1 / (Z_0 * ... * Z_n) mod P
681 */
682 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
683
684 for( i = t_len - 1; ; i-- )
685 {
686 /*
687 * Zi = 1 / Z_i mod p
688 * u = 1 / (Z_0 * ... * Z_i) mod P
689 */
690 if( i == 0 ) {
691 MPI_CHK( mpi_copy( &Zi, &u ) );
692 }
693 else
694 {
695 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
696 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
697 }
698
699 /*
700 * proceed as in normalize()
701 */
702 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
703 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
704 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
705 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
706 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
707
708 if( i == 0 )
709 break;
710 }
711
712cleanup:
713
714 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
715 for( i = 0; i < t_len; i++ )
716 mpi_free( &c[i] );
717 free( c );
718
719 return( ret );
720}
721
722
723/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100724 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
725 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100726static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
727 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100728{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100729 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100730 mpi T1, T2, T3, X, Y, Z;
731
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100732#if defined(POLARSSL_SELF_TEST)
733 dbl_count++;
734#endif
735
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100736 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100737 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100738
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100739 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
740 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
741
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_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
744 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
745 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
746 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
747 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
748 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
749 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
750 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
751 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100752
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100753 /*
754 * For Y = Y / 2 mod p, we must make sure that Y is even before
755 * using right-shift. No need to reduce mod p afterwards.
756 */
757 if( mpi_get_bit( &Y, 0 ) == 1 )
758 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
759 MPI_CHK( mpi_shift_r( &Y, 1 ) );
760
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100761 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
762 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
763 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
764 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
765 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
766 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100767
768 MPI_CHK( mpi_copy( &R->X, &X ) );
769 MPI_CHK( mpi_copy( &R->Y, &Y ) );
770 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100771
772cleanup:
773
774 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
775 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
776
777 return( ret );
778}
779
780/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100781 * Addition or subtraction: R = P + Q or R = P + Q,
782 * mixed affine-Jacobian coordinates (GECC 3.22)
783 *
784 * The coordinates of Q must be normalized (= affine),
785 * but those of P don't need to. R is not normalized.
786 *
787 * If sign >= 0, perform addition, otherwise perform subtraction,
788 * taking advantage of the fact that, for Q != 0, we have
789 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100790 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100791static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100792 const ecp_point *P, const ecp_point *Q,
793 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100794{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100795 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100796 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100797
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100798#if defined(POLARSSL_SELF_TEST)
799 add_count++;
800#endif
801
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100802 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100803 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100804 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100805 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100806 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
807 return( ecp_copy( R, P ) );
808
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100809 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
810 {
811 ret = ecp_copy( R, Q );
812
813 /*
814 * -R.Y mod P = P - R.Y unless R.Y == 0
815 */
816 if( ret == 0 && sign < 0)
817 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
818 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
819
820 return( ret );
821 }
822
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100823 /*
824 * Make sure Q coordinates are normalized
825 */
826 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
827 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100828
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100829 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
830 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100831
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100832 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
833 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
834 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
835 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100836
837 /*
838 * For subtraction, -Q.Y should have been used instead of Q.Y,
839 * so we replace T2 by -T2, which is P - T2 mod P
840 */
841 if( sign < 0 )
842 {
843 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
844 MOD_SUB( T2 );
845 }
846
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100847 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
848 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100849
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100850 if( mpi_cmp_int( &T1, 0 ) == 0 )
851 {
852 if( mpi_cmp_int( &T2, 0 ) == 0 )
853 {
854 ret = ecp_double_jac( grp, R, P );
855 goto cleanup;
856 }
857 else
858 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100859 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100860 goto cleanup;
861 }
862 }
863
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100864 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
865 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
866 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
867 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
868 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
869 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
870 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
871 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
872 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
873 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
874 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
875 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100876
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100877 MPI_CHK( mpi_copy( &R->X, &X ) );
878 MPI_CHK( mpi_copy( &R->Y, &Y ) );
879 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100880
881cleanup:
882
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100883 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
884 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100885
886 return( ret );
887}
888
889/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100890 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100891 */
892int ecp_add( const ecp_group *grp, ecp_point *R,
893 const ecp_point *P, const ecp_point *Q )
894{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100895 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100896
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100897 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
898 MPI_CHK( ecp_normalize( grp, R ) );
899
900cleanup:
901 return( ret );
902}
903
904/*
905 * Subtraction: R = P - Q, result's coordinates normalized
906 */
907int ecp_sub( const ecp_group *grp, ecp_point *R,
908 const ecp_point *P, const ecp_point *Q )
909{
910 int ret;
911
912 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100913 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100914
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100915cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100916 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100917}
918
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100919/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100920 * Compute a modified width-w non-adjacent form (NAF) of a number,
921 * with a fixed pattern for resistance to SPA/timing attacks,
922 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
923 * (The resulting multiplication algorithm can also been seen as a
924 * modification of 2^w-ary multiplication, with signed coefficients,
925 * all of them odd.)
926 *
927 * Input:
928 * m must be an odd positive mpi less than w * k bits long
929 * x must be an array of k elements
930 * w must be less than a certain maximum (currently 8)
931 *
932 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
933 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
934 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
935 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
936 *
937 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
938 * p. 335 of the cited reference, here we return only u, not d_w since
939 * it is known that the other d_w[j] will be 0. Moreover, the returned
940 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
941 * that u_i is odd. Also, since we always select a positive value for d
942 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
943 * does. Finally, there is an off-by-one error in the reference: the
944 * last index should be k-1, not k.
945 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100946static int ecp_w_naf_fixed( signed char x[], size_t k,
947 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100948{
949 int ret;
950 unsigned int i, u, mask, carry;
951 mpi M;
952
953 mpi_init( &M );
954
955 MPI_CHK( mpi_copy( &M, m ) );
956 mask = ( 1 << w ) - 1;
957 carry = 1 << ( w - 1 );
958
959 for( i = 0; i < k; i++ )
960 {
961 u = M.p[0] & mask;
962
963 if( ( u & 1 ) == 0 && i > 0 )
964 x[i - 1] -= carry;
965
966 x[i] = u >> 1;
967 mpi_shift_r( &M, w );
968 }
969
970 /*
971 * We should have consumed all the bits now
972 */
973 if( mpi_cmp_int( &M, 0 ) != 0 )
974 ret = POLARSSL_ERR_ECP_GENERIC;
975
976cleanup:
977
978 mpi_free( &M );
979
980 return( ret );
981}
982
983/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100984 * Precompute odd multiples of P up to (2 * t_len - 1) P.
985 * The table is filled with T[i] = (2 * i + 1) P.
986 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100987static int ecp_precompute( const ecp_group *grp,
988 ecp_point T[], size_t t_len,
989 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100990{
991 int ret;
992 size_t i;
993 ecp_point PP;
994
995 ecp_point_init( &PP );
996
997 MPI_CHK( ecp_add( grp, &PP, P, P ) );
998
999 MPI_CHK( ecp_copy( &T[0], P ) );
1000
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001001 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001002 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1003
1004 /*
1005 * T[0] = P already has normalized coordinates
1006 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001007 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001008
1009cleanup:
1010
1011 ecp_point_free( &PP );
1012
1013 return( ret );
1014}
1015
1016/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001017 * Maximum length of the precomputed table
1018 */
1019#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1020
1021/*
1022 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1023 * (that is: grp->nbits / w + 1)
1024 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1025 */
1026#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1027
1028/*
1029 * Integer multiplication: R = m * P
1030 *
1031 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1032 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1033 *
1034 * This function executes a fixed number of operations for
1035 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001036 */
1037int ecp_mul( const ecp_group *grp, ecp_point *R,
1038 const mpi *m, const ecp_point *P )
1039{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001040 int ret;
1041 unsigned char w, m_is_odd;
1042 size_t pre_len, naf_len, i, j;
1043 signed char naf[ MAX_NAF_LEN ];
1044 ecp_point Q, T[ MAX_PRE_LEN ];
1045 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001046
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001047 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001048 return( POLARSSL_ERR_ECP_GENERIC );
1049
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001050 w = grp->nbits >= 521 ? 6 :
1051 grp->nbits >= 224 ? 5 :
1052 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001053
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001054 /*
1055 * Make sure w is within the limits.
1056 * The last test ensures that none of the precomputed points is zero,
1057 * which wouldn't be handled correctly by ecp_normalize_many().
1058 * It is only useful for small curves, as used in the test suite.
1059 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001060 if( w > POLARSSL_ECP_WINDOW_SIZE )
1061 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001062 if( w < 2 || w >= grp->nbits )
1063 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001064
1065 pre_len = 1 << ( w - 1 );
1066 naf_len = grp->nbits / w + 1;
1067
1068 mpi_init( &M );
1069 ecp_point_init( &Q );
1070 for( i = 0; i < pre_len; i++ )
1071 ecp_point_init( &T[i] );
1072
1073 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1074
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001075 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001076 * Make sure M is odd:
1077 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001078 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001079 MPI_CHK( mpi_copy( &M, m ) );
1080 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001081
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001082 /*
1083 * Compute the fixed-pattern NAF and precompute odd multiples
1084 */
1085 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1086 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001087
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001088 /*
1089 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1090 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1091 *
1092 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1093 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1094 * == T[ - naf[i] - 1 ]
1095 */
1096 MPI_CHK( ecp_set_zero( &Q ) );
1097 i = naf_len - 1;
1098 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001099 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001100 if( naf[i] < 0 )
1101 {
1102 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1103 }
1104 else
1105 {
1106 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1107 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001108
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001109 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001110 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001111 i--;
1112
1113 for( j = 0; j < w; j++ )
1114 {
1115 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1116 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001117 }
1118
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001119 /*
1120 * Now get m * P from M * P.
1121 * Since we don't need T[] any more, we can recycle it:
1122 * we already have T[0] = P, now set T[1] = 2 * P.
1123 */
1124 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1125 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001126
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001127
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001128cleanup:
1129
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001130 mpi_free( &M );
1131 ecp_point_free( &Q );
1132 for( i = 0; i < pre_len; i++ )
1133 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001134
1135 return( ret );
1136}
1137
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001138/*
1139 * Generate a keypair (SEC1 3.2.1)
1140 */
1141int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1142 int (*f_rng)(void *, unsigned char *, size_t),
1143 void *p_rng )
1144{
1145 int count = 0;
1146 size_t n_size = (grp->nbits + 7) / 8;
1147
1148 /*
1149 * Generate d such that 1 <= n < N
1150 */
1151 do
1152 {
1153 mpi_fill_random( d, n_size, f_rng, p_rng );
1154
1155 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1156 mpi_shift_r( d, 1 );
1157
1158 if( count++ > 10 )
1159 return( POLARSSL_ERR_ECP_GENERIC );
1160 }
1161 while( mpi_cmp_int( d, 1 ) < 0 );
1162
1163 return( ecp_mul( grp, Q, d, &grp->G ) );
1164}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001165
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001166#if defined(POLARSSL_SELF_TEST)
1167
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001168/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001169 * Checkup routine
1170 */
1171int ecp_self_test( int verbose )
1172{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001173 int ret;
1174 size_t i;
1175 ecp_group grp;
1176 ecp_point R;
1177 mpi m;
1178 unsigned long add_c_prev, dbl_c_prev;
1179 char *exponents[] =
1180 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001181 "000000000000000000000000000000000000000000000000", /* zero */
1182 "000000000000000000000000000000000000000000000001", /* one */
1183 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1184 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001185 "400000000000000000000000000000000000000000000000",
1186 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1187 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001188 };
1189
1190 ecp_group_init( &grp );
1191 ecp_point_init( &R );
1192 mpi_init( &m );
1193
1194 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1195
1196 if( verbose != 0 )
1197 printf( " ECP test #1 (SPA resistance): " );
1198
1199 add_count = 0;
1200 dbl_count = 0;
1201 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1202 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1203
1204 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1205 {
1206 add_c_prev = add_count;
1207 dbl_c_prev = dbl_count;
1208 add_count = 0;
1209 dbl_count = 0;
1210
1211 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1212 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1213
1214 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1215 {
1216 if( verbose != 0 )
1217 printf( "failed (%zu)\n", i );
1218
1219 ret = 1;
1220 goto cleanup;
1221 }
1222 }
1223
1224 if( verbose != 0 )
1225 printf( "passed\n" );
1226
1227cleanup:
1228
1229 if( ret < 0 && verbose != 0 )
1230 printf( "Unexpected error, return code = %08X\n", ret );
1231
1232 ecp_group_free( &grp );
1233 ecp_point_free( &R );
1234 mpi_free( &m );
1235
1236 if( verbose != 0 )
1237 printf( "\n" );
1238
1239 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001240}
1241
1242#endif
1243
1244#endif