blob: f14b0b42e8cdaa10eb571c5000f27d46c6a24927 [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{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200194 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100195 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,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100274 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100275{
276 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100277 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100278
279 /*
280 * We must have at least two bytes (1 for length, at least of for data)
281 */
282 if( buf_len < 2 )
283 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
284
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100285 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100286 if( data_len < 1 || data_len > buf_len - 1 )
287 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
288
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100289 /*
290 * Save buffer start for read_binary and update buf
291 */
292 buf_start = *buf;
293 *buf += data_len;
294
295 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100296}
297
298/*
299 * Export a point as a TLS ECPoint record (RFC 4492)
300 * struct {
301 * opaque point <1..2^8-1>;
302 * } ECPoint;
303 */
304int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100305 int format, size_t *olen,
306 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100307{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100308 int ret;
309
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100310 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100311 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100312 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100313 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100314 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
315
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100316 if( ( ret = ecp_point_write_binary( grp, pt, format,
317 olen, buf + 1, blen - 1) ) != 0 )
318 return( ret );
319
320 /*
321 * write length to the first byte and update total length
322 */
323 buf[0] = *olen;
324 ++*olen;
325
326 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100327}
328
329/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100330 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
331 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100332 */
333static int ecp_modp( mpi *N, const ecp_group *grp )
334{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100335 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100336
337 if( grp->modp == NULL )
338 return( mpi_mod_mpi( N, N, &grp->P ) );
339
340 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
341 return( POLARSSL_ERR_ECP_GENERIC );
342
343 MPI_CHK( grp->modp( N ) );
344
345 while( mpi_cmp_int( N, 0 ) < 0 )
346 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
347
348 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
349 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
350
351cleanup:
352 return( ret );
353}
354
355/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100356 * 192 bits in terms of t_uint
357 */
358#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
359
360/*
361 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
362 * -1 means let this chunk be 0
363 * a positive value i means A_i.
364 */
365#define P192_CHUNKS 3
366#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
367#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
368
369const signed char p192_tbl[][P192_CHUNKS] = {
370 { -1, 3, 3 }, /* S1 */
371 { 4, 4, -1 }, /* S2 */
372 { 5, 5, 5 }, /* S3 */
373};
374
375/*
376 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
377 */
378static int ecp_mod_p192( mpi *N )
379{
380 int ret;
381 unsigned char i, j, offset;
382 signed char chunk;
383 mpi tmp, acc;
384 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
385
386 tmp.s = 1;
387 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
388 tmp.p = tmp_p;
389
390 acc.s = 1;
391 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
392 acc.p = acc_p;
393
394 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
395
396 /*
397 * acc = T
398 */
399 memset( acc_p, 0, sizeof( acc_p ) );
400 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
401
402 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
403 {
404 /*
405 * tmp = S_i
406 */
407 memset( tmp_p, 0, sizeof( tmp_p ) );
408 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
409 {
410 chunk = p192_tbl[i][j];
411 if( chunk >= 0 )
412 memcpy( tmp_p + offset * P192_CHUNK_INT,
413 N->p + chunk * P192_CHUNK_INT,
414 P192_CHUNK_CHAR );
415 }
416
417 /*
418 * acc += tmp
419 */
420 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
421 }
422
423 MPI_CHK( mpi_copy( N, &acc ) );
424
425cleanup:
426 return( ret );
427}
428
429/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100430 * Size of p521 in terms of t_uint
431 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100432#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100433
434/*
435 * Bits to keep in the most significant t_uint
436 */
437#if defined(POLARSS_HAVE_INT8)
438#define P521_MASK 0x01
439#else
440#define P521_MASK 0x01FF
441#endif
442
443/*
444 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100445 */
446static int ecp_mod_p521( mpi *N )
447{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100448 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100449 t_uint Mp[P521_SIZE_INT];
450 mpi M;
451
452 if( N->n < P521_SIZE_INT )
453 return( 0 );
454
455 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
456 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
457 Mp[P521_SIZE_INT - 1] &= P521_MASK;
458
459 M.s = 1;
460 M.n = P521_SIZE_INT;
461 M.p = Mp;
462
463 MPI_CHK( mpi_shift_r( N, 521 ) );
464
465 MPI_CHK( mpi_add_abs( N, N, &M ) );
466
467cleanup:
468 return( ret );
469}
470
471/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100472 * Domain parameters for secp192r1
473 */
474#define SECP192R1_P \
475 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
476#define SECP192R1_B \
477 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
478#define SECP192R1_GX \
479 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
480#define SECP192R1_GY \
481 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
482#define SECP192R1_N \
483 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
484
485/*
486 * Domain parameters for secp224r1
487 */
488#define SECP224R1_P \
489 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
490#define SECP224R1_B \
491 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
492#define SECP224R1_GX \
493 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
494#define SECP224R1_GY \
495 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
496#define SECP224R1_N \
497 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
498
499/*
500 * Domain parameters for secp256r1
501 */
502#define SECP256R1_P \
503 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
504#define SECP256R1_B \
505 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
506#define SECP256R1_GX \
507 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
508#define SECP256R1_GY \
509 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
510#define SECP256R1_N \
511 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
512
513/*
514 * Domain parameters for secp384r1
515 */
516#define SECP384R1_P \
517 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
518 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
519#define SECP384R1_B \
520 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
521 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
522#define SECP384R1_GX \
523 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
524 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
525#define SECP384R1_GY \
526 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
527 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
528#define SECP384R1_N \
529 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
530 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
531
532/*
533 * Domain parameters for secp521r1
534 */
535#define SECP521R1_P \
536 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
537 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
538 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
539#define SECP521R1_B \
540 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
541 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
542 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
543#define SECP521R1_GX \
544 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
545 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
546 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
547#define SECP521R1_GY \
548 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
549 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
550 "3FAD0761353C7086A272C24088BE94769FD16650"
551#define SECP521R1_N \
552 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
553 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
554 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
555
556/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100557 * Set a group using well-known domain parameters
558 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100559int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100560{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100561 grp->id = id;
562
563 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100564 {
565 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100566 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100567 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100568 SECP192R1_P, SECP192R1_B,
569 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
570
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100571 case POLARSSL_ECP_DP_SECP224R1:
572 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100573 SECP224R1_P, SECP224R1_B,
574 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
575
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100576 case POLARSSL_ECP_DP_SECP256R1:
577 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100578 SECP256R1_P, SECP256R1_B,
579 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
580
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100581 case POLARSSL_ECP_DP_SECP384R1:
582 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100583 SECP384R1_P, SECP384R1_B,
584 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
585
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100586 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100587 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100588 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100589 SECP521R1_P, SECP521R1_B,
590 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100591 }
592
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100593 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
594}
595
596/*
597 * Set a group from an ECParameters record (RFC 4492)
598 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100599int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100600{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100601 ecp_group_id id;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100602
603 /*
604 * We expect at least three bytes (see below)
605 */
606 if( len < 3 )
607 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
608
609 /*
610 * First byte is curve_type; only named_curve is handled
611 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100612 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100613 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
614
615 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100616 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100617 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100618 id = *(*buf)++;
619 id <<= 8;
620 id |= *(*buf)++;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100621 return ecp_use_known_dp( grp, id );
622}
623
624/*
625 * Write the ECParameters record corresponding to a group (RFC 4492)
626 */
627int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
628 unsigned char *buf, size_t blen )
629{
630 /*
631 * We are going to write 3 bytes (see below)
632 */
633 *olen = 3;
634 if( blen < *olen )
635 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
636
637 /*
638 * First byte is curve_type, always named_curve
639 */
640 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
641
642 /*
643 * Next two bytes are the namedcurve value
644 */
645 buf[0] = grp->id >> 8;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100646 buf[1] = grp->id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100647
648 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100649}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100650
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100651/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100652 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100653 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100654 * In order to guarantee that, we need to ensure that operands of
655 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100656 * bring the result back to this range.
657 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100658 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100659 */
660
661/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100662 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
663 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100664#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100665
666/*
667 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
668 */
669#define MOD_SUB( N ) \
670 while( mpi_cmp_int( &N, 0 ) < 0 ) \
671 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
672
673/*
674 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
675 */
676#define MOD_ADD( N ) \
677 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
678 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
679
680/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100681 * Check that a point is valid as a public key (SEC1 3.2.3.1)
682 */
683int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
684{
685 int ret;
686 mpi YY, RHS;
687
688 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
689 return( POLARSSL_ERR_ECP_GENERIC );
690
691 /*
692 * pt coordinates must be normalized for our checks
693 */
694 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
695 return( POLARSSL_ERR_ECP_GENERIC );
696
697 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
698 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
699 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
700 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
701 return( POLARSSL_ERR_ECP_GENERIC );
702
703 mpi_init( &YY ); mpi_init( &RHS );
704
705 /*
706 * YY = Y^2
707 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
708 */
709 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
710 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
711 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
712 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
713 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
714
715 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
716 ret = POLARSSL_ERR_ECP_GENERIC;
717
718cleanup:
719
720 mpi_free( &YY ); mpi_free( &RHS );
721
722 return( ret );
723}
724
725/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100726 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100727 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100728static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100729{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100730 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100731 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100732
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100733 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100734 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100735
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100736 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100737
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100738 /*
739 * X = X / Z^2 mod p
740 */
741 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
742 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
743 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100744
745 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100746 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100747 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100748 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
749 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100750
751 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100752 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100753 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100754 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100755
756cleanup:
757
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100758 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100759
760 return( ret );
761}
762
763/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100764 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100765 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100766 * (See for example Cohen's "A Course in Computational Algebraic Number
767 * Theory", Algorithm 10.3.4.)
768 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100769 * Warning: fails if one of the points is zero!
770 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100771 */
772static int ecp_normalize_many( const ecp_group *grp,
773 ecp_point T[], size_t t_len )
774{
775 int ret;
776 size_t i;
777 mpi *c, u, Zi, ZZi;
778
779 if( t_len < 2 )
780 return( ecp_normalize( grp, T ) );
781
782 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
783 return( POLARSSL_ERR_ECP_GENERIC );
784
785 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
786 for( i = 0; i < t_len; i++ )
787 mpi_init( &c[i] );
788
789 /*
790 * c[i] = Z_0 * ... * Z_i
791 */
792 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
793 for( i = 1; i < t_len; i++ )
794 {
795 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
796 MOD_MUL( c[i] );
797 }
798
799 /*
800 * u = 1 / (Z_0 * ... * Z_n) mod P
801 */
802 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
803
804 for( i = t_len - 1; ; i-- )
805 {
806 /*
807 * Zi = 1 / Z_i mod p
808 * u = 1 / (Z_0 * ... * Z_i) mod P
809 */
810 if( i == 0 ) {
811 MPI_CHK( mpi_copy( &Zi, &u ) );
812 }
813 else
814 {
815 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
816 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
817 }
818
819 /*
820 * proceed as in normalize()
821 */
822 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
823 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
824 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
825 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
826 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
827
828 if( i == 0 )
829 break;
830 }
831
832cleanup:
833
834 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
835 for( i = 0; i < t_len; i++ )
836 mpi_free( &c[i] );
837 free( c );
838
839 return( ret );
840}
841
842
843/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100844 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
845 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100846static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
847 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100848{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100849 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100850 mpi T1, T2, T3, X, Y, Z;
851
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100852#if defined(POLARSSL_SELF_TEST)
853 dbl_count++;
854#endif
855
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100856 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100857 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100858
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100859 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
860 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
861
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100862 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
863 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
864 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
865 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
866 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
867 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
868 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
869 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
870 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
871 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100872
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100873 /*
874 * For Y = Y / 2 mod p, we must make sure that Y is even before
875 * using right-shift. No need to reduce mod p afterwards.
876 */
877 if( mpi_get_bit( &Y, 0 ) == 1 )
878 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
879 MPI_CHK( mpi_shift_r( &Y, 1 ) );
880
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100881 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
882 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
883 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
884 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
885 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
886 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100887
888 MPI_CHK( mpi_copy( &R->X, &X ) );
889 MPI_CHK( mpi_copy( &R->Y, &Y ) );
890 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100891
892cleanup:
893
894 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
895 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
896
897 return( ret );
898}
899
900/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100901 * Addition or subtraction: R = P + Q or R = P + Q,
902 * mixed affine-Jacobian coordinates (GECC 3.22)
903 *
904 * The coordinates of Q must be normalized (= affine),
905 * but those of P don't need to. R is not normalized.
906 *
907 * If sign >= 0, perform addition, otherwise perform subtraction,
908 * taking advantage of the fact that, for Q != 0, we have
909 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100910 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100911static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100912 const ecp_point *P, const ecp_point *Q,
913 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100914{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100915 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100916 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100917
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100918#if defined(POLARSSL_SELF_TEST)
919 add_count++;
920#endif
921
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100922 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100923 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100924 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100925 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100926 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
927 return( ecp_copy( R, P ) );
928
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100929 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
930 {
931 ret = ecp_copy( R, Q );
932
933 /*
934 * -R.Y mod P = P - R.Y unless R.Y == 0
935 */
936 if( ret == 0 && sign < 0)
937 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
938 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
939
940 return( ret );
941 }
942
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100943 /*
944 * Make sure Q coordinates are normalized
945 */
946 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
947 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100948
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100949 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
950 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100951
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100952 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
953 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
954 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
955 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100956
957 /*
958 * For subtraction, -Q.Y should have been used instead of Q.Y,
959 * so we replace T2 by -T2, which is P - T2 mod P
960 */
961 if( sign < 0 )
962 {
963 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
964 MOD_SUB( T2 );
965 }
966
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100967 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
968 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100969
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100970 if( mpi_cmp_int( &T1, 0 ) == 0 )
971 {
972 if( mpi_cmp_int( &T2, 0 ) == 0 )
973 {
974 ret = ecp_double_jac( grp, R, P );
975 goto cleanup;
976 }
977 else
978 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100979 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100980 goto cleanup;
981 }
982 }
983
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100984 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
985 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
986 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
987 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
988 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
989 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
990 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
991 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
992 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
993 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
994 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
995 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100996
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100997 MPI_CHK( mpi_copy( &R->X, &X ) );
998 MPI_CHK( mpi_copy( &R->Y, &Y ) );
999 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001000
1001cleanup:
1002
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001003 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1004 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001005
1006 return( ret );
1007}
1008
1009/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001010 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001011 */
1012int ecp_add( const ecp_group *grp, ecp_point *R,
1013 const ecp_point *P, const ecp_point *Q )
1014{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001015 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001016
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001017 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1018 MPI_CHK( ecp_normalize( grp, R ) );
1019
1020cleanup:
1021 return( ret );
1022}
1023
1024/*
1025 * Subtraction: R = P - Q, result's coordinates normalized
1026 */
1027int ecp_sub( const ecp_group *grp, ecp_point *R,
1028 const ecp_point *P, const ecp_point *Q )
1029{
1030 int ret;
1031
1032 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001033 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001034
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001035cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001036 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001037}
1038
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001039/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001040 * Compute a modified width-w non-adjacent form (NAF) of a number,
1041 * with a fixed pattern for resistance to SPA/timing attacks,
1042 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1043 * (The resulting multiplication algorithm can also been seen as a
1044 * modification of 2^w-ary multiplication, with signed coefficients,
1045 * all of them odd.)
1046 *
1047 * Input:
1048 * m must be an odd positive mpi less than w * k bits long
1049 * x must be an array of k elements
1050 * w must be less than a certain maximum (currently 8)
1051 *
1052 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1053 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1054 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1055 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1056 *
1057 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1058 * p. 335 of the cited reference, here we return only u, not d_w since
1059 * it is known that the other d_w[j] will be 0. Moreover, the returned
1060 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1061 * that u_i is odd. Also, since we always select a positive value for d
1062 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1063 * does. Finally, there is an off-by-one error in the reference: the
1064 * last index should be k-1, not k.
1065 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001066static int ecp_w_naf_fixed( signed char x[], size_t k,
1067 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001068{
1069 int ret;
1070 unsigned int i, u, mask, carry;
1071 mpi M;
1072
1073 mpi_init( &M );
1074
1075 MPI_CHK( mpi_copy( &M, m ) );
1076 mask = ( 1 << w ) - 1;
1077 carry = 1 << ( w - 1 );
1078
1079 for( i = 0; i < k; i++ )
1080 {
1081 u = M.p[0] & mask;
1082
1083 if( ( u & 1 ) == 0 && i > 0 )
1084 x[i - 1] -= carry;
1085
1086 x[i] = u >> 1;
1087 mpi_shift_r( &M, w );
1088 }
1089
1090 /*
1091 * We should have consumed all the bits now
1092 */
1093 if( mpi_cmp_int( &M, 0 ) != 0 )
1094 ret = POLARSSL_ERR_ECP_GENERIC;
1095
1096cleanup:
1097
1098 mpi_free( &M );
1099
1100 return( ret );
1101}
1102
1103/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001104 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1105 * The table is filled with T[i] = (2 * i + 1) P.
1106 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001107static int ecp_precompute( const ecp_group *grp,
1108 ecp_point T[], size_t t_len,
1109 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001110{
1111 int ret;
1112 size_t i;
1113 ecp_point PP;
1114
1115 ecp_point_init( &PP );
1116
1117 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1118
1119 MPI_CHK( ecp_copy( &T[0], P ) );
1120
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001121 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001122 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1123
1124 /*
1125 * T[0] = P already has normalized coordinates
1126 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001127 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001128
1129cleanup:
1130
1131 ecp_point_free( &PP );
1132
1133 return( ret );
1134}
1135
1136/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001137 * Maximum length of the precomputed table
1138 */
1139#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1140
1141/*
1142 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1143 * (that is: grp->nbits / w + 1)
1144 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1145 */
1146#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1147
1148/*
1149 * Integer multiplication: R = m * P
1150 *
1151 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1152 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1153 *
1154 * This function executes a fixed number of operations for
1155 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001156 */
1157int ecp_mul( const ecp_group *grp, ecp_point *R,
1158 const mpi *m, const ecp_point *P )
1159{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001160 int ret;
1161 unsigned char w, m_is_odd;
1162 size_t pre_len, naf_len, i, j;
1163 signed char naf[ MAX_NAF_LEN ];
1164 ecp_point Q, T[ MAX_PRE_LEN ];
1165 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001166
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001167 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001168 return( POLARSSL_ERR_ECP_GENERIC );
1169
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001170 w = grp->nbits >= 521 ? 6 :
1171 grp->nbits >= 224 ? 5 :
1172 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001173
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001174 /*
1175 * Make sure w is within the limits.
1176 * The last test ensures that none of the precomputed points is zero,
1177 * which wouldn't be handled correctly by ecp_normalize_many().
1178 * It is only useful for small curves, as used in the test suite.
1179 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001180 if( w > POLARSSL_ECP_WINDOW_SIZE )
1181 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001182 if( w < 2 || w >= grp->nbits )
1183 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001184
1185 pre_len = 1 << ( w - 1 );
1186 naf_len = grp->nbits / w + 1;
1187
1188 mpi_init( &M );
1189 ecp_point_init( &Q );
1190 for( i = 0; i < pre_len; i++ )
1191 ecp_point_init( &T[i] );
1192
1193 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1194
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001195 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001196 * Make sure M is odd:
1197 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001198 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001199 MPI_CHK( mpi_copy( &M, m ) );
1200 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001201
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001202 /*
1203 * Compute the fixed-pattern NAF and precompute odd multiples
1204 */
1205 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1206 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001207
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001208 /*
1209 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1210 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1211 *
1212 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1213 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1214 * == T[ - naf[i] - 1 ]
1215 */
1216 MPI_CHK( ecp_set_zero( &Q ) );
1217 i = naf_len - 1;
1218 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001219 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001220 if( naf[i] < 0 )
1221 {
1222 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1223 }
1224 else
1225 {
1226 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1227 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001228
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001229 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001230 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001231 i--;
1232
1233 for( j = 0; j < w; j++ )
1234 {
1235 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1236 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001237 }
1238
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001239 /*
1240 * Now get m * P from M * P.
1241 * Since we don't need T[] any more, we can recycle it:
1242 * we already have T[0] = P, now set T[1] = 2 * P.
1243 */
1244 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1245 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001246
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001247
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001248cleanup:
1249
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001250 mpi_free( &M );
1251 ecp_point_free( &Q );
1252 for( i = 0; i < pre_len; i++ )
1253 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001254
1255 return( ret );
1256}
1257
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001258/*
1259 * Generate a keypair (SEC1 3.2.1)
1260 */
1261int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1262 int (*f_rng)(void *, unsigned char *, size_t),
1263 void *p_rng )
1264{
1265 int count = 0;
1266 size_t n_size = (grp->nbits + 7) / 8;
1267
1268 /*
1269 * Generate d such that 1 <= n < N
1270 */
1271 do
1272 {
1273 mpi_fill_random( d, n_size, f_rng, p_rng );
1274
1275 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1276 mpi_shift_r( d, 1 );
1277
1278 if( count++ > 10 )
1279 return( POLARSSL_ERR_ECP_GENERIC );
1280 }
1281 while( mpi_cmp_int( d, 1 ) < 0 );
1282
1283 return( ecp_mul( grp, Q, d, &grp->G ) );
1284}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001285
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001286#if defined(POLARSSL_SELF_TEST)
1287
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001288/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001289 * Checkup routine
1290 */
1291int ecp_self_test( int verbose )
1292{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001293 int ret;
1294 size_t i;
1295 ecp_group grp;
1296 ecp_point R;
1297 mpi m;
1298 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001299 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001300 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001301 "000000000000000000000000000000000000000000000000", /* zero */
1302 "000000000000000000000000000000000000000000000001", /* one */
1303 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1304 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001305 "400000000000000000000000000000000000000000000000",
1306 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1307 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001308 };
1309
1310 ecp_group_init( &grp );
1311 ecp_point_init( &R );
1312 mpi_init( &m );
1313
1314 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
1315
1316 if( verbose != 0 )
1317 printf( " ECP test #1 (SPA resistance): " );
1318
1319 add_count = 0;
1320 dbl_count = 0;
1321 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1322 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1323
1324 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1325 {
1326 add_c_prev = add_count;
1327 dbl_c_prev = dbl_count;
1328 add_count = 0;
1329 dbl_count = 0;
1330
1331 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1332 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1333
1334 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1335 {
1336 if( verbose != 0 )
1337 printf( "failed (%zu)\n", i );
1338
1339 ret = 1;
1340 goto cleanup;
1341 }
1342 }
1343
1344 if( verbose != 0 )
1345 printf( "passed\n" );
1346
1347cleanup:
1348
1349 if( ret < 0 && verbose != 0 )
1350 printf( "Unexpected error, return code = %08X\n", ret );
1351
1352 ecp_group_free( &grp );
1353 ecp_point_free( &R );
1354 mpi_free( &m );
1355
1356 if( verbose != 0 )
1357 printf( "\n" );
1358
1359 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001360}
1361
1362#endif
1363
1364#endif