blob: 68f20f7c7733bb946d0e6a3321ef7ac82bca811c [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
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +010072 grp->id = 0;
73
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010074 mpi_init( &grp->P );
75 mpi_init( &grp->B );
76 ecp_point_init( &grp->G );
77 mpi_init( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010078
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010079 grp->pbits = 0;
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010080 grp->nbits = 0;
81
82 grp->modp = NULL;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010083}
84
85/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010086 * Unallocate (the components of) a point
87 */
88void ecp_point_free( ecp_point *pt )
89{
90 if( pt == NULL )
91 return;
92
93 mpi_free( &( pt->X ) );
94 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010095 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010096}
97
98/*
99 * Unallocate (the components of) a group
100 */
101void ecp_group_free( ecp_group *grp )
102{
103 if( grp == NULL )
104 return;
105
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100106 mpi_free( &grp->P );
107 mpi_free( &grp->B );
108 ecp_point_free( &grp->G );
109 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100110}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100111
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100112/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100113 * Set point to zero
114 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100115int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100116{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100117 int ret;
118
119 MPI_CHK( mpi_lset( &pt->X , 1 ) );
120 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
121 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
122
123cleanup:
124 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100125}
126
127/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100128 * Tell if a point is zero
129 */
130int ecp_is_zero( ecp_point *pt )
131{
132 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
133}
134
135/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100136 * Copy the contents of Q into P
137 */
138int ecp_copy( ecp_point *P, const ecp_point *Q )
139{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100140 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100141
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100142 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
143 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100144 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100145
146cleanup:
147 return( ret );
148}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100149
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100150/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100151 * Import a non-zero point from ASCII strings
152 */
153int ecp_point_read_string( ecp_point *P, int radix,
154 const char *x, const char *y )
155{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100156 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100157
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100158 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
159 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100160 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100161
162cleanup:
163 return( ret );
164}
165
166/*
167 * Import an ECP group from ASCII strings
168 */
169int ecp_group_read_string( ecp_group *grp, int radix,
170 const char *p, const char *b,
171 const char *gx, const char *gy, const char *n)
172{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100173 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100174
175 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
176 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
177 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
178 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
179
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100180 grp->pbits = mpi_msb( &grp->P );
181 grp->nbits = mpi_msb( &grp->N );
182
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100183cleanup:
184 return( ret );
185}
186
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100187/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100188 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100189 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100190int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100191 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100192 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100193{
194 int ret;
195 size_t plen;
196
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100197 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
198 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100199 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100200
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100201 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100202 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100203 */
204 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
205 {
206 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100207 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100208
209 buf[0] = 0x00;
210 *olen = 1;
211
212 return( 0 );
213 }
214
215 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100216
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100217 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
218 {
219 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100220
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100221 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100222 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100223
224 buf[0] = 0x04;
225 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
226 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
227 }
228 else if( format == POLARSSL_ECP_PF_COMPRESSED )
229 {
230 *olen = plen + 1;
231
232 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100233 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100234
235 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
236 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
237 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100238
239cleanup:
240 return( ret );
241}
242
243/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100244 * Import a point from unsigned binary data (SEC1 2.3.4)
245 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100246int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
247 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100248 int ret;
249 size_t plen;
250
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100251 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100252 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100253
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100254 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100255
256 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100257 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100258
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100259 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
260 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
261 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100262
263cleanup:
264 return( ret );
265}
266
267/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100268 * Import a point from a TLS ECPoint record (RFC 4492)
269 * struct {
270 * opaque point <1..2^8-1>;
271 * } ECPoint;
272 */
273int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
274 const unsigned char *buf, size_t buf_len )
275{
276 unsigned char data_len;
277
278 /*
279 * We must have at least two bytes (1 for length, at least of for data)
280 */
281 if( buf_len < 2 )
282 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
283
284 data_len = *buf++;
285 if( data_len < 1 || data_len > buf_len - 1 )
286 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
287
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100288 return ecp_point_read_binary( grp, pt, buf, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100289}
290
291/*
292 * Export a point as a TLS ECPoint record (RFC 4492)
293 * struct {
294 * opaque point <1..2^8-1>;
295 * } ECPoint;
296 */
297int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100298 int format, size_t *olen,
299 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100300{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100301 int ret;
302
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100303 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100304 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100305 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100306 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100307 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
308
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100309 if( ( ret = ecp_point_write_binary( grp, pt, format,
310 olen, buf + 1, blen - 1) ) != 0 )
311 return( ret );
312
313 /*
314 * write length to the first byte and update total length
315 */
316 buf[0] = *olen;
317 ++*olen;
318
319 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100320}
321
322/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100323 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
324 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100325 */
326static int ecp_modp( mpi *N, const ecp_group *grp )
327{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100328 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100329
330 if( grp->modp == NULL )
331 return( mpi_mod_mpi( N, N, &grp->P ) );
332
333 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
334 return( POLARSSL_ERR_ECP_GENERIC );
335
336 MPI_CHK( grp->modp( N ) );
337
338 while( mpi_cmp_int( N, 0 ) < 0 )
339 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
340
341 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
342 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
343
344cleanup:
345 return( ret );
346}
347
348/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100349 * 192 bits in terms of t_uint
350 */
351#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
352
353/*
354 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
355 * -1 means let this chunk be 0
356 * a positive value i means A_i.
357 */
358#define P192_CHUNKS 3
359#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
360#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
361
362const signed char p192_tbl[][P192_CHUNKS] = {
363 { -1, 3, 3 }, /* S1 */
364 { 4, 4, -1 }, /* S2 */
365 { 5, 5, 5 }, /* S3 */
366};
367
368/*
369 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
370 */
371static int ecp_mod_p192( mpi *N )
372{
373 int ret;
374 unsigned char i, j, offset;
375 signed char chunk;
376 mpi tmp, acc;
377 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
378
379 tmp.s = 1;
380 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
381 tmp.p = tmp_p;
382
383 acc.s = 1;
384 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
385 acc.p = acc_p;
386
387 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
388
389 /*
390 * acc = T
391 */
392 memset( acc_p, 0, sizeof( acc_p ) );
393 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
394
395 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
396 {
397 /*
398 * tmp = S_i
399 */
400 memset( tmp_p, 0, sizeof( tmp_p ) );
401 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
402 {
403 chunk = p192_tbl[i][j];
404 if( chunk >= 0 )
405 memcpy( tmp_p + offset * P192_CHUNK_INT,
406 N->p + chunk * P192_CHUNK_INT,
407 P192_CHUNK_CHAR );
408 }
409
410 /*
411 * acc += tmp
412 */
413 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
414 }
415
416 MPI_CHK( mpi_copy( N, &acc ) );
417
418cleanup:
419 return( ret );
420}
421
422/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100423 * Size of p521 in terms of t_uint
424 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100425#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100426
427/*
428 * Bits to keep in the most significant t_uint
429 */
430#if defined(POLARSS_HAVE_INT8)
431#define P521_MASK 0x01
432#else
433#define P521_MASK 0x01FF
434#endif
435
436/*
437 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100438 */
439static int ecp_mod_p521( mpi *N )
440{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100441 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100442 t_uint Mp[P521_SIZE_INT];
443 mpi M;
444
445 if( N->n < P521_SIZE_INT )
446 return( 0 );
447
448 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
449 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
450 Mp[P521_SIZE_INT - 1] &= P521_MASK;
451
452 M.s = 1;
453 M.n = P521_SIZE_INT;
454 M.p = Mp;
455
456 MPI_CHK( mpi_shift_r( N, 521 ) );
457
458 MPI_CHK( mpi_add_abs( N, N, &M ) );
459
460cleanup:
461 return( ret );
462}
463
464/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100465 * Domain parameters for secp192r1
466 */
467#define SECP192R1_P \
468 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
469#define SECP192R1_B \
470 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
471#define SECP192R1_GX \
472 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
473#define SECP192R1_GY \
474 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
475#define SECP192R1_N \
476 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
477
478/*
479 * Domain parameters for secp224r1
480 */
481#define SECP224R1_P \
482 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
483#define SECP224R1_B \
484 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
485#define SECP224R1_GX \
486 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
487#define SECP224R1_GY \
488 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
489#define SECP224R1_N \
490 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
491
492/*
493 * Domain parameters for secp256r1
494 */
495#define SECP256R1_P \
496 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
497#define SECP256R1_B \
498 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
499#define SECP256R1_GX \
500 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
501#define SECP256R1_GY \
502 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
503#define SECP256R1_N \
504 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
505
506/*
507 * Domain parameters for secp384r1
508 */
509#define SECP384R1_P \
510 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
511 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
512#define SECP384R1_B \
513 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
514 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
515#define SECP384R1_GX \
516 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
517 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
518#define SECP384R1_GY \
519 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
520 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
521#define SECP384R1_N \
522 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
523 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
524
525/*
526 * Domain parameters for secp521r1
527 */
528#define SECP521R1_P \
529 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
530 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
531 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
532#define SECP521R1_B \
533 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
534 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
535 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
536#define SECP521R1_GX \
537 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
538 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
539 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
540#define SECP521R1_GY \
541 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
542 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
543 "3FAD0761353C7086A272C24088BE94769FD16650"
544#define SECP521R1_N \
545 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
546 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
547 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
548
549/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100550 * Set a group using well-known domain parameters
551 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100552int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100553{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100554 grp->id = id;
555
556 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100557 {
558 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100559 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100560 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100561 SECP192R1_P, SECP192R1_B,
562 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
563
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100564 case POLARSSL_ECP_DP_SECP224R1:
565 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100566 SECP224R1_P, SECP224R1_B,
567 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
568
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100569 case POLARSSL_ECP_DP_SECP256R1:
570 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100571 SECP256R1_P, SECP256R1_B,
572 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
573
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100574 case POLARSSL_ECP_DP_SECP384R1:
575 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100576 SECP384R1_P, SECP384R1_B,
577 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
578
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100579 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100580 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100581 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100582 SECP521R1_P, SECP521R1_B,
583 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100584 }
585
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100586 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
587}
588
589/*
590 * Set a group from an ECParameters record (RFC 4492)
591 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100592int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100593{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100594 ecp_group_id id;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100595
596 /*
597 * We expect at least three bytes (see below)
598 */
599 if( len < 3 )
600 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
601
602 /*
603 * First byte is curve_type; only named_curve is handled
604 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100605 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100606 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
607
608 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100609 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100610 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100611 id = *(*buf)++;
612 id <<= 8;
613 id |= *(*buf)++;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100614 return ecp_use_known_dp( grp, id );
615}
616
617/*
618 * Write the ECParameters record corresponding to a group (RFC 4492)
619 */
620int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
621 unsigned char *buf, size_t blen )
622{
623 /*
624 * We are going to write 3 bytes (see below)
625 */
626 *olen = 3;
627 if( blen < *olen )
628 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
629
630 /*
631 * First byte is curve_type, always named_curve
632 */
633 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
634
635 /*
636 * Next two bytes are the namedcurve value
637 */
638 buf[0] = grp->id >> 8;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100639 buf[1] = grp->id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100640
641 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100642}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100643
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100644/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100645 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100646 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100647 * In order to guarantee that, we need to ensure that operands of
648 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100649 * bring the result back to this range.
650 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100651 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100652 */
653
654/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100655 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
656 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100657#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100658
659/*
660 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
661 */
662#define MOD_SUB( N ) \
663 while( mpi_cmp_int( &N, 0 ) < 0 ) \
664 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
665
666/*
667 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
668 */
669#define MOD_ADD( N ) \
670 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
671 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
672
673/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100674 * Check that a point is valid as a public key (SEC1 3.2.3.1)
675 */
676int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
677{
678 int ret;
679 mpi YY, RHS;
680
681 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
682 return( POLARSSL_ERR_ECP_GENERIC );
683
684 /*
685 * pt coordinates must be normalized for our checks
686 */
687 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
688 return( POLARSSL_ERR_ECP_GENERIC );
689
690 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
691 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
692 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
693 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
694 return( POLARSSL_ERR_ECP_GENERIC );
695
696 mpi_init( &YY ); mpi_init( &RHS );
697
698 /*
699 * YY = Y^2
700 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
701 */
702 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
703 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
704 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
705 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
706 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
707
708 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
709 ret = POLARSSL_ERR_ECP_GENERIC;
710
711cleanup:
712
713 mpi_free( &YY ); mpi_free( &RHS );
714
715 return( ret );
716}
717
718/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100719 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100720 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100721static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100722{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100723 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100724 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100725
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100726 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100727 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100728
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100729 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100730
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100731 /*
732 * X = X / Z^2 mod p
733 */
734 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
735 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
736 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100737
738 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100739 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100740 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100741 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
742 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100743
744 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100745 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100746 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100747 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100748
749cleanup:
750
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100751 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100752
753 return( ret );
754}
755
756/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100757 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100758 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100759 * (See for example Cohen's "A Course in Computational Algebraic Number
760 * Theory", Algorithm 10.3.4.)
761 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100762 * Warning: fails if one of the points is zero!
763 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100764 */
765static int ecp_normalize_many( const ecp_group *grp,
766 ecp_point T[], size_t t_len )
767{
768 int ret;
769 size_t i;
770 mpi *c, u, Zi, ZZi;
771
772 if( t_len < 2 )
773 return( ecp_normalize( grp, T ) );
774
775 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
776 return( POLARSSL_ERR_ECP_GENERIC );
777
778 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
779 for( i = 0; i < t_len; i++ )
780 mpi_init( &c[i] );
781
782 /*
783 * c[i] = Z_0 * ... * Z_i
784 */
785 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
786 for( i = 1; i < t_len; i++ )
787 {
788 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
789 MOD_MUL( c[i] );
790 }
791
792 /*
793 * u = 1 / (Z_0 * ... * Z_n) mod P
794 */
795 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
796
797 for( i = t_len - 1; ; i-- )
798 {
799 /*
800 * Zi = 1 / Z_i mod p
801 * u = 1 / (Z_0 * ... * Z_i) mod P
802 */
803 if( i == 0 ) {
804 MPI_CHK( mpi_copy( &Zi, &u ) );
805 }
806 else
807 {
808 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
809 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
810 }
811
812 /*
813 * proceed as in normalize()
814 */
815 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
816 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
817 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
818 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
819 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
820
821 if( i == 0 )
822 break;
823 }
824
825cleanup:
826
827 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
828 for( i = 0; i < t_len; i++ )
829 mpi_free( &c[i] );
830 free( c );
831
832 return( ret );
833}
834
835
836/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100837 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
838 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100839static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
840 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100841{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100842 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100843 mpi T1, T2, T3, X, Y, Z;
844
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100845#if defined(POLARSSL_SELF_TEST)
846 dbl_count++;
847#endif
848
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100849 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100850 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100851
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100852 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
853 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
854
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100855 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
856 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
857 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
858 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
859 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
860 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
861 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
862 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
863 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
864 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100865
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100866 /*
867 * For Y = Y / 2 mod p, we must make sure that Y is even before
868 * using right-shift. No need to reduce mod p afterwards.
869 */
870 if( mpi_get_bit( &Y, 0 ) == 1 )
871 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
872 MPI_CHK( mpi_shift_r( &Y, 1 ) );
873
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100874 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
875 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
876 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
877 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
878 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
879 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100880
881 MPI_CHK( mpi_copy( &R->X, &X ) );
882 MPI_CHK( mpi_copy( &R->Y, &Y ) );
883 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100884
885cleanup:
886
887 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
888 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
889
890 return( ret );
891}
892
893/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100894 * Addition or subtraction: R = P + Q or R = P + Q,
895 * mixed affine-Jacobian coordinates (GECC 3.22)
896 *
897 * The coordinates of Q must be normalized (= affine),
898 * but those of P don't need to. R is not normalized.
899 *
900 * If sign >= 0, perform addition, otherwise perform subtraction,
901 * taking advantage of the fact that, for Q != 0, we have
902 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100903 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100904static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100905 const ecp_point *P, const ecp_point *Q,
906 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100907{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100908 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100909 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100910
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100911#if defined(POLARSSL_SELF_TEST)
912 add_count++;
913#endif
914
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100915 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100916 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100917 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100918 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100919 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
920 return( ecp_copy( R, P ) );
921
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100922 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
923 {
924 ret = ecp_copy( R, Q );
925
926 /*
927 * -R.Y mod P = P - R.Y unless R.Y == 0
928 */
929 if( ret == 0 && sign < 0)
930 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
931 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
932
933 return( ret );
934 }
935
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100936 /*
937 * Make sure Q coordinates are normalized
938 */
939 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
940 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100941
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100942 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
943 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100944
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100945 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
946 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
947 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
948 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100949
950 /*
951 * For subtraction, -Q.Y should have been used instead of Q.Y,
952 * so we replace T2 by -T2, which is P - T2 mod P
953 */
954 if( sign < 0 )
955 {
956 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
957 MOD_SUB( T2 );
958 }
959
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100960 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
961 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100962
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100963 if( mpi_cmp_int( &T1, 0 ) == 0 )
964 {
965 if( mpi_cmp_int( &T2, 0 ) == 0 )
966 {
967 ret = ecp_double_jac( grp, R, P );
968 goto cleanup;
969 }
970 else
971 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100972 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100973 goto cleanup;
974 }
975 }
976
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100977 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
978 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
979 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
980 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
981 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
982 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
983 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
984 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
985 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
986 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
987 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
988 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100989
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100990 MPI_CHK( mpi_copy( &R->X, &X ) );
991 MPI_CHK( mpi_copy( &R->Y, &Y ) );
992 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100993
994cleanup:
995
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100996 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
997 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100998
999 return( ret );
1000}
1001
1002/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001003 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001004 */
1005int ecp_add( const ecp_group *grp, ecp_point *R,
1006 const ecp_point *P, const ecp_point *Q )
1007{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001008 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001009
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001010 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1011 MPI_CHK( ecp_normalize( grp, R ) );
1012
1013cleanup:
1014 return( ret );
1015}
1016
1017/*
1018 * Subtraction: R = P - Q, result's coordinates normalized
1019 */
1020int ecp_sub( const ecp_group *grp, ecp_point *R,
1021 const ecp_point *P, const ecp_point *Q )
1022{
1023 int ret;
1024
1025 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001026 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001027
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001028cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001029 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001030}
1031
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001032/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001033 * Compute a modified width-w non-adjacent form (NAF) of a number,
1034 * with a fixed pattern for resistance to SPA/timing attacks,
1035 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1036 * (The resulting multiplication algorithm can also been seen as a
1037 * modification of 2^w-ary multiplication, with signed coefficients,
1038 * all of them odd.)
1039 *
1040 * Input:
1041 * m must be an odd positive mpi less than w * k bits long
1042 * x must be an array of k elements
1043 * w must be less than a certain maximum (currently 8)
1044 *
1045 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1046 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1047 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1048 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1049 *
1050 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1051 * p. 335 of the cited reference, here we return only u, not d_w since
1052 * it is known that the other d_w[j] will be 0. Moreover, the returned
1053 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1054 * that u_i is odd. Also, since we always select a positive value for d
1055 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1056 * does. Finally, there is an off-by-one error in the reference: the
1057 * last index should be k-1, not k.
1058 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001059static int ecp_w_naf_fixed( signed char x[], size_t k,
1060 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001061{
1062 int ret;
1063 unsigned int i, u, mask, carry;
1064 mpi M;
1065
1066 mpi_init( &M );
1067
1068 MPI_CHK( mpi_copy( &M, m ) );
1069 mask = ( 1 << w ) - 1;
1070 carry = 1 << ( w - 1 );
1071
1072 for( i = 0; i < k; i++ )
1073 {
1074 u = M.p[0] & mask;
1075
1076 if( ( u & 1 ) == 0 && i > 0 )
1077 x[i - 1] -= carry;
1078
1079 x[i] = u >> 1;
1080 mpi_shift_r( &M, w );
1081 }
1082
1083 /*
1084 * We should have consumed all the bits now
1085 */
1086 if( mpi_cmp_int( &M, 0 ) != 0 )
1087 ret = POLARSSL_ERR_ECP_GENERIC;
1088
1089cleanup:
1090
1091 mpi_free( &M );
1092
1093 return( ret );
1094}
1095
1096/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001097 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1098 * The table is filled with T[i] = (2 * i + 1) P.
1099 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001100static int ecp_precompute( const ecp_group *grp,
1101 ecp_point T[], size_t t_len,
1102 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001103{
1104 int ret;
1105 size_t i;
1106 ecp_point PP;
1107
1108 ecp_point_init( &PP );
1109
1110 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1111
1112 MPI_CHK( ecp_copy( &T[0], P ) );
1113
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001114 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001115 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1116
1117 /*
1118 * T[0] = P already has normalized coordinates
1119 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001120 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001121
1122cleanup:
1123
1124 ecp_point_free( &PP );
1125
1126 return( ret );
1127}
1128
1129/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001130 * Maximum length of the precomputed table
1131 */
1132#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1133
1134/*
1135 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1136 * (that is: grp->nbits / w + 1)
1137 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1138 */
1139#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1140
1141/*
1142 * Integer multiplication: R = m * P
1143 *
1144 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1145 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1146 *
1147 * This function executes a fixed number of operations for
1148 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001149 */
1150int ecp_mul( const ecp_group *grp, ecp_point *R,
1151 const mpi *m, const ecp_point *P )
1152{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001153 int ret;
1154 unsigned char w, m_is_odd;
1155 size_t pre_len, naf_len, i, j;
1156 signed char naf[ MAX_NAF_LEN ];
1157 ecp_point Q, T[ MAX_PRE_LEN ];
1158 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001159
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001160 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001161 return( POLARSSL_ERR_ECP_GENERIC );
1162
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001163 w = grp->nbits >= 521 ? 6 :
1164 grp->nbits >= 224 ? 5 :
1165 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001166
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001167 /*
1168 * Make sure w is within the limits.
1169 * The last test ensures that none of the precomputed points is zero,
1170 * which wouldn't be handled correctly by ecp_normalize_many().
1171 * It is only useful for small curves, as used in the test suite.
1172 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001173 if( w > POLARSSL_ECP_WINDOW_SIZE )
1174 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001175 if( w < 2 || w >= grp->nbits )
1176 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001177
1178 pre_len = 1 << ( w - 1 );
1179 naf_len = grp->nbits / w + 1;
1180
1181 mpi_init( &M );
1182 ecp_point_init( &Q );
1183 for( i = 0; i < pre_len; i++ )
1184 ecp_point_init( &T[i] );
1185
1186 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1187
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001188 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001189 * Make sure M is odd:
1190 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001191 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001192 MPI_CHK( mpi_copy( &M, m ) );
1193 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001194
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001195 /*
1196 * Compute the fixed-pattern NAF and precompute odd multiples
1197 */
1198 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1199 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001200
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001201 /*
1202 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1203 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1204 *
1205 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1206 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1207 * == T[ - naf[i] - 1 ]
1208 */
1209 MPI_CHK( ecp_set_zero( &Q ) );
1210 i = naf_len - 1;
1211 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001212 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001213 if( naf[i] < 0 )
1214 {
1215 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1216 }
1217 else
1218 {
1219 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1220 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001221
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001222 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001223 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001224 i--;
1225
1226 for( j = 0; j < w; j++ )
1227 {
1228 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1229 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001230 }
1231
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001232 /*
1233 * Now get m * P from M * P.
1234 * Since we don't need T[] any more, we can recycle it:
1235 * we already have T[0] = P, now set T[1] = 2 * P.
1236 */
1237 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1238 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001239
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001240
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001241cleanup:
1242
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001243 mpi_free( &M );
1244 ecp_point_free( &Q );
1245 for( i = 0; i < pre_len; i++ )
1246 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001247
1248 return( ret );
1249}
1250
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001251/*
1252 * Generate a keypair (SEC1 3.2.1)
1253 */
1254int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1255 int (*f_rng)(void *, unsigned char *, size_t),
1256 void *p_rng )
1257{
1258 int count = 0;
1259 size_t n_size = (grp->nbits + 7) / 8;
1260
1261 /*
1262 * Generate d such that 1 <= n < N
1263 */
1264 do
1265 {
1266 mpi_fill_random( d, n_size, f_rng, p_rng );
1267
1268 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1269 mpi_shift_r( d, 1 );
1270
1271 if( count++ > 10 )
1272 return( POLARSSL_ERR_ECP_GENERIC );
1273 }
1274 while( mpi_cmp_int( d, 1 ) < 0 );
1275
1276 return( ecp_mul( grp, Q, d, &grp->G ) );
1277}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001278
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001279#if defined(POLARSSL_SELF_TEST)
1280
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001281/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001282 * Checkup routine
1283 */
1284int ecp_self_test( int verbose )
1285{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001286 int ret;
1287 size_t i;
1288 ecp_group grp;
1289 ecp_point R;
1290 mpi m;
1291 unsigned long add_c_prev, dbl_c_prev;
1292 char *exponents[] =
1293 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001294 "000000000000000000000000000000000000000000000000", /* zero */
1295 "000000000000000000000000000000000000000000000001", /* one */
1296 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1297 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001298 "400000000000000000000000000000000000000000000000",
1299 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1300 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001301 };
1302
1303 ecp_group_init( &grp );
1304 ecp_point_init( &R );
1305 mpi_init( &m );
1306
1307 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1308
1309 if( verbose != 0 )
1310 printf( " ECP test #1 (SPA resistance): " );
1311
1312 add_count = 0;
1313 dbl_count = 0;
1314 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1315 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1316
1317 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1318 {
1319 add_c_prev = add_count;
1320 dbl_c_prev = dbl_count;
1321 add_count = 0;
1322 dbl_count = 0;
1323
1324 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1325 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1326
1327 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1328 {
1329 if( verbose != 0 )
1330 printf( "failed (%zu)\n", i );
1331
1332 ret = 1;
1333 goto cleanup;
1334 }
1335 }
1336
1337 if( verbose != 0 )
1338 printf( "passed\n" );
1339
1340cleanup:
1341
1342 if( ret < 0 && verbose != 0 )
1343 printf( "Unexpected error, return code = %08X\n", ret );
1344
1345 ecp_group_free( &grp );
1346 ecp_point_free( &R );
1347 mpi_free( &m );
1348
1349 if( verbose != 0 )
1350 printf( "\n" );
1351
1352 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001353}
1354
1355#endif
1356
1357#endif