blob: cc79b7733396dfd99d2ac6668d2c631d3cb76f21 [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 */
592int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len )
593{
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 */
605 if( *buf++ != POLARSSL_ECP_TLS_NAMED_CURVE )
606 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é-Gonnardb3258872013-02-10 12:06:19 +0100611 id = 256 * buf[0] + buf[1];
612 return ecp_use_known_dp( grp, id );
613}
614
615/*
616 * Write the ECParameters record corresponding to a group (RFC 4492)
617 */
618int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
619 unsigned char *buf, size_t blen )
620{
621 /*
622 * We are going to write 3 bytes (see below)
623 */
624 *olen = 3;
625 if( blen < *olen )
626 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
627
628 /*
629 * First byte is curve_type, always named_curve
630 */
631 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
632
633 /*
634 * Next two bytes are the namedcurve value
635 */
636 buf[0] = grp->id >> 8;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100637 buf[1] = grp->id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100638
639 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100640}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100641
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100642/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100643 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100644 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100645 * In order to guarantee that, we need to ensure that operands of
646 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100647 * bring the result back to this range.
648 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100649 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100650 */
651
652/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100653 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
654 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100655#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100656
657/*
658 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
659 */
660#define MOD_SUB( N ) \
661 while( mpi_cmp_int( &N, 0 ) < 0 ) \
662 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
663
664/*
665 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
666 */
667#define MOD_ADD( N ) \
668 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
669 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
670
671/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100672 * Check that a point is valid as a public key (SEC1 3.2.3.1)
673 */
674int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
675{
676 int ret;
677 mpi YY, RHS;
678
679 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
680 return( POLARSSL_ERR_ECP_GENERIC );
681
682 /*
683 * pt coordinates must be normalized for our checks
684 */
685 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
686 return( POLARSSL_ERR_ECP_GENERIC );
687
688 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
689 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
690 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
691 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
692 return( POLARSSL_ERR_ECP_GENERIC );
693
694 mpi_init( &YY ); mpi_init( &RHS );
695
696 /*
697 * YY = Y^2
698 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
699 */
700 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
701 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
702 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
703 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
704 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
705
706 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
707 ret = POLARSSL_ERR_ECP_GENERIC;
708
709cleanup:
710
711 mpi_free( &YY ); mpi_free( &RHS );
712
713 return( ret );
714}
715
716/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100717 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100718 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100719static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100720{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100721 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100722 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100723
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100724 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100725 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100726
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100727 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100728
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100729 /*
730 * X = X / Z^2 mod p
731 */
732 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
733 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
734 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100735
736 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100737 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100738 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100739 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
740 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100741
742 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100743 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100744 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100745 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100746
747cleanup:
748
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100749 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100750
751 return( ret );
752}
753
754/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100755 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100756 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100757 * (See for example Cohen's "A Course in Computational Algebraic Number
758 * Theory", Algorithm 10.3.4.)
759 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100760 * Warning: fails if one of the points is zero!
761 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100762 */
763static int ecp_normalize_many( const ecp_group *grp,
764 ecp_point T[], size_t t_len )
765{
766 int ret;
767 size_t i;
768 mpi *c, u, Zi, ZZi;
769
770 if( t_len < 2 )
771 return( ecp_normalize( grp, T ) );
772
773 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
774 return( POLARSSL_ERR_ECP_GENERIC );
775
776 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
777 for( i = 0; i < t_len; i++ )
778 mpi_init( &c[i] );
779
780 /*
781 * c[i] = Z_0 * ... * Z_i
782 */
783 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
784 for( i = 1; i < t_len; i++ )
785 {
786 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
787 MOD_MUL( c[i] );
788 }
789
790 /*
791 * u = 1 / (Z_0 * ... * Z_n) mod P
792 */
793 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
794
795 for( i = t_len - 1; ; i-- )
796 {
797 /*
798 * Zi = 1 / Z_i mod p
799 * u = 1 / (Z_0 * ... * Z_i) mod P
800 */
801 if( i == 0 ) {
802 MPI_CHK( mpi_copy( &Zi, &u ) );
803 }
804 else
805 {
806 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
807 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
808 }
809
810 /*
811 * proceed as in normalize()
812 */
813 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
814 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
815 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
816 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
817 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
818
819 if( i == 0 )
820 break;
821 }
822
823cleanup:
824
825 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
826 for( i = 0; i < t_len; i++ )
827 mpi_free( &c[i] );
828 free( c );
829
830 return( ret );
831}
832
833
834/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100835 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
836 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100837static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
838 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100839{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100840 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100841 mpi T1, T2, T3, X, Y, Z;
842
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100843#if defined(POLARSSL_SELF_TEST)
844 dbl_count++;
845#endif
846
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100847 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100848 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100849
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100850 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
851 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
852
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100853 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
854 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
855 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
856 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
857 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
858 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
859 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
860 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
861 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
862 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100863
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100864 /*
865 * For Y = Y / 2 mod p, we must make sure that Y is even before
866 * using right-shift. No need to reduce mod p afterwards.
867 */
868 if( mpi_get_bit( &Y, 0 ) == 1 )
869 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
870 MPI_CHK( mpi_shift_r( &Y, 1 ) );
871
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100872 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
873 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
874 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
875 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
876 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
877 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100878
879 MPI_CHK( mpi_copy( &R->X, &X ) );
880 MPI_CHK( mpi_copy( &R->Y, &Y ) );
881 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100882
883cleanup:
884
885 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
886 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
887
888 return( ret );
889}
890
891/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100892 * Addition or subtraction: R = P + Q or R = P + Q,
893 * mixed affine-Jacobian coordinates (GECC 3.22)
894 *
895 * The coordinates of Q must be normalized (= affine),
896 * but those of P don't need to. R is not normalized.
897 *
898 * If sign >= 0, perform addition, otherwise perform subtraction,
899 * taking advantage of the fact that, for Q != 0, we have
900 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100901 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100902static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100903 const ecp_point *P, const ecp_point *Q,
904 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100905{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100906 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100907 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100908
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100909#if defined(POLARSSL_SELF_TEST)
910 add_count++;
911#endif
912
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100913 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100914 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100915 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100916 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100917 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
918 return( ecp_copy( R, P ) );
919
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100920 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
921 {
922 ret = ecp_copy( R, Q );
923
924 /*
925 * -R.Y mod P = P - R.Y unless R.Y == 0
926 */
927 if( ret == 0 && sign < 0)
928 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
929 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
930
931 return( ret );
932 }
933
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100934 /*
935 * Make sure Q coordinates are normalized
936 */
937 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
938 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100939
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100940 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
941 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100942
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100943 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
944 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
945 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
946 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100947
948 /*
949 * For subtraction, -Q.Y should have been used instead of Q.Y,
950 * so we replace T2 by -T2, which is P - T2 mod P
951 */
952 if( sign < 0 )
953 {
954 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
955 MOD_SUB( T2 );
956 }
957
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100958 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
959 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100960
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100961 if( mpi_cmp_int( &T1, 0 ) == 0 )
962 {
963 if( mpi_cmp_int( &T2, 0 ) == 0 )
964 {
965 ret = ecp_double_jac( grp, R, P );
966 goto cleanup;
967 }
968 else
969 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100970 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100971 goto cleanup;
972 }
973 }
974
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100975 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
976 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
977 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
978 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
979 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
980 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
981 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
982 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
983 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
984 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
985 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
986 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100987
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100988 MPI_CHK( mpi_copy( &R->X, &X ) );
989 MPI_CHK( mpi_copy( &R->Y, &Y ) );
990 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100991
992cleanup:
993
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100994 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
995 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100996
997 return( ret );
998}
999
1000/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001001 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001002 */
1003int ecp_add( const ecp_group *grp, ecp_point *R,
1004 const ecp_point *P, const ecp_point *Q )
1005{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001006 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001007
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001008 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1009 MPI_CHK( ecp_normalize( grp, R ) );
1010
1011cleanup:
1012 return( ret );
1013}
1014
1015/*
1016 * Subtraction: R = P - Q, result's coordinates normalized
1017 */
1018int ecp_sub( const ecp_group *grp, ecp_point *R,
1019 const ecp_point *P, const ecp_point *Q )
1020{
1021 int ret;
1022
1023 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001024 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001025
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001026cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001027 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001028}
1029
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001030/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001031 * Compute a modified width-w non-adjacent form (NAF) of a number,
1032 * with a fixed pattern for resistance to SPA/timing attacks,
1033 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1034 * (The resulting multiplication algorithm can also been seen as a
1035 * modification of 2^w-ary multiplication, with signed coefficients,
1036 * all of them odd.)
1037 *
1038 * Input:
1039 * m must be an odd positive mpi less than w * k bits long
1040 * x must be an array of k elements
1041 * w must be less than a certain maximum (currently 8)
1042 *
1043 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1044 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1045 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1046 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1047 *
1048 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1049 * p. 335 of the cited reference, here we return only u, not d_w since
1050 * it is known that the other d_w[j] will be 0. Moreover, the returned
1051 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1052 * that u_i is odd. Also, since we always select a positive value for d
1053 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1054 * does. Finally, there is an off-by-one error in the reference: the
1055 * last index should be k-1, not k.
1056 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001057static int ecp_w_naf_fixed( signed char x[], size_t k,
1058 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001059{
1060 int ret;
1061 unsigned int i, u, mask, carry;
1062 mpi M;
1063
1064 mpi_init( &M );
1065
1066 MPI_CHK( mpi_copy( &M, m ) );
1067 mask = ( 1 << w ) - 1;
1068 carry = 1 << ( w - 1 );
1069
1070 for( i = 0; i < k; i++ )
1071 {
1072 u = M.p[0] & mask;
1073
1074 if( ( u & 1 ) == 0 && i > 0 )
1075 x[i - 1] -= carry;
1076
1077 x[i] = u >> 1;
1078 mpi_shift_r( &M, w );
1079 }
1080
1081 /*
1082 * We should have consumed all the bits now
1083 */
1084 if( mpi_cmp_int( &M, 0 ) != 0 )
1085 ret = POLARSSL_ERR_ECP_GENERIC;
1086
1087cleanup:
1088
1089 mpi_free( &M );
1090
1091 return( ret );
1092}
1093
1094/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001095 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1096 * The table is filled with T[i] = (2 * i + 1) P.
1097 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001098static int ecp_precompute( const ecp_group *grp,
1099 ecp_point T[], size_t t_len,
1100 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001101{
1102 int ret;
1103 size_t i;
1104 ecp_point PP;
1105
1106 ecp_point_init( &PP );
1107
1108 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1109
1110 MPI_CHK( ecp_copy( &T[0], P ) );
1111
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001112 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001113 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1114
1115 /*
1116 * T[0] = P already has normalized coordinates
1117 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001118 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001119
1120cleanup:
1121
1122 ecp_point_free( &PP );
1123
1124 return( ret );
1125}
1126
1127/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001128 * Maximum length of the precomputed table
1129 */
1130#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1131
1132/*
1133 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1134 * (that is: grp->nbits / w + 1)
1135 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1136 */
1137#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1138
1139/*
1140 * Integer multiplication: R = m * P
1141 *
1142 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1143 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1144 *
1145 * This function executes a fixed number of operations for
1146 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001147 */
1148int ecp_mul( const ecp_group *grp, ecp_point *R,
1149 const mpi *m, const ecp_point *P )
1150{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001151 int ret;
1152 unsigned char w, m_is_odd;
1153 size_t pre_len, naf_len, i, j;
1154 signed char naf[ MAX_NAF_LEN ];
1155 ecp_point Q, T[ MAX_PRE_LEN ];
1156 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001157
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001158 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001159 return( POLARSSL_ERR_ECP_GENERIC );
1160
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001161 w = grp->nbits >= 521 ? 6 :
1162 grp->nbits >= 224 ? 5 :
1163 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001164
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001165 /*
1166 * Make sure w is within the limits.
1167 * The last test ensures that none of the precomputed points is zero,
1168 * which wouldn't be handled correctly by ecp_normalize_many().
1169 * It is only useful for small curves, as used in the test suite.
1170 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001171 if( w > POLARSSL_ECP_WINDOW_SIZE )
1172 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001173 if( w < 2 || w >= grp->nbits )
1174 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001175
1176 pre_len = 1 << ( w - 1 );
1177 naf_len = grp->nbits / w + 1;
1178
1179 mpi_init( &M );
1180 ecp_point_init( &Q );
1181 for( i = 0; i < pre_len; i++ )
1182 ecp_point_init( &T[i] );
1183
1184 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1185
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001186 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001187 * Make sure M is odd:
1188 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001189 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001190 MPI_CHK( mpi_copy( &M, m ) );
1191 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001192
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001193 /*
1194 * Compute the fixed-pattern NAF and precompute odd multiples
1195 */
1196 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1197 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001198
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001199 /*
1200 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1201 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1202 *
1203 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1204 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1205 * == T[ - naf[i] - 1 ]
1206 */
1207 MPI_CHK( ecp_set_zero( &Q ) );
1208 i = naf_len - 1;
1209 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001210 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001211 if( naf[i] < 0 )
1212 {
1213 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1214 }
1215 else
1216 {
1217 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1218 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001219
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001220 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001221 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001222 i--;
1223
1224 for( j = 0; j < w; j++ )
1225 {
1226 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1227 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001228 }
1229
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001230 /*
1231 * Now get m * P from M * P.
1232 * Since we don't need T[] any more, we can recycle it:
1233 * we already have T[0] = P, now set T[1] = 2 * P.
1234 */
1235 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1236 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001237
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001238
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001239cleanup:
1240
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001241 mpi_free( &M );
1242 ecp_point_free( &Q );
1243 for( i = 0; i < pre_len; i++ )
1244 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001245
1246 return( ret );
1247}
1248
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001249/*
1250 * Generate a keypair (SEC1 3.2.1)
1251 */
1252int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1253 int (*f_rng)(void *, unsigned char *, size_t),
1254 void *p_rng )
1255{
1256 int count = 0;
1257 size_t n_size = (grp->nbits + 7) / 8;
1258
1259 /*
1260 * Generate d such that 1 <= n < N
1261 */
1262 do
1263 {
1264 mpi_fill_random( d, n_size, f_rng, p_rng );
1265
1266 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1267 mpi_shift_r( d, 1 );
1268
1269 if( count++ > 10 )
1270 return( POLARSSL_ERR_ECP_GENERIC );
1271 }
1272 while( mpi_cmp_int( d, 1 ) < 0 );
1273
1274 return( ecp_mul( grp, Q, d, &grp->G ) );
1275}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001276
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001277#if defined(POLARSSL_SELF_TEST)
1278
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001279/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001280 * Checkup routine
1281 */
1282int ecp_self_test( int verbose )
1283{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001284 int ret;
1285 size_t i;
1286 ecp_group grp;
1287 ecp_point R;
1288 mpi m;
1289 unsigned long add_c_prev, dbl_c_prev;
1290 char *exponents[] =
1291 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001292 "000000000000000000000000000000000000000000000000", /* zero */
1293 "000000000000000000000000000000000000000000000001", /* one */
1294 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1295 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001296 "400000000000000000000000000000000000000000000000",
1297 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1298 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001299 };
1300
1301 ecp_group_init( &grp );
1302 ecp_point_init( &R );
1303 mpi_init( &m );
1304
1305 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1306
1307 if( verbose != 0 )
1308 printf( " ECP test #1 (SPA resistance): " );
1309
1310 add_count = 0;
1311 dbl_count = 0;
1312 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1313 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1314
1315 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1316 {
1317 add_c_prev = add_count;
1318 dbl_c_prev = dbl_count;
1319 add_count = 0;
1320 dbl_count = 0;
1321
1322 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1323 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1324
1325 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1326 {
1327 if( verbose != 0 )
1328 printf( "failed (%zu)\n", i );
1329
1330 ret = 1;
1331 goto cleanup;
1332 }
1333 }
1334
1335 if( verbose != 0 )
1336 printf( "passed\n" );
1337
1338cleanup:
1339
1340 if( ret < 0 && verbose != 0 )
1341 printf( "Unexpected error, return code = %08X\n", ret );
1342
1343 ecp_group_free( &grp );
1344 ecp_point_free( &R );
1345 mpi_free( &m );
1346
1347 if( verbose != 0 )
1348 printf( "\n" );
1349
1350 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001351}
1352
1353#endif
1354
1355#endif