blob: 0a27df2be3857f6567fac8e8351b6f350abb72aa [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é-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010033 */
34
35#include "polarssl/config.h"
36
37#if defined(POLARSSL_ECP_C)
38
39#include "polarssl/ecp.h"
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010040#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010041#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010042
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010043#if defined(POLARSSL_SELF_TEST)
44/*
45 * Counts of point addition and doubling operations.
46 * Used to test resistance of point multiplication to SPA/timing attacks.
47 */
48unsigned long add_count, dbl_count;
49#endif
50
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010051/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010052 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010053 */
54void ecp_point_init( ecp_point *pt )
55{
56 if( pt == NULL )
57 return;
58
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010059 mpi_init( &pt->X );
60 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010061 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010062}
63
64/*
65 * Initialize (the components of) a group
66 */
67void ecp_group_init( ecp_group *grp )
68{
69 if( grp == NULL )
70 return;
71
72 mpi_init( &grp->P );
73 mpi_init( &grp->B );
74 ecp_point_init( &grp->G );
75 mpi_init( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010076
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010077 grp->pbits = 0;
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010078 grp->nbits = 0;
79
80 grp->modp = NULL;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010081}
82
83/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010084 * Unallocate (the components of) a point
85 */
86void ecp_point_free( ecp_point *pt )
87{
88 if( pt == NULL )
89 return;
90
91 mpi_free( &( pt->X ) );
92 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010093 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010094}
95
96/*
97 * Unallocate (the components of) a group
98 */
99void ecp_group_free( ecp_group *grp )
100{
101 if( grp == NULL )
102 return;
103
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100104 mpi_free( &grp->P );
105 mpi_free( &grp->B );
106 ecp_point_free( &grp->G );
107 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100108}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100109
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100110/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100111 * Set point to zero
112 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100113int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100114{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100115 int ret;
116
117 MPI_CHK( mpi_lset( &pt->X , 1 ) );
118 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
119 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
120
121cleanup:
122 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100123}
124
125/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100126 * Tell if a point is zero
127 */
128int ecp_is_zero( ecp_point *pt )
129{
130 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
131}
132
133/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100134 * Copy the contents of Q into P
135 */
136int ecp_copy( ecp_point *P, const ecp_point *Q )
137{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100138 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100139
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100140 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
141 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100142 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100143
144cleanup:
145 return( ret );
146}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100147
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100148/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100149 * Import a non-zero point from ASCII strings
150 */
151int ecp_point_read_string( ecp_point *P, int radix,
152 const char *x, const char *y )
153{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100154 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100155
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100156 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
157 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100158 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100159
160cleanup:
161 return( ret );
162}
163
164/*
165 * Import an ECP group from ASCII strings
166 */
167int ecp_group_read_string( ecp_group *grp, int radix,
168 const char *p, const char *b,
169 const char *gx, const char *gy, const char *n)
170{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100171 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100172
173 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
174 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
175 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
176 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
177
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100178 grp->pbits = mpi_msb( &grp->P );
179 grp->nbits = mpi_msb( &grp->N );
180
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100181cleanup:
182 return( ret );
183}
184
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100185/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100186 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100187 */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100188int ecp_write_binary( const ecp_group *grp, const ecp_point *P, int format,
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100189 size_t *olen, unsigned char *buf, size_t buflen )
190{
191 int ret;
192 size_t plen;
193
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100194 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
195 format != POLARSSL_ECP_PF_COMPRESSED )
196 return( POLARSSL_ERR_ECP_GENERIC );
197
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100198 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100199 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100200 */
201 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
202 {
203 if( buflen < 1 )
204 return( POLARSSL_ERR_ECP_GENERIC );
205
206 buf[0] = 0x00;
207 *olen = 1;
208
209 return( 0 );
210 }
211
212 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100213
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100214 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
215 {
216 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100217
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100218 if( buflen < *olen )
219 return( POLARSSL_ERR_ECP_GENERIC );
220
221 buf[0] = 0x04;
222 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
223 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
224 }
225 else if( format == POLARSSL_ECP_PF_COMPRESSED )
226 {
227 *olen = plen + 1;
228
229 if( buflen < *olen )
230 return( POLARSSL_ERR_ECP_GENERIC );
231
232 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
233 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
234 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100235
236cleanup:
237 return( ret );
238}
239
240/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100241 * Import a point from unsigned binary data (SEC1 2.3.4)
242 */
243int ecp_read_binary( const ecp_group *grp, ecp_point *P, int format,
244 const unsigned char *buf, size_t ilen ) {
245 int ret;
246 size_t plen;
247
248 if( format != POLARSSL_ECP_PF_UNCOMPRESSED )
249 return( POLARSSL_ERR_ECP_GENERIC );
250
251 if( ilen == 1 && buf[0] == 0x00 )
252 return( ecp_set_zero( P ) );
253
254 plen = mpi_size( &grp-> P );
255
256 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
257 return( POLARSSL_ERR_ECP_GENERIC );
258
259 MPI_CHK( mpi_read_binary( &P->X, buf + 1, plen ) );
260 MPI_CHK( mpi_read_binary( &P->Y, buf + 1 + plen, plen ) );
261 MPI_CHK( mpi_lset( &P->Z, 1 ) );
262
263cleanup:
264 return( ret );
265}
266
267/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100268 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
269 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100270 */
271static int ecp_modp( mpi *N, const ecp_group *grp )
272{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100273 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100274
275 if( grp->modp == NULL )
276 return( mpi_mod_mpi( N, N, &grp->P ) );
277
278 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
279 return( POLARSSL_ERR_ECP_GENERIC );
280
281 MPI_CHK( grp->modp( N ) );
282
283 while( mpi_cmp_int( N, 0 ) < 0 )
284 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
285
286 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
287 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
288
289cleanup:
290 return( ret );
291}
292
293/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100294 * 192 bits in terms of t_uint
295 */
296#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
297
298/*
299 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
300 * -1 means let this chunk be 0
301 * a positive value i means A_i.
302 */
303#define P192_CHUNKS 3
304#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
305#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
306
307const signed char p192_tbl[][P192_CHUNKS] = {
308 { -1, 3, 3 }, /* S1 */
309 { 4, 4, -1 }, /* S2 */
310 { 5, 5, 5 }, /* S3 */
311};
312
313/*
314 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
315 */
316static int ecp_mod_p192( mpi *N )
317{
318 int ret;
319 unsigned char i, j, offset;
320 signed char chunk;
321 mpi tmp, acc;
322 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
323
324 tmp.s = 1;
325 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
326 tmp.p = tmp_p;
327
328 acc.s = 1;
329 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
330 acc.p = acc_p;
331
332 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
333
334 /*
335 * acc = T
336 */
337 memset( acc_p, 0, sizeof( acc_p ) );
338 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
339
340 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
341 {
342 /*
343 * tmp = S_i
344 */
345 memset( tmp_p, 0, sizeof( tmp_p ) );
346 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
347 {
348 chunk = p192_tbl[i][j];
349 if( chunk >= 0 )
350 memcpy( tmp_p + offset * P192_CHUNK_INT,
351 N->p + chunk * P192_CHUNK_INT,
352 P192_CHUNK_CHAR );
353 }
354
355 /*
356 * acc += tmp
357 */
358 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
359 }
360
361 MPI_CHK( mpi_copy( N, &acc ) );
362
363cleanup:
364 return( ret );
365}
366
367/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100368 * Size of p521 in terms of t_uint
369 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100370#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100371
372/*
373 * Bits to keep in the most significant t_uint
374 */
375#if defined(POLARSS_HAVE_INT8)
376#define P521_MASK 0x01
377#else
378#define P521_MASK 0x01FF
379#endif
380
381/*
382 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100383 */
384static int ecp_mod_p521( mpi *N )
385{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100386 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100387 t_uint Mp[P521_SIZE_INT];
388 mpi M;
389
390 if( N->n < P521_SIZE_INT )
391 return( 0 );
392
393 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
394 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
395 Mp[P521_SIZE_INT - 1] &= P521_MASK;
396
397 M.s = 1;
398 M.n = P521_SIZE_INT;
399 M.p = Mp;
400
401 MPI_CHK( mpi_shift_r( N, 521 ) );
402
403 MPI_CHK( mpi_add_abs( N, N, &M ) );
404
405cleanup:
406 return( ret );
407}
408
409/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100410 * Domain parameters for secp192r1
411 */
412#define SECP192R1_P \
413 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
414#define SECP192R1_B \
415 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
416#define SECP192R1_GX \
417 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
418#define SECP192R1_GY \
419 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
420#define SECP192R1_N \
421 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
422
423/*
424 * Domain parameters for secp224r1
425 */
426#define SECP224R1_P \
427 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
428#define SECP224R1_B \
429 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
430#define SECP224R1_GX \
431 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
432#define SECP224R1_GY \
433 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
434#define SECP224R1_N \
435 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
436
437/*
438 * Domain parameters for secp256r1
439 */
440#define SECP256R1_P \
441 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
442#define SECP256R1_B \
443 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
444#define SECP256R1_GX \
445 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
446#define SECP256R1_GY \
447 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
448#define SECP256R1_N \
449 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
450
451/*
452 * Domain parameters for secp384r1
453 */
454#define SECP384R1_P \
455 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
456 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
457#define SECP384R1_B \
458 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
459 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
460#define SECP384R1_GX \
461 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
462 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
463#define SECP384R1_GY \
464 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
465 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
466#define SECP384R1_N \
467 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
468 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
469
470/*
471 * Domain parameters for secp521r1
472 */
473#define SECP521R1_P \
474 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
475 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
476 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
477#define SECP521R1_B \
478 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
479 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
480 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
481#define SECP521R1_GX \
482 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
483 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
484 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
485#define SECP521R1_GY \
486 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
487 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
488 "3FAD0761353C7086A272C24088BE94769FD16650"
489#define SECP521R1_N \
490 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
491 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
492 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
493
494/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100495 * Set a group using well-known domain parameters
496 */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100497int ecp_use_known_dp( ecp_group *grp, uint16_t index )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100498{
499 switch( index )
500 {
501 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100502 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100503 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100504 SECP192R1_P, SECP192R1_B,
505 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
506
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100507 case POLARSSL_ECP_DP_SECP224R1:
508 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100509 SECP224R1_P, SECP224R1_B,
510 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
511
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100512 case POLARSSL_ECP_DP_SECP256R1:
513 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100514 SECP256R1_P, SECP256R1_B,
515 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
516
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100517 case POLARSSL_ECP_DP_SECP384R1:
518 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100519 SECP384R1_P, SECP384R1_B,
520 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
521
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100522 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100523 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100524 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100525 SECP521R1_P, SECP521R1_B,
526 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100527 }
528
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100529 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
530}
531
532/*
533 * Set a group from an ECParameters record (RFC 4492)
534 */
535int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len )
536{
537 uint16_t namedcurve;
538
539 /*
540 * We expect at least three bytes (see below)
541 */
542 if( len < 3 )
543 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
544
545 /*
546 * First byte is curve_type; only named_curve is handled
547 */
548 if( *buf++ != POLARSSL_ECP_TLS_NAMED_CURVE )
549 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
550
551 /*
552 * Next two bytes are the namedcurve
553 */
554 namedcurve = 256 * buf[0] + buf[1];
555 return ecp_use_known_dp( grp, namedcurve );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100556}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100557
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100558/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100559 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100560 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100561 * In order to guarantee that, we need to ensure that operands of
562 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100563 * bring the result back to this range.
564 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100565 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100566 */
567
568/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100569 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
570 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100571#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100572
573/*
574 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
575 */
576#define MOD_SUB( N ) \
577 while( mpi_cmp_int( &N, 0 ) < 0 ) \
578 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
579
580/*
581 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
582 */
583#define MOD_ADD( N ) \
584 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
585 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
586
587/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100588 * Check that a point is valid as a public key (SEC1 3.2.3.1)
589 */
590int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
591{
592 int ret;
593 mpi YY, RHS;
594
595 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
596 return( POLARSSL_ERR_ECP_GENERIC );
597
598 /*
599 * pt coordinates must be normalized for our checks
600 */
601 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
602 return( POLARSSL_ERR_ECP_GENERIC );
603
604 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
605 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
606 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
607 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
608 return( POLARSSL_ERR_ECP_GENERIC );
609
610 mpi_init( &YY ); mpi_init( &RHS );
611
612 /*
613 * YY = Y^2
614 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
615 */
616 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
617 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
618 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
619 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
620 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
621
622 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
623 ret = POLARSSL_ERR_ECP_GENERIC;
624
625cleanup:
626
627 mpi_free( &YY ); mpi_free( &RHS );
628
629 return( ret );
630}
631
632/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100633 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100634 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100635static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100636{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100637 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100638 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100639
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100640 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100641 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100642
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100643 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100644
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100645 /*
646 * X = X / Z^2 mod p
647 */
648 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
649 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
650 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100651
652 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100653 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100654 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100655 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
656 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100657
658 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100659 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100660 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100661 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100662
663cleanup:
664
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100665 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100666
667 return( ret );
668}
669
670/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100671 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100672 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100673 * (See for example Cohen's "A Course in Computational Algebraic Number
674 * Theory", Algorithm 10.3.4.)
675 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100676 * Warning: fails if one of the points is zero!
677 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100678 */
679static int ecp_normalize_many( const ecp_group *grp,
680 ecp_point T[], size_t t_len )
681{
682 int ret;
683 size_t i;
684 mpi *c, u, Zi, ZZi;
685
686 if( t_len < 2 )
687 return( ecp_normalize( grp, T ) );
688
689 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
690 return( POLARSSL_ERR_ECP_GENERIC );
691
692 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
693 for( i = 0; i < t_len; i++ )
694 mpi_init( &c[i] );
695
696 /*
697 * c[i] = Z_0 * ... * Z_i
698 */
699 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
700 for( i = 1; i < t_len; i++ )
701 {
702 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
703 MOD_MUL( c[i] );
704 }
705
706 /*
707 * u = 1 / (Z_0 * ... * Z_n) mod P
708 */
709 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
710
711 for( i = t_len - 1; ; i-- )
712 {
713 /*
714 * Zi = 1 / Z_i mod p
715 * u = 1 / (Z_0 * ... * Z_i) mod P
716 */
717 if( i == 0 ) {
718 MPI_CHK( mpi_copy( &Zi, &u ) );
719 }
720 else
721 {
722 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
723 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
724 }
725
726 /*
727 * proceed as in normalize()
728 */
729 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
730 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
731 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
732 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
733 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
734
735 if( i == 0 )
736 break;
737 }
738
739cleanup:
740
741 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
742 for( i = 0; i < t_len; i++ )
743 mpi_free( &c[i] );
744 free( c );
745
746 return( ret );
747}
748
749
750/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100751 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
752 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100753static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
754 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100755{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100756 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100757 mpi T1, T2, T3, X, Y, Z;
758
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100759#if defined(POLARSSL_SELF_TEST)
760 dbl_count++;
761#endif
762
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100763 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100764 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100765
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100766 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
767 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
768
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100769 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
770 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
771 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
772 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
773 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
774 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
775 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
776 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
777 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
778 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100779
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100780 /*
781 * For Y = Y / 2 mod p, we must make sure that Y is even before
782 * using right-shift. No need to reduce mod p afterwards.
783 */
784 if( mpi_get_bit( &Y, 0 ) == 1 )
785 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
786 MPI_CHK( mpi_shift_r( &Y, 1 ) );
787
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100788 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
789 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
790 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
791 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
792 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
793 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100794
795 MPI_CHK( mpi_copy( &R->X, &X ) );
796 MPI_CHK( mpi_copy( &R->Y, &Y ) );
797 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100798
799cleanup:
800
801 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
802 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
803
804 return( ret );
805}
806
807/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100808 * Addition or subtraction: R = P + Q or R = P + Q,
809 * mixed affine-Jacobian coordinates (GECC 3.22)
810 *
811 * The coordinates of Q must be normalized (= affine),
812 * but those of P don't need to. R is not normalized.
813 *
814 * If sign >= 0, perform addition, otherwise perform subtraction,
815 * taking advantage of the fact that, for Q != 0, we have
816 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100817 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100818static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100819 const ecp_point *P, const ecp_point *Q,
820 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100821{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100822 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100823 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100824
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100825#if defined(POLARSSL_SELF_TEST)
826 add_count++;
827#endif
828
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100829 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100830 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100831 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100832 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100833 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
834 return( ecp_copy( R, P ) );
835
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100836 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
837 {
838 ret = ecp_copy( R, Q );
839
840 /*
841 * -R.Y mod P = P - R.Y unless R.Y == 0
842 */
843 if( ret == 0 && sign < 0)
844 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
845 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
846
847 return( ret );
848 }
849
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100850 /*
851 * Make sure Q coordinates are normalized
852 */
853 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
854 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100855
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100856 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
857 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100858
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100859 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
860 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
861 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
862 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100863
864 /*
865 * For subtraction, -Q.Y should have been used instead of Q.Y,
866 * so we replace T2 by -T2, which is P - T2 mod P
867 */
868 if( sign < 0 )
869 {
870 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
871 MOD_SUB( T2 );
872 }
873
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100874 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
875 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100876
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100877 if( mpi_cmp_int( &T1, 0 ) == 0 )
878 {
879 if( mpi_cmp_int( &T2, 0 ) == 0 )
880 {
881 ret = ecp_double_jac( grp, R, P );
882 goto cleanup;
883 }
884 else
885 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100886 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100887 goto cleanup;
888 }
889 }
890
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100891 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
892 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
893 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
894 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
895 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
896 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
897 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
898 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
899 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
900 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
901 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
902 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100903
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100904 MPI_CHK( mpi_copy( &R->X, &X ) );
905 MPI_CHK( mpi_copy( &R->Y, &Y ) );
906 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100907
908cleanup:
909
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100910 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
911 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100912
913 return( ret );
914}
915
916/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100917 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100918 */
919int ecp_add( const ecp_group *grp, ecp_point *R,
920 const ecp_point *P, const ecp_point *Q )
921{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100922 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100923
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100924 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
925 MPI_CHK( ecp_normalize( grp, R ) );
926
927cleanup:
928 return( ret );
929}
930
931/*
932 * Subtraction: R = P - Q, result's coordinates normalized
933 */
934int ecp_sub( const ecp_group *grp, ecp_point *R,
935 const ecp_point *P, const ecp_point *Q )
936{
937 int ret;
938
939 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100940 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100941
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100942cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100943 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100944}
945
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100946/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100947 * Compute a modified width-w non-adjacent form (NAF) of a number,
948 * with a fixed pattern for resistance to SPA/timing attacks,
949 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
950 * (The resulting multiplication algorithm can also been seen as a
951 * modification of 2^w-ary multiplication, with signed coefficients,
952 * all of them odd.)
953 *
954 * Input:
955 * m must be an odd positive mpi less than w * k bits long
956 * x must be an array of k elements
957 * w must be less than a certain maximum (currently 8)
958 *
959 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
960 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
961 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
962 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
963 *
964 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
965 * p. 335 of the cited reference, here we return only u, not d_w since
966 * it is known that the other d_w[j] will be 0. Moreover, the returned
967 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
968 * that u_i is odd. Also, since we always select a positive value for d
969 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
970 * does. Finally, there is an off-by-one error in the reference: the
971 * last index should be k-1, not k.
972 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100973static int ecp_w_naf_fixed( signed char x[], size_t k,
974 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100975{
976 int ret;
977 unsigned int i, u, mask, carry;
978 mpi M;
979
980 mpi_init( &M );
981
982 MPI_CHK( mpi_copy( &M, m ) );
983 mask = ( 1 << w ) - 1;
984 carry = 1 << ( w - 1 );
985
986 for( i = 0; i < k; i++ )
987 {
988 u = M.p[0] & mask;
989
990 if( ( u & 1 ) == 0 && i > 0 )
991 x[i - 1] -= carry;
992
993 x[i] = u >> 1;
994 mpi_shift_r( &M, w );
995 }
996
997 /*
998 * We should have consumed all the bits now
999 */
1000 if( mpi_cmp_int( &M, 0 ) != 0 )
1001 ret = POLARSSL_ERR_ECP_GENERIC;
1002
1003cleanup:
1004
1005 mpi_free( &M );
1006
1007 return( ret );
1008}
1009
1010/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001011 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1012 * The table is filled with T[i] = (2 * i + 1) P.
1013 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001014static int ecp_precompute( const ecp_group *grp,
1015 ecp_point T[], size_t t_len,
1016 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001017{
1018 int ret;
1019 size_t i;
1020 ecp_point PP;
1021
1022 ecp_point_init( &PP );
1023
1024 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1025
1026 MPI_CHK( ecp_copy( &T[0], P ) );
1027
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001028 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001029 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1030
1031 /*
1032 * T[0] = P already has normalized coordinates
1033 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001034 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001035
1036cleanup:
1037
1038 ecp_point_free( &PP );
1039
1040 return( ret );
1041}
1042
1043/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001044 * Maximum length of the precomputed table
1045 */
1046#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1047
1048/*
1049 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1050 * (that is: grp->nbits / w + 1)
1051 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1052 */
1053#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1054
1055/*
1056 * Integer multiplication: R = m * P
1057 *
1058 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1059 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1060 *
1061 * This function executes a fixed number of operations for
1062 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001063 */
1064int ecp_mul( const ecp_group *grp, ecp_point *R,
1065 const mpi *m, const ecp_point *P )
1066{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001067 int ret;
1068 unsigned char w, m_is_odd;
1069 size_t pre_len, naf_len, i, j;
1070 signed char naf[ MAX_NAF_LEN ];
1071 ecp_point Q, T[ MAX_PRE_LEN ];
1072 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001073
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001074 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001075 return( POLARSSL_ERR_ECP_GENERIC );
1076
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001077 w = grp->nbits >= 521 ? 6 :
1078 grp->nbits >= 224 ? 5 :
1079 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001080
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001081 /*
1082 * Make sure w is within the limits.
1083 * The last test ensures that none of the precomputed points is zero,
1084 * which wouldn't be handled correctly by ecp_normalize_many().
1085 * It is only useful for small curves, as used in the test suite.
1086 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001087 if( w > POLARSSL_ECP_WINDOW_SIZE )
1088 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001089 if( w < 2 || w >= grp->nbits )
1090 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001091
1092 pre_len = 1 << ( w - 1 );
1093 naf_len = grp->nbits / w + 1;
1094
1095 mpi_init( &M );
1096 ecp_point_init( &Q );
1097 for( i = 0; i < pre_len; i++ )
1098 ecp_point_init( &T[i] );
1099
1100 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1101
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001102 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001103 * Make sure M is odd:
1104 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001105 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001106 MPI_CHK( mpi_copy( &M, m ) );
1107 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001108
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001109 /*
1110 * Compute the fixed-pattern NAF and precompute odd multiples
1111 */
1112 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1113 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001114
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001115 /*
1116 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1117 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1118 *
1119 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1120 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1121 * == T[ - naf[i] - 1 ]
1122 */
1123 MPI_CHK( ecp_set_zero( &Q ) );
1124 i = naf_len - 1;
1125 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001126 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001127 if( naf[i] < 0 )
1128 {
1129 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1130 }
1131 else
1132 {
1133 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1134 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001135
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001136 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001137 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001138 i--;
1139
1140 for( j = 0; j < w; j++ )
1141 {
1142 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1143 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001144 }
1145
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001146 /*
1147 * Now get m * P from M * P.
1148 * Since we don't need T[] any more, we can recycle it:
1149 * we already have T[0] = P, now set T[1] = 2 * P.
1150 */
1151 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1152 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001153
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001154
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001155cleanup:
1156
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001157 mpi_free( &M );
1158 ecp_point_free( &Q );
1159 for( i = 0; i < pre_len; i++ )
1160 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001161
1162 return( ret );
1163}
1164
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001165/*
1166 * Generate a keypair (SEC1 3.2.1)
1167 */
1168int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1169 int (*f_rng)(void *, unsigned char *, size_t),
1170 void *p_rng )
1171{
1172 int count = 0;
1173 size_t n_size = (grp->nbits + 7) / 8;
1174
1175 /*
1176 * Generate d such that 1 <= n < N
1177 */
1178 do
1179 {
1180 mpi_fill_random( d, n_size, f_rng, p_rng );
1181
1182 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1183 mpi_shift_r( d, 1 );
1184
1185 if( count++ > 10 )
1186 return( POLARSSL_ERR_ECP_GENERIC );
1187 }
1188 while( mpi_cmp_int( d, 1 ) < 0 );
1189
1190 return( ecp_mul( grp, Q, d, &grp->G ) );
1191}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001192
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001193#if defined(POLARSSL_SELF_TEST)
1194
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001195/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001196 * Checkup routine
1197 */
1198int ecp_self_test( int verbose )
1199{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001200 int ret;
1201 size_t i;
1202 ecp_group grp;
1203 ecp_point R;
1204 mpi m;
1205 unsigned long add_c_prev, dbl_c_prev;
1206 char *exponents[] =
1207 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001208 "000000000000000000000000000000000000000000000000", /* zero */
1209 "000000000000000000000000000000000000000000000001", /* one */
1210 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1211 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001212 "400000000000000000000000000000000000000000000000",
1213 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1214 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001215 };
1216
1217 ecp_group_init( &grp );
1218 ecp_point_init( &R );
1219 mpi_init( &m );
1220
1221 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1222
1223 if( verbose != 0 )
1224 printf( " ECP test #1 (SPA resistance): " );
1225
1226 add_count = 0;
1227 dbl_count = 0;
1228 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1229 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1230
1231 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1232 {
1233 add_c_prev = add_count;
1234 dbl_c_prev = dbl_count;
1235 add_count = 0;
1236 dbl_count = 0;
1237
1238 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1239 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1240
1241 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1242 {
1243 if( verbose != 0 )
1244 printf( "failed (%zu)\n", i );
1245
1246 ret = 1;
1247 goto cleanup;
1248 }
1249 }
1250
1251 if( verbose != 0 )
1252 printf( "passed\n" );
1253
1254cleanup:
1255
1256 if( ret < 0 && verbose != 0 )
1257 printf( "Unexpected error, return code = %08X\n", ret );
1258
1259 ecp_group_free( &grp );
1260 ecp_point_free( &R );
1261 mpi_free( &m );
1262
1263 if( verbose != 0 )
1264 printf( "\n" );
1265
1266 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001267}
1268
1269#endif
1270
1271#endif