blob: 233c2eba40225aeade5594ef47da57909c9d2f10 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
Paul Bakkercf4365f2013-01-16 17:00:43 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010033 */
34
35#include "polarssl/config.h"
36
37#if defined(POLARSSL_ECP_C)
38
39#include "polarssl/ecp.h"
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010040#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010041#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010042
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010043#if defined(POLARSSL_SELF_TEST)
44/*
45 * Counts of point addition and doubling operations.
46 * Used to test resistance of point multiplication to SPA/timing attacks.
47 */
48unsigned long add_count, dbl_count;
49#endif
50
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010051/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010052 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010053 */
54void ecp_point_init( ecp_point *pt )
55{
56 if( pt == NULL )
57 return;
58
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010059 mpi_init( &pt->X );
60 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010061 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010062}
63
64/*
65 * Initialize (the components of) a group
66 */
67void ecp_group_init( ecp_group *grp )
68{
69 if( grp == NULL )
70 return;
71
72 mpi_init( &grp->P );
73 mpi_init( &grp->B );
74 ecp_point_init( &grp->G );
75 mpi_init( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010076
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010077 grp->pbits = 0;
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010078 grp->nbits = 0;
79
80 grp->modp = NULL;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010081}
82
83/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010084 * Unallocate (the components of) a point
85 */
86void ecp_point_free( ecp_point *pt )
87{
88 if( pt == NULL )
89 return;
90
91 mpi_free( &( pt->X ) );
92 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010093 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010094}
95
96/*
97 * Unallocate (the components of) a group
98 */
99void ecp_group_free( ecp_group *grp )
100{
101 if( grp == NULL )
102 return;
103
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100104 mpi_free( &grp->P );
105 mpi_free( &grp->B );
106 ecp_point_free( &grp->G );
107 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100108}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100109
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100110/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100111 * Set point to zero
112 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100113int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100114{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100115 int ret;
116
117 MPI_CHK( mpi_lset( &pt->X , 1 ) );
118 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
119 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
120
121cleanup:
122 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100123}
124
125/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100126 * Tell if a point is zero
127 */
128int ecp_is_zero( ecp_point *pt )
129{
130 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
131}
132
133/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100134 * Copy the contents of Q into P
135 */
136int ecp_copy( ecp_point *P, const ecp_point *Q )
137{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100138 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100139
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100140 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
141 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100142 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100143
144cleanup:
145 return( ret );
146}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100147
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100148/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100149 * Import a non-zero point from ASCII strings
150 */
151int ecp_point_read_string( ecp_point *P, int radix,
152 const char *x, const char *y )
153{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100154 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100155
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100156 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
157 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100158 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100159
160cleanup:
161 return( ret );
162}
163
164/*
165 * Import an ECP group from ASCII strings
166 */
167int ecp_group_read_string( ecp_group *grp, int radix,
168 const char *p, const char *b,
169 const char *gx, const char *gy, const char *n)
170{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100171 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100172
173 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
174 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
175 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
176 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
177
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100178 grp->pbits = mpi_msb( &grp->P );
179 grp->nbits = mpi_msb( &grp->N );
180
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100181cleanup:
182 return( ret );
183}
184
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100185/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100186 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100187 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100188int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100189 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100190 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100191{
192 int ret;
193 size_t plen;
194
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100195 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
196 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100197 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100198
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100199 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100200 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100201 */
202 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
203 {
204 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100205 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100206
207 buf[0] = 0x00;
208 *olen = 1;
209
210 return( 0 );
211 }
212
213 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100214
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100215 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
216 {
217 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100218
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100219 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100220 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100221
222 buf[0] = 0x04;
223 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
224 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
225 }
226 else if( format == POLARSSL_ECP_PF_COMPRESSED )
227 {
228 *olen = plen + 1;
229
230 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100231 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100232
233 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
234 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
235 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100236
237cleanup:
238 return( ret );
239}
240
241/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100242 * Import a point from unsigned binary data (SEC1 2.3.4)
243 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100244int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
245 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100246 int ret;
247 size_t plen;
248
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100249 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100250 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100251
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100252 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100253
254 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100255 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100256
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100257 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
258 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
259 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100260
261cleanup:
262 return( ret );
263}
264
265/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100266 * Import a point from a TLS ECPoint record (RFC 4492)
267 * struct {
268 * opaque point <1..2^8-1>;
269 * } ECPoint;
270 */
271int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
272 const unsigned char *buf, size_t buf_len )
273{
274 unsigned char data_len;
275
276 /*
277 * We must have at least two bytes (1 for length, at least of for data)
278 */
279 if( buf_len < 2 )
280 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
281
282 data_len = *buf++;
283 if( data_len < 1 || data_len > buf_len - 1 )
284 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
285
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100286 return ecp_point_read_binary( grp, pt, buf, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100287}
288
289/*
290 * Export a point as a TLS ECPoint record (RFC 4492)
291 * struct {
292 * opaque point <1..2^8-1>;
293 * } ECPoint;
294 */
295int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100296 int format, size_t *olen,
297 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100298{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100299 int ret;
300
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100301 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100302 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100303 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100304 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100305 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
306
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100307 if( ( ret = ecp_point_write_binary( grp, pt, format,
308 olen, buf + 1, blen - 1) ) != 0 )
309 return( ret );
310
311 /*
312 * write length to the first byte and update total length
313 */
314 buf[0] = *olen;
315 ++*olen;
316
317 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100318}
319
320/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100321 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
322 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100323 */
324static int ecp_modp( mpi *N, const ecp_group *grp )
325{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100326 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100327
328 if( grp->modp == NULL )
329 return( mpi_mod_mpi( N, N, &grp->P ) );
330
331 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
332 return( POLARSSL_ERR_ECP_GENERIC );
333
334 MPI_CHK( grp->modp( N ) );
335
336 while( mpi_cmp_int( N, 0 ) < 0 )
337 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
338
339 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
340 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
341
342cleanup:
343 return( ret );
344}
345
346/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100347 * 192 bits in terms of t_uint
348 */
349#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
350
351/*
352 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
353 * -1 means let this chunk be 0
354 * a positive value i means A_i.
355 */
356#define P192_CHUNKS 3
357#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
358#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
359
360const signed char p192_tbl[][P192_CHUNKS] = {
361 { -1, 3, 3 }, /* S1 */
362 { 4, 4, -1 }, /* S2 */
363 { 5, 5, 5 }, /* S3 */
364};
365
366/*
367 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
368 */
369static int ecp_mod_p192( mpi *N )
370{
371 int ret;
372 unsigned char i, j, offset;
373 signed char chunk;
374 mpi tmp, acc;
375 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
376
377 tmp.s = 1;
378 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
379 tmp.p = tmp_p;
380
381 acc.s = 1;
382 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
383 acc.p = acc_p;
384
385 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
386
387 /*
388 * acc = T
389 */
390 memset( acc_p, 0, sizeof( acc_p ) );
391 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
392
393 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
394 {
395 /*
396 * tmp = S_i
397 */
398 memset( tmp_p, 0, sizeof( tmp_p ) );
399 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
400 {
401 chunk = p192_tbl[i][j];
402 if( chunk >= 0 )
403 memcpy( tmp_p + offset * P192_CHUNK_INT,
404 N->p + chunk * P192_CHUNK_INT,
405 P192_CHUNK_CHAR );
406 }
407
408 /*
409 * acc += tmp
410 */
411 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
412 }
413
414 MPI_CHK( mpi_copy( N, &acc ) );
415
416cleanup:
417 return( ret );
418}
419
420/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100421 * Size of p521 in terms of t_uint
422 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100423#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100424
425/*
426 * Bits to keep in the most significant t_uint
427 */
428#if defined(POLARSS_HAVE_INT8)
429#define P521_MASK 0x01
430#else
431#define P521_MASK 0x01FF
432#endif
433
434/*
435 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100436 */
437static int ecp_mod_p521( mpi *N )
438{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100439 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100440 t_uint Mp[P521_SIZE_INT];
441 mpi M;
442
443 if( N->n < P521_SIZE_INT )
444 return( 0 );
445
446 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
447 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
448 Mp[P521_SIZE_INT - 1] &= P521_MASK;
449
450 M.s = 1;
451 M.n = P521_SIZE_INT;
452 M.p = Mp;
453
454 MPI_CHK( mpi_shift_r( N, 521 ) );
455
456 MPI_CHK( mpi_add_abs( N, N, &M ) );
457
458cleanup:
459 return( ret );
460}
461
462/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100463 * Domain parameters for secp192r1
464 */
465#define SECP192R1_P \
466 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
467#define SECP192R1_B \
468 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
469#define SECP192R1_GX \
470 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
471#define SECP192R1_GY \
472 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
473#define SECP192R1_N \
474 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
475
476/*
477 * Domain parameters for secp224r1
478 */
479#define SECP224R1_P \
480 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
481#define SECP224R1_B \
482 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
483#define SECP224R1_GX \
484 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
485#define SECP224R1_GY \
486 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
487#define SECP224R1_N \
488 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
489
490/*
491 * Domain parameters for secp256r1
492 */
493#define SECP256R1_P \
494 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
495#define SECP256R1_B \
496 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
497#define SECP256R1_GX \
498 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
499#define SECP256R1_GY \
500 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
501#define SECP256R1_N \
502 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
503
504/*
505 * Domain parameters for secp384r1
506 */
507#define SECP384R1_P \
508 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
509 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
510#define SECP384R1_B \
511 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
512 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
513#define SECP384R1_GX \
514 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
515 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
516#define SECP384R1_GY \
517 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
518 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
519#define SECP384R1_N \
520 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
521 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
522
523/*
524 * Domain parameters for secp521r1
525 */
526#define SECP521R1_P \
527 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
528 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
529 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
530#define SECP521R1_B \
531 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
532 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
533 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
534#define SECP521R1_GX \
535 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
536 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
537 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
538#define SECP521R1_GY \
539 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
540 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
541 "3FAD0761353C7086A272C24088BE94769FD16650"
542#define SECP521R1_N \
543 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
544 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
545 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
546
547/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100548 * Set a group using well-known domain parameters
549 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100550int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100551{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100552 grp->id = id;
553
554 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100555 {
556 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100557 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100558 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100559 SECP192R1_P, SECP192R1_B,
560 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
561
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100562 case POLARSSL_ECP_DP_SECP224R1:
563 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100564 SECP224R1_P, SECP224R1_B,
565 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
566
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100567 case POLARSSL_ECP_DP_SECP256R1:
568 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100569 SECP256R1_P, SECP256R1_B,
570 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
571
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100572 case POLARSSL_ECP_DP_SECP384R1:
573 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100574 SECP384R1_P, SECP384R1_B,
575 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
576
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100577 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100578 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100579 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100580 SECP521R1_P, SECP521R1_B,
581 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100582 }
583
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100584 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
585}
586
587/*
588 * Set a group from an ECParameters record (RFC 4492)
589 */
590int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len )
591{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100592 ecp_group_id id;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100593
594 /*
595 * We expect at least three bytes (see below)
596 */
597 if( len < 3 )
598 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
599
600 /*
601 * First byte is curve_type; only named_curve is handled
602 */
603 if( *buf++ != POLARSSL_ECP_TLS_NAMED_CURVE )
604 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
605
606 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100607 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100608 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100609 id = 256 * buf[0] + buf[1];
610 return ecp_use_known_dp( grp, id );
611}
612
613/*
614 * Write the ECParameters record corresponding to a group (RFC 4492)
615 */
616int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
617 unsigned char *buf, size_t blen )
618{
619 /*
620 * We are going to write 3 bytes (see below)
621 */
622 *olen = 3;
623 if( blen < *olen )
624 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
625
626 /*
627 * First byte is curve_type, always named_curve
628 */
629 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
630
631 /*
632 * Next two bytes are the namedcurve value
633 */
634 buf[0] = grp->id >> 8;
635 buf[1] = grp->id && 0xFF;
636
637 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100638}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100639
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100640/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100641 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100642 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100643 * In order to guarantee that, we need to ensure that operands of
644 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100645 * bring the result back to this range.
646 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100647 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100648 */
649
650/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100651 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
652 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100653#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100654
655/*
656 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
657 */
658#define MOD_SUB( N ) \
659 while( mpi_cmp_int( &N, 0 ) < 0 ) \
660 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
661
662/*
663 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
664 */
665#define MOD_ADD( N ) \
666 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
667 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
668
669/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100670 * Check that a point is valid as a public key (SEC1 3.2.3.1)
671 */
672int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
673{
674 int ret;
675 mpi YY, RHS;
676
677 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
678 return( POLARSSL_ERR_ECP_GENERIC );
679
680 /*
681 * pt coordinates must be normalized for our checks
682 */
683 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
684 return( POLARSSL_ERR_ECP_GENERIC );
685
686 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
687 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
688 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
689 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
690 return( POLARSSL_ERR_ECP_GENERIC );
691
692 mpi_init( &YY ); mpi_init( &RHS );
693
694 /*
695 * YY = Y^2
696 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
697 */
698 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
699 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
700 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
701 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
702 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
703
704 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
705 ret = POLARSSL_ERR_ECP_GENERIC;
706
707cleanup:
708
709 mpi_free( &YY ); mpi_free( &RHS );
710
711 return( ret );
712}
713
714/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100715 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100716 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100717static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100718{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100719 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100720 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100721
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100722 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100723 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100724
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100725 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100726
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100727 /*
728 * X = X / Z^2 mod p
729 */
730 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
731 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
732 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100733
734 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100735 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100736 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100737 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
738 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100739
740 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100741 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100742 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100743 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100744
745cleanup:
746
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100747 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100748
749 return( ret );
750}
751
752/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100753 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100754 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100755 * (See for example Cohen's "A Course in Computational Algebraic Number
756 * Theory", Algorithm 10.3.4.)
757 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100758 * Warning: fails if one of the points is zero!
759 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100760 */
761static int ecp_normalize_many( const ecp_group *grp,
762 ecp_point T[], size_t t_len )
763{
764 int ret;
765 size_t i;
766 mpi *c, u, Zi, ZZi;
767
768 if( t_len < 2 )
769 return( ecp_normalize( grp, T ) );
770
771 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
772 return( POLARSSL_ERR_ECP_GENERIC );
773
774 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
775 for( i = 0; i < t_len; i++ )
776 mpi_init( &c[i] );
777
778 /*
779 * c[i] = Z_0 * ... * Z_i
780 */
781 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
782 for( i = 1; i < t_len; i++ )
783 {
784 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
785 MOD_MUL( c[i] );
786 }
787
788 /*
789 * u = 1 / (Z_0 * ... * Z_n) mod P
790 */
791 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
792
793 for( i = t_len - 1; ; i-- )
794 {
795 /*
796 * Zi = 1 / Z_i mod p
797 * u = 1 / (Z_0 * ... * Z_i) mod P
798 */
799 if( i == 0 ) {
800 MPI_CHK( mpi_copy( &Zi, &u ) );
801 }
802 else
803 {
804 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
805 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
806 }
807
808 /*
809 * proceed as in normalize()
810 */
811 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
812 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
813 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
814 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
815 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
816
817 if( i == 0 )
818 break;
819 }
820
821cleanup:
822
823 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
824 for( i = 0; i < t_len; i++ )
825 mpi_free( &c[i] );
826 free( c );
827
828 return( ret );
829}
830
831
832/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100833 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
834 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100835static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
836 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100837{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100838 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100839 mpi T1, T2, T3, X, Y, Z;
840
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100841#if defined(POLARSSL_SELF_TEST)
842 dbl_count++;
843#endif
844
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100845 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100846 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100847
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100848 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
849 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
850
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100851 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
852 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
853 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
854 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
855 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
856 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
857 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
858 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
859 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
860 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100861
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100862 /*
863 * For Y = Y / 2 mod p, we must make sure that Y is even before
864 * using right-shift. No need to reduce mod p afterwards.
865 */
866 if( mpi_get_bit( &Y, 0 ) == 1 )
867 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
868 MPI_CHK( mpi_shift_r( &Y, 1 ) );
869
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100870 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
871 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
872 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
873 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
874 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
875 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100876
877 MPI_CHK( mpi_copy( &R->X, &X ) );
878 MPI_CHK( mpi_copy( &R->Y, &Y ) );
879 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100880
881cleanup:
882
883 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
884 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
885
886 return( ret );
887}
888
889/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100890 * Addition or subtraction: R = P + Q or R = P + Q,
891 * mixed affine-Jacobian coordinates (GECC 3.22)
892 *
893 * The coordinates of Q must be normalized (= affine),
894 * but those of P don't need to. R is not normalized.
895 *
896 * If sign >= 0, perform addition, otherwise perform subtraction,
897 * taking advantage of the fact that, for Q != 0, we have
898 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100899 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100900static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100901 const ecp_point *P, const ecp_point *Q,
902 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100903{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100904 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100905 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100906
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100907#if defined(POLARSSL_SELF_TEST)
908 add_count++;
909#endif
910
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100911 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100912 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100913 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100914 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100915 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
916 return( ecp_copy( R, P ) );
917
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100918 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
919 {
920 ret = ecp_copy( R, Q );
921
922 /*
923 * -R.Y mod P = P - R.Y unless R.Y == 0
924 */
925 if( ret == 0 && sign < 0)
926 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
927 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
928
929 return( ret );
930 }
931
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100932 /*
933 * Make sure Q coordinates are normalized
934 */
935 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
936 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100937
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100938 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
939 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100940
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100941 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
942 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
943 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
944 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100945
946 /*
947 * For subtraction, -Q.Y should have been used instead of Q.Y,
948 * so we replace T2 by -T2, which is P - T2 mod P
949 */
950 if( sign < 0 )
951 {
952 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
953 MOD_SUB( T2 );
954 }
955
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100956 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
957 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100958
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100959 if( mpi_cmp_int( &T1, 0 ) == 0 )
960 {
961 if( mpi_cmp_int( &T2, 0 ) == 0 )
962 {
963 ret = ecp_double_jac( grp, R, P );
964 goto cleanup;
965 }
966 else
967 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100968 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100969 goto cleanup;
970 }
971 }
972
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100973 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
974 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
975 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
976 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
977 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
978 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
979 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
980 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
981 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
982 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
983 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
984 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100985
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100986 MPI_CHK( mpi_copy( &R->X, &X ) );
987 MPI_CHK( mpi_copy( &R->Y, &Y ) );
988 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100989
990cleanup:
991
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100992 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
993 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100994
995 return( ret );
996}
997
998/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100999 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001000 */
1001int ecp_add( const ecp_group *grp, ecp_point *R,
1002 const ecp_point *P, const ecp_point *Q )
1003{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001004 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001005
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001006 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1007 MPI_CHK( ecp_normalize( grp, R ) );
1008
1009cleanup:
1010 return( ret );
1011}
1012
1013/*
1014 * Subtraction: R = P - Q, result's coordinates normalized
1015 */
1016int ecp_sub( const ecp_group *grp, ecp_point *R,
1017 const ecp_point *P, const ecp_point *Q )
1018{
1019 int ret;
1020
1021 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001022 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001023
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001024cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001025 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001026}
1027
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001028/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001029 * Compute a modified width-w non-adjacent form (NAF) of a number,
1030 * with a fixed pattern for resistance to SPA/timing attacks,
1031 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1032 * (The resulting multiplication algorithm can also been seen as a
1033 * modification of 2^w-ary multiplication, with signed coefficients,
1034 * all of them odd.)
1035 *
1036 * Input:
1037 * m must be an odd positive mpi less than w * k bits long
1038 * x must be an array of k elements
1039 * w must be less than a certain maximum (currently 8)
1040 *
1041 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1042 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1043 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1044 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1045 *
1046 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1047 * p. 335 of the cited reference, here we return only u, not d_w since
1048 * it is known that the other d_w[j] will be 0. Moreover, the returned
1049 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1050 * that u_i is odd. Also, since we always select a positive value for d
1051 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1052 * does. Finally, there is an off-by-one error in the reference: the
1053 * last index should be k-1, not k.
1054 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001055static int ecp_w_naf_fixed( signed char x[], size_t k,
1056 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001057{
1058 int ret;
1059 unsigned int i, u, mask, carry;
1060 mpi M;
1061
1062 mpi_init( &M );
1063
1064 MPI_CHK( mpi_copy( &M, m ) );
1065 mask = ( 1 << w ) - 1;
1066 carry = 1 << ( w - 1 );
1067
1068 for( i = 0; i < k; i++ )
1069 {
1070 u = M.p[0] & mask;
1071
1072 if( ( u & 1 ) == 0 && i > 0 )
1073 x[i - 1] -= carry;
1074
1075 x[i] = u >> 1;
1076 mpi_shift_r( &M, w );
1077 }
1078
1079 /*
1080 * We should have consumed all the bits now
1081 */
1082 if( mpi_cmp_int( &M, 0 ) != 0 )
1083 ret = POLARSSL_ERR_ECP_GENERIC;
1084
1085cleanup:
1086
1087 mpi_free( &M );
1088
1089 return( ret );
1090}
1091
1092/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001093 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1094 * The table is filled with T[i] = (2 * i + 1) P.
1095 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001096static int ecp_precompute( const ecp_group *grp,
1097 ecp_point T[], size_t t_len,
1098 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001099{
1100 int ret;
1101 size_t i;
1102 ecp_point PP;
1103
1104 ecp_point_init( &PP );
1105
1106 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1107
1108 MPI_CHK( ecp_copy( &T[0], P ) );
1109
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001110 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001111 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1112
1113 /*
1114 * T[0] = P already has normalized coordinates
1115 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001116 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001117
1118cleanup:
1119
1120 ecp_point_free( &PP );
1121
1122 return( ret );
1123}
1124
1125/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001126 * Maximum length of the precomputed table
1127 */
1128#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1129
1130/*
1131 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1132 * (that is: grp->nbits / w + 1)
1133 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1134 */
1135#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1136
1137/*
1138 * Integer multiplication: R = m * P
1139 *
1140 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1141 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1142 *
1143 * This function executes a fixed number of operations for
1144 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001145 */
1146int ecp_mul( const ecp_group *grp, ecp_point *R,
1147 const mpi *m, const ecp_point *P )
1148{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001149 int ret;
1150 unsigned char w, m_is_odd;
1151 size_t pre_len, naf_len, i, j;
1152 signed char naf[ MAX_NAF_LEN ];
1153 ecp_point Q, T[ MAX_PRE_LEN ];
1154 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001155
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001156 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001157 return( POLARSSL_ERR_ECP_GENERIC );
1158
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001159 w = grp->nbits >= 521 ? 6 :
1160 grp->nbits >= 224 ? 5 :
1161 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001162
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001163 /*
1164 * Make sure w is within the limits.
1165 * The last test ensures that none of the precomputed points is zero,
1166 * which wouldn't be handled correctly by ecp_normalize_many().
1167 * It is only useful for small curves, as used in the test suite.
1168 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001169 if( w > POLARSSL_ECP_WINDOW_SIZE )
1170 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001171 if( w < 2 || w >= grp->nbits )
1172 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001173
1174 pre_len = 1 << ( w - 1 );
1175 naf_len = grp->nbits / w + 1;
1176
1177 mpi_init( &M );
1178 ecp_point_init( &Q );
1179 for( i = 0; i < pre_len; i++ )
1180 ecp_point_init( &T[i] );
1181
1182 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1183
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001184 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001185 * Make sure M is odd:
1186 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001187 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001188 MPI_CHK( mpi_copy( &M, m ) );
1189 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001190
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001191 /*
1192 * Compute the fixed-pattern NAF and precompute odd multiples
1193 */
1194 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1195 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001196
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001197 /*
1198 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1199 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1200 *
1201 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1202 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1203 * == T[ - naf[i] - 1 ]
1204 */
1205 MPI_CHK( ecp_set_zero( &Q ) );
1206 i = naf_len - 1;
1207 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001208 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001209 if( naf[i] < 0 )
1210 {
1211 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1212 }
1213 else
1214 {
1215 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1216 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001217
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001218 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001219 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001220 i--;
1221
1222 for( j = 0; j < w; j++ )
1223 {
1224 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1225 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001226 }
1227
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001228 /*
1229 * Now get m * P from M * P.
1230 * Since we don't need T[] any more, we can recycle it:
1231 * we already have T[0] = P, now set T[1] = 2 * P.
1232 */
1233 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1234 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001235
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001236
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001237cleanup:
1238
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001239 mpi_free( &M );
1240 ecp_point_free( &Q );
1241 for( i = 0; i < pre_len; i++ )
1242 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001243
1244 return( ret );
1245}
1246
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001247/*
1248 * Generate a keypair (SEC1 3.2.1)
1249 */
1250int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1251 int (*f_rng)(void *, unsigned char *, size_t),
1252 void *p_rng )
1253{
1254 int count = 0;
1255 size_t n_size = (grp->nbits + 7) / 8;
1256
1257 /*
1258 * Generate d such that 1 <= n < N
1259 */
1260 do
1261 {
1262 mpi_fill_random( d, n_size, f_rng, p_rng );
1263
1264 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1265 mpi_shift_r( d, 1 );
1266
1267 if( count++ > 10 )
1268 return( POLARSSL_ERR_ECP_GENERIC );
1269 }
1270 while( mpi_cmp_int( d, 1 ) < 0 );
1271
1272 return( ecp_mul( grp, Q, d, &grp->G ) );
1273}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001274
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001275#if defined(POLARSSL_SELF_TEST)
1276
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001277/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001278 * Checkup routine
1279 */
1280int ecp_self_test( int verbose )
1281{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001282 int ret;
1283 size_t i;
1284 ecp_group grp;
1285 ecp_point R;
1286 mpi m;
1287 unsigned long add_c_prev, dbl_c_prev;
1288 char *exponents[] =
1289 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001290 "000000000000000000000000000000000000000000000000", /* zero */
1291 "000000000000000000000000000000000000000000000001", /* one */
1292 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1293 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001294 "400000000000000000000000000000000000000000000000",
1295 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1296 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001297 };
1298
1299 ecp_group_init( &grp );
1300 ecp_point_init( &R );
1301 mpi_init( &m );
1302
1303 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1304
1305 if( verbose != 0 )
1306 printf( " ECP test #1 (SPA resistance): " );
1307
1308 add_count = 0;
1309 dbl_count = 0;
1310 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1311 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1312
1313 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1314 {
1315 add_c_prev = add_count;
1316 dbl_c_prev = dbl_count;
1317 add_count = 0;
1318 dbl_count = 0;
1319
1320 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1321 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1322
1323 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1324 {
1325 if( verbose != 0 )
1326 printf( "failed (%zu)\n", i );
1327
1328 ret = 1;
1329 goto cleanup;
1330 }
1331 }
1332
1333 if( verbose != 0 )
1334 printf( "passed\n" );
1335
1336cleanup:
1337
1338 if( ret < 0 && verbose != 0 )
1339 printf( "Unexpected error, return code = %08X\n", ret );
1340
1341 ecp_group_free( &grp );
1342 ecp_point_free( &R );
1343 mpi_free( &m );
1344
1345 if( verbose != 0 )
1346 printf( "\n" );
1347
1348 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001349}
1350
1351#endif
1352
1353#endif