blob: 854da58c8a5fc99f7d1d01a050cb4b8f72bbc041 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
4 * Copyright (C) 2012, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010032 */
33
34#include "polarssl/config.h"
35
36#if defined(POLARSSL_ECP_C)
37
38#include "polarssl/ecp.h"
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010039#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010040#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010041
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010042#if defined(POLARSSL_SELF_TEST)
43/*
44 * Counts of point addition and doubling operations.
45 * Used to test resistance of point multiplication to SPA/timing attacks.
46 */
47unsigned long add_count, dbl_count;
48#endif
49
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010050/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010051 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010052 */
53void ecp_point_init( ecp_point *pt )
54{
55 if( pt == NULL )
56 return;
57
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010058 mpi_init( &pt->X );
59 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010060 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010061}
62
63/*
64 * Initialize (the components of) a group
65 */
66void ecp_group_init( ecp_group *grp )
67{
68 if( grp == NULL )
69 return;
70
71 mpi_init( &grp->P );
72 mpi_init( &grp->B );
73 ecp_point_init( &grp->G );
74 mpi_init( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010075
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010076 grp->pbits = 0;
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010077 grp->nbits = 0;
78
79 grp->modp = NULL;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010080}
81
82/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010083 * Unallocate (the components of) a point
84 */
85void ecp_point_free( ecp_point *pt )
86{
87 if( pt == NULL )
88 return;
89
90 mpi_free( &( pt->X ) );
91 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010092 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010093}
94
95/*
96 * Unallocate (the components of) a group
97 */
98void ecp_group_free( ecp_group *grp )
99{
100 if( grp == NULL )
101 return;
102
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100103 mpi_free( &grp->P );
104 mpi_free( &grp->B );
105 ecp_point_free( &grp->G );
106 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100107}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100108
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100109/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100110 * Set point to zero
111 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100112int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100113{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100114 int ret;
115
116 MPI_CHK( mpi_lset( &pt->X , 1 ) );
117 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
118 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
119
120cleanup:
121 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100122}
123
124/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100125 * Copy the contents of Q into P
126 */
127int ecp_copy( ecp_point *P, const ecp_point *Q )
128{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100129 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100130
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100131 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
132 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100133 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100134
135cleanup:
136 return( ret );
137}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100138
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100139/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100140 * Import a non-zero point from ASCII strings
141 */
142int ecp_point_read_string( ecp_point *P, int radix,
143 const char *x, const char *y )
144{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100145 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100146
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100147 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
148 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100149 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100150
151cleanup:
152 return( ret );
153}
154
155/*
156 * Import an ECP group from ASCII strings
157 */
158int ecp_group_read_string( ecp_group *grp, int radix,
159 const char *p, const char *b,
160 const char *gx, const char *gy, const char *n)
161{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100162 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100163
164 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
165 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
166 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
167 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
168
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100169 grp->pbits = mpi_msb( &grp->P );
170 grp->nbits = mpi_msb( &grp->N );
171
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100172cleanup:
173 return( ret );
174}
175
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100176/*
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100177 * Export a point into unsigned binary data, uncompressed format (SEC1 2.3.3)
178 */
179int ecp_write_binary( const ecp_group *grp, const ecp_point *P,
180 size_t *olen, unsigned char *buf, size_t buflen )
181{
182 int ret;
183 size_t plen;
184
185 /*
186 * Case P == 0
187 */
188 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
189 {
190 if( buflen < 1 )
191 return( POLARSSL_ERR_ECP_GENERIC );
192
193 buf[0] = 0x00;
194 *olen = 1;
195
196 return( 0 );
197 }
198
199 plen = mpi_size( &grp->P );
200 *olen = 2 * plen + 1;
201
202 if( buflen < *olen )
203 return( POLARSSL_ERR_ECP_GENERIC );
204
205 buf[0] = 0x04;
206 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
207 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
208
209cleanup:
210 return( ret );
211}
212
213/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100214 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
215 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100216 */
217static int ecp_modp( mpi *N, const ecp_group *grp )
218{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100219 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100220
221 if( grp->modp == NULL )
222 return( mpi_mod_mpi( N, N, &grp->P ) );
223
224 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
225 return( POLARSSL_ERR_ECP_GENERIC );
226
227 MPI_CHK( grp->modp( N ) );
228
229 while( mpi_cmp_int( N, 0 ) < 0 )
230 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
231
232 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
233 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
234
235cleanup:
236 return( ret );
237}
238
239/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100240 * 192 bits in terms of t_uint
241 */
242#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
243
244/*
245 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
246 * -1 means let this chunk be 0
247 * a positive value i means A_i.
248 */
249#define P192_CHUNKS 3
250#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
251#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
252
253const signed char p192_tbl[][P192_CHUNKS] = {
254 { -1, 3, 3 }, /* S1 */
255 { 4, 4, -1 }, /* S2 */
256 { 5, 5, 5 }, /* S3 */
257};
258
259/*
260 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
261 */
262static int ecp_mod_p192( mpi *N )
263{
264 int ret;
265 unsigned char i, j, offset;
266 signed char chunk;
267 mpi tmp, acc;
268 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
269
270 tmp.s = 1;
271 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
272 tmp.p = tmp_p;
273
274 acc.s = 1;
275 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
276 acc.p = acc_p;
277
278 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
279
280 /*
281 * acc = T
282 */
283 memset( acc_p, 0, sizeof( acc_p ) );
284 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
285
286 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
287 {
288 /*
289 * tmp = S_i
290 */
291 memset( tmp_p, 0, sizeof( tmp_p ) );
292 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
293 {
294 chunk = p192_tbl[i][j];
295 if( chunk >= 0 )
296 memcpy( tmp_p + offset * P192_CHUNK_INT,
297 N->p + chunk * P192_CHUNK_INT,
298 P192_CHUNK_CHAR );
299 }
300
301 /*
302 * acc += tmp
303 */
304 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
305 }
306
307 MPI_CHK( mpi_copy( N, &acc ) );
308
309cleanup:
310 return( ret );
311}
312
313/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100314 * Size of p521 in terms of t_uint
315 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100316#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100317
318/*
319 * Bits to keep in the most significant t_uint
320 */
321#if defined(POLARSS_HAVE_INT8)
322#define P521_MASK 0x01
323#else
324#define P521_MASK 0x01FF
325#endif
326
327/*
328 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100329 */
330static int ecp_mod_p521( mpi *N )
331{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100332 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100333 t_uint Mp[P521_SIZE_INT];
334 mpi M;
335
336 if( N->n < P521_SIZE_INT )
337 return( 0 );
338
339 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
340 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
341 Mp[P521_SIZE_INT - 1] &= P521_MASK;
342
343 M.s = 1;
344 M.n = P521_SIZE_INT;
345 M.p = Mp;
346
347 MPI_CHK( mpi_shift_r( N, 521 ) );
348
349 MPI_CHK( mpi_add_abs( N, N, &M ) );
350
351cleanup:
352 return( ret );
353}
354
355/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100356 * Domain parameters for secp192r1
357 */
358#define SECP192R1_P \
359 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
360#define SECP192R1_B \
361 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
362#define SECP192R1_GX \
363 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
364#define SECP192R1_GY \
365 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
366#define SECP192R1_N \
367 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
368
369/*
370 * Domain parameters for secp224r1
371 */
372#define SECP224R1_P \
373 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
374#define SECP224R1_B \
375 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
376#define SECP224R1_GX \
377 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
378#define SECP224R1_GY \
379 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
380#define SECP224R1_N \
381 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
382
383/*
384 * Domain parameters for secp256r1
385 */
386#define SECP256R1_P \
387 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
388#define SECP256R1_B \
389 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
390#define SECP256R1_GX \
391 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
392#define SECP256R1_GY \
393 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
394#define SECP256R1_N \
395 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
396
397/*
398 * Domain parameters for secp384r1
399 */
400#define SECP384R1_P \
401 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
402 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
403#define SECP384R1_B \
404 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
405 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
406#define SECP384R1_GX \
407 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
408 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
409#define SECP384R1_GY \
410 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
411 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
412#define SECP384R1_N \
413 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
414 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
415
416/*
417 * Domain parameters for secp521r1
418 */
419#define SECP521R1_P \
420 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
421 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
422 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
423#define SECP521R1_B \
424 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
425 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
426 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
427#define SECP521R1_GX \
428 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
429 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
430 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
431#define SECP521R1_GY \
432 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
433 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
434 "3FAD0761353C7086A272C24088BE94769FD16650"
435#define SECP521R1_N \
436 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
437 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
438 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
439
440/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100441 * Set a group using well-known domain parameters
442 */
443int ecp_use_known_dp( ecp_group *grp, size_t index )
444{
445 switch( index )
446 {
447 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100448 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100449 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100450 SECP192R1_P, SECP192R1_B,
451 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
452
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100453 case POLARSSL_ECP_DP_SECP224R1:
454 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100455 SECP224R1_P, SECP224R1_B,
456 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
457
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100458 case POLARSSL_ECP_DP_SECP256R1:
459 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100460 SECP256R1_P, SECP256R1_B,
461 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
462
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100463 case POLARSSL_ECP_DP_SECP384R1:
464 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100465 SECP384R1_P, SECP384R1_B,
466 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
467
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100468 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100469 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100470 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100471 SECP521R1_P, SECP521R1_B,
472 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100473 }
474
475 return( POLARSSL_ERR_ECP_GENERIC );
476}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100477
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100478/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100479 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100480 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100481 * In order to guarantee that, we need to ensure that operands of
482 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100483 * bring the result back to this range.
484 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100485 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100486 */
487
488/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100489 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
490 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100491#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100492
493/*
494 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
495 */
496#define MOD_SUB( N ) \
497 while( mpi_cmp_int( &N, 0 ) < 0 ) \
498 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
499
500/*
501 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
502 */
503#define MOD_ADD( N ) \
504 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
505 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
506
507/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100508 * Check that a point is valid as a public key (SEC1 3.2.3.1)
509 */
510int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
511{
512 int ret;
513 mpi YY, RHS;
514
515 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
516 return( POLARSSL_ERR_ECP_GENERIC );
517
518 /*
519 * pt coordinates must be normalized for our checks
520 */
521 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
522 return( POLARSSL_ERR_ECP_GENERIC );
523
524 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
525 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
526 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
527 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
528 return( POLARSSL_ERR_ECP_GENERIC );
529
530 mpi_init( &YY ); mpi_init( &RHS );
531
532 /*
533 * YY = Y^2
534 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
535 */
536 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
537 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
538 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
539 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
540 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
541
542 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
543 ret = POLARSSL_ERR_ECP_GENERIC;
544
545cleanup:
546
547 mpi_free( &YY ); mpi_free( &RHS );
548
549 return( ret );
550}
551
552/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100553 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100554 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100555static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100556{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100557 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100558 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100559
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100560 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100561 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100562
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100563 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100564
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100565 /*
566 * X = X / Z^2 mod p
567 */
568 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
569 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
570 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100571
572 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100573 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100574 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100575 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
576 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100577
578 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100579 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100580 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100581 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100582
583cleanup:
584
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100585 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100586
587 return( ret );
588}
589
590/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100591 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100592 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100593 * (See for example Cohen's "A Course in Computational Algebraic Number
594 * Theory", Algorithm 10.3.4.)
595 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100596 * Warning: fails if one of the points is zero!
597 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100598 */
599static int ecp_normalize_many( const ecp_group *grp,
600 ecp_point T[], size_t t_len )
601{
602 int ret;
603 size_t i;
604 mpi *c, u, Zi, ZZi;
605
606 if( t_len < 2 )
607 return( ecp_normalize( grp, T ) );
608
609 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
610 return( POLARSSL_ERR_ECP_GENERIC );
611
612 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
613 for( i = 0; i < t_len; i++ )
614 mpi_init( &c[i] );
615
616 /*
617 * c[i] = Z_0 * ... * Z_i
618 */
619 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
620 for( i = 1; i < t_len; i++ )
621 {
622 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
623 MOD_MUL( c[i] );
624 }
625
626 /*
627 * u = 1 / (Z_0 * ... * Z_n) mod P
628 */
629 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
630
631 for( i = t_len - 1; ; i-- )
632 {
633 /*
634 * Zi = 1 / Z_i mod p
635 * u = 1 / (Z_0 * ... * Z_i) mod P
636 */
637 if( i == 0 ) {
638 MPI_CHK( mpi_copy( &Zi, &u ) );
639 }
640 else
641 {
642 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
643 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
644 }
645
646 /*
647 * proceed as in normalize()
648 */
649 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
650 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
651 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
652 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
653 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
654
655 if( i == 0 )
656 break;
657 }
658
659cleanup:
660
661 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
662 for( i = 0; i < t_len; i++ )
663 mpi_free( &c[i] );
664 free( c );
665
666 return( ret );
667}
668
669
670/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100671 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
672 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100673static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
674 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100675{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100676 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100677 mpi T1, T2, T3, X, Y, Z;
678
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100679#if defined(POLARSSL_SELF_TEST)
680 dbl_count++;
681#endif
682
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100683 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100684 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100685
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100686 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
687 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
688
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100689 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
690 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
691 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
692 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
693 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
694 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
695 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
696 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
697 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
698 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100699
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100700 /*
701 * For Y = Y / 2 mod p, we must make sure that Y is even before
702 * using right-shift. No need to reduce mod p afterwards.
703 */
704 if( mpi_get_bit( &Y, 0 ) == 1 )
705 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
706 MPI_CHK( mpi_shift_r( &Y, 1 ) );
707
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100708 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
709 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
710 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
711 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
712 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
713 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100714
715 MPI_CHK( mpi_copy( &R->X, &X ) );
716 MPI_CHK( mpi_copy( &R->Y, &Y ) );
717 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100718
719cleanup:
720
721 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
722 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
723
724 return( ret );
725}
726
727/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100728 * Addition or subtraction: R = P + Q or R = P + Q,
729 * mixed affine-Jacobian coordinates (GECC 3.22)
730 *
731 * The coordinates of Q must be normalized (= affine),
732 * but those of P don't need to. R is not normalized.
733 *
734 * If sign >= 0, perform addition, otherwise perform subtraction,
735 * taking advantage of the fact that, for Q != 0, we have
736 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100737 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100738static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100739 const ecp_point *P, const ecp_point *Q,
740 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100741{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100742 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100743 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100744
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100745#if defined(POLARSSL_SELF_TEST)
746 add_count++;
747#endif
748
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100749 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100750 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100751 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100752 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100753 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
754 return( ecp_copy( R, P ) );
755
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100756 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
757 {
758 ret = ecp_copy( R, Q );
759
760 /*
761 * -R.Y mod P = P - R.Y unless R.Y == 0
762 */
763 if( ret == 0 && sign < 0)
764 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
765 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
766
767 return( ret );
768 }
769
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100770 /*
771 * Make sure Q coordinates are normalized
772 */
773 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
774 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100775
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100776 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
777 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100778
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100779 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
780 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
781 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
782 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100783
784 /*
785 * For subtraction, -Q.Y should have been used instead of Q.Y,
786 * so we replace T2 by -T2, which is P - T2 mod P
787 */
788 if( sign < 0 )
789 {
790 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
791 MOD_SUB( T2 );
792 }
793
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100794 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
795 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100796
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100797 if( mpi_cmp_int( &T1, 0 ) == 0 )
798 {
799 if( mpi_cmp_int( &T2, 0 ) == 0 )
800 {
801 ret = ecp_double_jac( grp, R, P );
802 goto cleanup;
803 }
804 else
805 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100806 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100807 goto cleanup;
808 }
809 }
810
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100811 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
812 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
813 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
814 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
815 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
816 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
817 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
818 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
819 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
820 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
821 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
822 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100823
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100824 MPI_CHK( mpi_copy( &R->X, &X ) );
825 MPI_CHK( mpi_copy( &R->Y, &Y ) );
826 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100827
828cleanup:
829
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100830 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
831 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100832
833 return( ret );
834}
835
836/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100837 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100838 */
839int ecp_add( const ecp_group *grp, ecp_point *R,
840 const ecp_point *P, const ecp_point *Q )
841{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100842 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100843
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100844 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
845 MPI_CHK( ecp_normalize( grp, R ) );
846
847cleanup:
848 return( ret );
849}
850
851/*
852 * Subtraction: R = P - Q, result's coordinates normalized
853 */
854int ecp_sub( const ecp_group *grp, ecp_point *R,
855 const ecp_point *P, const ecp_point *Q )
856{
857 int ret;
858
859 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100860 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100861
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100862cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100863 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100864}
865
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100866/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100867 * Compute a modified width-w non-adjacent form (NAF) of a number,
868 * with a fixed pattern for resistance to SPA/timing attacks,
869 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
870 * (The resulting multiplication algorithm can also been seen as a
871 * modification of 2^w-ary multiplication, with signed coefficients,
872 * all of them odd.)
873 *
874 * Input:
875 * m must be an odd positive mpi less than w * k bits long
876 * x must be an array of k elements
877 * w must be less than a certain maximum (currently 8)
878 *
879 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
880 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
881 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
882 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
883 *
884 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
885 * p. 335 of the cited reference, here we return only u, not d_w since
886 * it is known that the other d_w[j] will be 0. Moreover, the returned
887 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
888 * that u_i is odd. Also, since we always select a positive value for d
889 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
890 * does. Finally, there is an off-by-one error in the reference: the
891 * last index should be k-1, not k.
892 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100893static int ecp_w_naf_fixed( signed char x[], size_t k,
894 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100895{
896 int ret;
897 unsigned int i, u, mask, carry;
898 mpi M;
899
900 mpi_init( &M );
901
902 MPI_CHK( mpi_copy( &M, m ) );
903 mask = ( 1 << w ) - 1;
904 carry = 1 << ( w - 1 );
905
906 for( i = 0; i < k; i++ )
907 {
908 u = M.p[0] & mask;
909
910 if( ( u & 1 ) == 0 && i > 0 )
911 x[i - 1] -= carry;
912
913 x[i] = u >> 1;
914 mpi_shift_r( &M, w );
915 }
916
917 /*
918 * We should have consumed all the bits now
919 */
920 if( mpi_cmp_int( &M, 0 ) != 0 )
921 ret = POLARSSL_ERR_ECP_GENERIC;
922
923cleanup:
924
925 mpi_free( &M );
926
927 return( ret );
928}
929
930/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100931 * Precompute odd multiples of P up to (2 * t_len - 1) P.
932 * The table is filled with T[i] = (2 * i + 1) P.
933 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100934static int ecp_precompute( const ecp_group *grp,
935 ecp_point T[], size_t t_len,
936 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100937{
938 int ret;
939 size_t i;
940 ecp_point PP;
941
942 ecp_point_init( &PP );
943
944 MPI_CHK( ecp_add( grp, &PP, P, P ) );
945
946 MPI_CHK( ecp_copy( &T[0], P ) );
947
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100948 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100949 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
950
951 /*
952 * T[0] = P already has normalized coordinates
953 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100954 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +0100955
956cleanup:
957
958 ecp_point_free( &PP );
959
960 return( ret );
961}
962
963/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100964 * Maximum length of the precomputed table
965 */
966#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
967
968/*
969 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
970 * (that is: grp->nbits / w + 1)
971 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
972 */
973#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
974
975/*
976 * Integer multiplication: R = m * P
977 *
978 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
979 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
980 *
981 * This function executes a fixed number of operations for
982 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100983 */
984int ecp_mul( const ecp_group *grp, ecp_point *R,
985 const mpi *m, const ecp_point *P )
986{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100987 int ret;
988 unsigned char w, m_is_odd;
989 size_t pre_len, naf_len, i, j;
990 signed char naf[ MAX_NAF_LEN ];
991 ecp_point Q, T[ MAX_PRE_LEN ];
992 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100993
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100994 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +0100995 return( POLARSSL_ERR_ECP_GENERIC );
996
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100997 w = grp->nbits >= 521 ? 6 :
998 grp->nbits >= 224 ? 5 :
999 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001000
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001001 /*
1002 * Make sure w is within the limits.
1003 * The last test ensures that none of the precomputed points is zero,
1004 * which wouldn't be handled correctly by ecp_normalize_many().
1005 * It is only useful for small curves, as used in the test suite.
1006 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001007 if( w > POLARSSL_ECP_WINDOW_SIZE )
1008 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001009 if( w < 2 || w >= grp->nbits )
1010 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001011
1012 pre_len = 1 << ( w - 1 );
1013 naf_len = grp->nbits / w + 1;
1014
1015 mpi_init( &M );
1016 ecp_point_init( &Q );
1017 for( i = 0; i < pre_len; i++ )
1018 ecp_point_init( &T[i] );
1019
1020 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1021
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001022 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001023 * Make sure M is odd:
1024 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001025 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001026 MPI_CHK( mpi_copy( &M, m ) );
1027 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001028
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001029 /*
1030 * Compute the fixed-pattern NAF and precompute odd multiples
1031 */
1032 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1033 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001034
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001035 /*
1036 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1037 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1038 *
1039 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1040 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1041 * == T[ - naf[i] - 1 ]
1042 */
1043 MPI_CHK( ecp_set_zero( &Q ) );
1044 i = naf_len - 1;
1045 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001046 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001047 if( naf[i] < 0 )
1048 {
1049 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1050 }
1051 else
1052 {
1053 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1054 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001055
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001056 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001057 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001058 i--;
1059
1060 for( j = 0; j < w; j++ )
1061 {
1062 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1063 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001064 }
1065
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001066 /*
1067 * Now get m * P from M * P.
1068 * Since we don't need T[] any more, we can recycle it:
1069 * we already have T[0] = P, now set T[1] = 2 * P.
1070 */
1071 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1072 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001073
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001074
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001075cleanup:
1076
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001077 mpi_free( &M );
1078 ecp_point_free( &Q );
1079 for( i = 0; i < pre_len; i++ )
1080 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001081
1082 return( ret );
1083}
1084
1085
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001086#if defined(POLARSSL_SELF_TEST)
1087
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001088/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001089 * Checkup routine
1090 */
1091int ecp_self_test( int verbose )
1092{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001093 int ret;
1094 size_t i;
1095 ecp_group grp;
1096 ecp_point R;
1097 mpi m;
1098 unsigned long add_c_prev, dbl_c_prev;
1099 char *exponents[] =
1100 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001101 "000000000000000000000000000000000000000000000000", /* zero */
1102 "000000000000000000000000000000000000000000000001", /* one */
1103 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1104 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001105 "400000000000000000000000000000000000000000000000",
1106 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1107 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001108 };
1109
1110 ecp_group_init( &grp );
1111 ecp_point_init( &R );
1112 mpi_init( &m );
1113
1114 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1115
1116 if( verbose != 0 )
1117 printf( " ECP test #1 (SPA resistance): " );
1118
1119 add_count = 0;
1120 dbl_count = 0;
1121 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1122 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1123
1124 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1125 {
1126 add_c_prev = add_count;
1127 dbl_c_prev = dbl_count;
1128 add_count = 0;
1129 dbl_count = 0;
1130
1131 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1132 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1133
1134 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1135 {
1136 if( verbose != 0 )
1137 printf( "failed (%zu)\n", i );
1138
1139 ret = 1;
1140 goto cleanup;
1141 }
1142 }
1143
1144 if( verbose != 0 )
1145 printf( "passed\n" );
1146
1147cleanup:
1148
1149 if( ret < 0 && verbose != 0 )
1150 printf( "Unexpected error, return code = %08X\n", ret );
1151
1152 ecp_group_free( &grp );
1153 ecp_point_free( &R );
1154 mpi_free( &m );
1155
1156 if( verbose != 0 )
1157 printf( "\n" );
1158
1159 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001160}
1161
1162#endif
1163
1164#endif