blob: 1a83ed0ce5df95a96aba6a0aa79a7648cc09dff5 [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
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200355#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100356/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100357 * 192 bits in terms of t_uint
358 */
359#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
360
361/*
362 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
363 * -1 means let this chunk be 0
364 * a positive value i means A_i.
365 */
366#define P192_CHUNKS 3
367#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
368#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
369
370const signed char p192_tbl[][P192_CHUNKS] = {
371 { -1, 3, 3 }, /* S1 */
372 { 4, 4, -1 }, /* S2 */
373 { 5, 5, 5 }, /* S3 */
374};
375
376/*
377 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
378 */
379static int ecp_mod_p192( mpi *N )
380{
381 int ret;
382 unsigned char i, j, offset;
383 signed char chunk;
384 mpi tmp, acc;
385 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
386
387 tmp.s = 1;
388 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
389 tmp.p = tmp_p;
390
391 acc.s = 1;
392 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
393 acc.p = acc_p;
394
395 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
396
397 /*
398 * acc = T
399 */
400 memset( acc_p, 0, sizeof( acc_p ) );
401 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
402
403 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
404 {
405 /*
406 * tmp = S_i
407 */
408 memset( tmp_p, 0, sizeof( tmp_p ) );
409 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
410 {
411 chunk = p192_tbl[i][j];
412 if( chunk >= 0 )
413 memcpy( tmp_p + offset * P192_CHUNK_INT,
414 N->p + chunk * P192_CHUNK_INT,
415 P192_CHUNK_CHAR );
416 }
417
418 /*
419 * acc += tmp
420 */
421 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
422 }
423
424 MPI_CHK( mpi_copy( N, &acc ) );
425
426cleanup:
427 return( ret );
428}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200429#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100430
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200431#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100432/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100433 * Size of p521 in terms of t_uint
434 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100435#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100436
437/*
438 * Bits to keep in the most significant t_uint
439 */
440#if defined(POLARSS_HAVE_INT8)
441#define P521_MASK 0x01
442#else
443#define P521_MASK 0x01FF
444#endif
445
446/*
447 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100448 */
449static int ecp_mod_p521( mpi *N )
450{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100451 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100452 t_uint Mp[P521_SIZE_INT];
453 mpi M;
454
455 if( N->n < P521_SIZE_INT )
456 return( 0 );
457
458 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
459 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
460 Mp[P521_SIZE_INT - 1] &= P521_MASK;
461
462 M.s = 1;
463 M.n = P521_SIZE_INT;
464 M.p = Mp;
465
466 MPI_CHK( mpi_shift_r( N, 521 ) );
467
468 MPI_CHK( mpi_add_abs( N, N, &M ) );
469
470cleanup:
471 return( ret );
472}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200473#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100474
475/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100476 * Domain parameters for secp192r1
477 */
478#define SECP192R1_P \
479 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
480#define SECP192R1_B \
481 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
482#define SECP192R1_GX \
483 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
484#define SECP192R1_GY \
485 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
486#define SECP192R1_N \
487 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
488
489/*
490 * Domain parameters for secp224r1
491 */
492#define SECP224R1_P \
493 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
494#define SECP224R1_B \
495 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
496#define SECP224R1_GX \
497 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
498#define SECP224R1_GY \
499 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
500#define SECP224R1_N \
501 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
502
503/*
504 * Domain parameters for secp256r1
505 */
506#define SECP256R1_P \
507 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
508#define SECP256R1_B \
509 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
510#define SECP256R1_GX \
511 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
512#define SECP256R1_GY \
513 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
514#define SECP256R1_N \
515 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
516
517/*
518 * Domain parameters for secp384r1
519 */
520#define SECP384R1_P \
521 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
522 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
523#define SECP384R1_B \
524 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
525 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
526#define SECP384R1_GX \
527 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
528 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
529#define SECP384R1_GY \
530 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
531 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
532#define SECP384R1_N \
533 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
534 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
535
536/*
537 * Domain parameters for secp521r1
538 */
539#define SECP521R1_P \
540 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
541 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
542 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
543#define SECP521R1_B \
544 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
545 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
546 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
547#define SECP521R1_GX \
548 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
549 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
550 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
551#define SECP521R1_GY \
552 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
553 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
554 "3FAD0761353C7086A272C24088BE94769FD16650"
555#define SECP521R1_N \
556 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
557 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
558 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
559
560/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100561 * Set a group using well-known domain parameters
562 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100563int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100564{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100565 grp->id = id;
566
567 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100568 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200569#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100570 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100571 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100572 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100573 SECP192R1_P, SECP192R1_B,
574 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200575#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100576
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200577#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100578 case POLARSSL_ECP_DP_SECP224R1:
579 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100580 SECP224R1_P, SECP224R1_B,
581 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200582#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100583
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200584#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100585 case POLARSSL_ECP_DP_SECP256R1:
586 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100587 SECP256R1_P, SECP256R1_B,
588 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200589#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100590
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200591#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100592 case POLARSSL_ECP_DP_SECP384R1:
593 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100594 SECP384R1_P, SECP384R1_B,
595 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200596#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100597
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200598#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100599 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100600 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100601 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100602 SECP521R1_P, SECP521R1_B,
603 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200604#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100605 }
606
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100607 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
608}
609
610/*
611 * Set a group from an ECParameters record (RFC 4492)
612 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100613int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100614{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100615 ecp_group_id id;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100616
617 /*
618 * We expect at least three bytes (see below)
619 */
620 if( len < 3 )
621 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
622
623 /*
624 * First byte is curve_type; only named_curve is handled
625 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100626 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100627 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
628
629 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100630 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100631 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100632 id = *(*buf)++;
633 id <<= 8;
634 id |= *(*buf)++;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100635 return ecp_use_known_dp( grp, id );
636}
637
638/*
639 * Write the ECParameters record corresponding to a group (RFC 4492)
640 */
641int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
642 unsigned char *buf, size_t blen )
643{
644 /*
645 * We are going to write 3 bytes (see below)
646 */
647 *olen = 3;
648 if( blen < *olen )
649 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
650
651 /*
652 * First byte is curve_type, always named_curve
653 */
654 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
655
656 /*
657 * Next two bytes are the namedcurve value
658 */
659 buf[0] = grp->id >> 8;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100660 buf[1] = grp->id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100661
662 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100663}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100664
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100665/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100666 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100667 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100668 * In order to guarantee that, we need to ensure that operands of
669 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100670 * bring the result back to this range.
671 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100672 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100673 */
674
675/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100676 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
677 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100678#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100679
680/*
681 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
682 */
683#define MOD_SUB( N ) \
684 while( mpi_cmp_int( &N, 0 ) < 0 ) \
685 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
686
687/*
688 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
689 */
690#define MOD_ADD( N ) \
691 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
692 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
693
694/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100695 * Check that a point is valid as a public key (SEC1 3.2.3.1)
696 */
697int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
698{
699 int ret;
700 mpi YY, RHS;
701
702 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
703 return( POLARSSL_ERR_ECP_GENERIC );
704
705 /*
706 * pt coordinates must be normalized for our checks
707 */
708 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
709 return( POLARSSL_ERR_ECP_GENERIC );
710
711 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
712 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
713 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
714 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
715 return( POLARSSL_ERR_ECP_GENERIC );
716
717 mpi_init( &YY ); mpi_init( &RHS );
718
719 /*
720 * YY = Y^2
721 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
722 */
723 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
724 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
725 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
726 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
727 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
728
729 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
730 ret = POLARSSL_ERR_ECP_GENERIC;
731
732cleanup:
733
734 mpi_free( &YY ); mpi_free( &RHS );
735
736 return( ret );
737}
738
739/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100740 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100741 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100742static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100743{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100744 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100745 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100746
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100747 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100748 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100749
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100750 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100751
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100752 /*
753 * X = X / Z^2 mod p
754 */
755 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
756 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
757 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100758
759 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100760 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100761 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100762 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
763 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100764
765 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100766 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100767 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100768 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100769
770cleanup:
771
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100772 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100773
774 return( ret );
775}
776
777/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100778 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100779 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100780 * (See for example Cohen's "A Course in Computational Algebraic Number
781 * Theory", Algorithm 10.3.4.)
782 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100783 * Warning: fails if one of the points is zero!
784 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100785 */
786static int ecp_normalize_many( const ecp_group *grp,
787 ecp_point T[], size_t t_len )
788{
789 int ret;
790 size_t i;
791 mpi *c, u, Zi, ZZi;
792
793 if( t_len < 2 )
794 return( ecp_normalize( grp, T ) );
795
796 if( ( c = (mpi *) malloc( t_len * sizeof( mpi ) ) ) == NULL )
797 return( POLARSSL_ERR_ECP_GENERIC );
798
799 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
800 for( i = 0; i < t_len; i++ )
801 mpi_init( &c[i] );
802
803 /*
804 * c[i] = Z_0 * ... * Z_i
805 */
806 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
807 for( i = 1; i < t_len; i++ )
808 {
809 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
810 MOD_MUL( c[i] );
811 }
812
813 /*
814 * u = 1 / (Z_0 * ... * Z_n) mod P
815 */
816 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
817
818 for( i = t_len - 1; ; i-- )
819 {
820 /*
821 * Zi = 1 / Z_i mod p
822 * u = 1 / (Z_0 * ... * Z_i) mod P
823 */
824 if( i == 0 ) {
825 MPI_CHK( mpi_copy( &Zi, &u ) );
826 }
827 else
828 {
829 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
830 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
831 }
832
833 /*
834 * proceed as in normalize()
835 */
836 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
837 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
838 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
839 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
840 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
841
842 if( i == 0 )
843 break;
844 }
845
846cleanup:
847
848 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
849 for( i = 0; i < t_len; i++ )
850 mpi_free( &c[i] );
851 free( c );
852
853 return( ret );
854}
855
856
857/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100858 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
859 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100860static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
861 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100862{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100863 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100864 mpi T1, T2, T3, X, Y, Z;
865
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100866#if defined(POLARSSL_SELF_TEST)
867 dbl_count++;
868#endif
869
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100870 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100871 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100872
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100873 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
874 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
875
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100876 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
877 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
878 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
879 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
880 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
881 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
882 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
883 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
884 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
885 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100886
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100887 /*
888 * For Y = Y / 2 mod p, we must make sure that Y is even before
889 * using right-shift. No need to reduce mod p afterwards.
890 */
891 if( mpi_get_bit( &Y, 0 ) == 1 )
892 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
893 MPI_CHK( mpi_shift_r( &Y, 1 ) );
894
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100895 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
896 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
897 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
898 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
899 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
900 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100901
902 MPI_CHK( mpi_copy( &R->X, &X ) );
903 MPI_CHK( mpi_copy( &R->Y, &Y ) );
904 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100905
906cleanup:
907
908 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
909 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
910
911 return( ret );
912}
913
914/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100915 * Addition or subtraction: R = P + Q or R = P + Q,
916 * mixed affine-Jacobian coordinates (GECC 3.22)
917 *
918 * The coordinates of Q must be normalized (= affine),
919 * but those of P don't need to. R is not normalized.
920 *
921 * If sign >= 0, perform addition, otherwise perform subtraction,
922 * taking advantage of the fact that, for Q != 0, we have
923 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100924 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100925static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100926 const ecp_point *P, const ecp_point *Q,
927 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100928{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100929 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100930 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100931
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100932#if defined(POLARSSL_SELF_TEST)
933 add_count++;
934#endif
935
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100936 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100937 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100938 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100939 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100940 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
941 return( ecp_copy( R, P ) );
942
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100943 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
944 {
945 ret = ecp_copy( R, Q );
946
947 /*
948 * -R.Y mod P = P - R.Y unless R.Y == 0
949 */
950 if( ret == 0 && sign < 0)
951 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
952 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
953
954 return( ret );
955 }
956
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100957 /*
958 * Make sure Q coordinates are normalized
959 */
960 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
961 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100962
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100963 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
964 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100965
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100966 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
967 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
968 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
969 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100970
971 /*
972 * For subtraction, -Q.Y should have been used instead of Q.Y,
973 * so we replace T2 by -T2, which is P - T2 mod P
974 */
975 if( sign < 0 )
976 {
977 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
978 MOD_SUB( T2 );
979 }
980
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100981 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
982 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100983
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100984 if( mpi_cmp_int( &T1, 0 ) == 0 )
985 {
986 if( mpi_cmp_int( &T2, 0 ) == 0 )
987 {
988 ret = ecp_double_jac( grp, R, P );
989 goto cleanup;
990 }
991 else
992 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100993 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100994 goto cleanup;
995 }
996 }
997
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100998 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
999 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1000 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1001 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1002 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1003 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1004 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1005 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1006 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1007 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1008 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1009 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001010
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001011 MPI_CHK( mpi_copy( &R->X, &X ) );
1012 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1013 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001014
1015cleanup:
1016
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001017 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1018 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001019
1020 return( ret );
1021}
1022
1023/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001024 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001025 */
1026int ecp_add( const ecp_group *grp, ecp_point *R,
1027 const ecp_point *P, const ecp_point *Q )
1028{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001029 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001030
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001031 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1032 MPI_CHK( ecp_normalize( grp, R ) );
1033
1034cleanup:
1035 return( ret );
1036}
1037
1038/*
1039 * Subtraction: R = P - Q, result's coordinates normalized
1040 */
1041int ecp_sub( const ecp_group *grp, ecp_point *R,
1042 const ecp_point *P, const ecp_point *Q )
1043{
1044 int ret;
1045
1046 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001047 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001048
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001049cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001050 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001051}
1052
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001053/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001054 * Compute a modified width-w non-adjacent form (NAF) of a number,
1055 * with a fixed pattern for resistance to SPA/timing attacks,
1056 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1057 * (The resulting multiplication algorithm can also been seen as a
1058 * modification of 2^w-ary multiplication, with signed coefficients,
1059 * all of them odd.)
1060 *
1061 * Input:
1062 * m must be an odd positive mpi less than w * k bits long
1063 * x must be an array of k elements
1064 * w must be less than a certain maximum (currently 8)
1065 *
1066 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1067 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1068 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1069 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1070 *
1071 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1072 * p. 335 of the cited reference, here we return only u, not d_w since
1073 * it is known that the other d_w[j] will be 0. Moreover, the returned
1074 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1075 * that u_i is odd. Also, since we always select a positive value for d
1076 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1077 * does. Finally, there is an off-by-one error in the reference: the
1078 * last index should be k-1, not k.
1079 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001080static int ecp_w_naf_fixed( signed char x[], size_t k,
1081 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001082{
1083 int ret;
1084 unsigned int i, u, mask, carry;
1085 mpi M;
1086
1087 mpi_init( &M );
1088
1089 MPI_CHK( mpi_copy( &M, m ) );
1090 mask = ( 1 << w ) - 1;
1091 carry = 1 << ( w - 1 );
1092
1093 for( i = 0; i < k; i++ )
1094 {
1095 u = M.p[0] & mask;
1096
1097 if( ( u & 1 ) == 0 && i > 0 )
1098 x[i - 1] -= carry;
1099
1100 x[i] = u >> 1;
1101 mpi_shift_r( &M, w );
1102 }
1103
1104 /*
1105 * We should have consumed all the bits now
1106 */
1107 if( mpi_cmp_int( &M, 0 ) != 0 )
1108 ret = POLARSSL_ERR_ECP_GENERIC;
1109
1110cleanup:
1111
1112 mpi_free( &M );
1113
1114 return( ret );
1115}
1116
1117/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001118 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1119 * The table is filled with T[i] = (2 * i + 1) P.
1120 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001121static int ecp_precompute( const ecp_group *grp,
1122 ecp_point T[], size_t t_len,
1123 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001124{
1125 int ret;
1126 size_t i;
1127 ecp_point PP;
1128
1129 ecp_point_init( &PP );
1130
1131 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1132
1133 MPI_CHK( ecp_copy( &T[0], P ) );
1134
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001135 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001136 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1137
1138 /*
1139 * T[0] = P already has normalized coordinates
1140 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001141 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001142
1143cleanup:
1144
1145 ecp_point_free( &PP );
1146
1147 return( ret );
1148}
1149
1150/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001151 * Maximum length of the precomputed table
1152 */
1153#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1154
1155/*
1156 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1157 * (that is: grp->nbits / w + 1)
1158 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1159 */
1160#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1161
1162/*
1163 * Integer multiplication: R = m * P
1164 *
1165 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1166 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1167 *
1168 * This function executes a fixed number of operations for
1169 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001170 */
1171int ecp_mul( const ecp_group *grp, ecp_point *R,
1172 const mpi *m, const ecp_point *P )
1173{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001174 int ret;
1175 unsigned char w, m_is_odd;
1176 size_t pre_len, naf_len, i, j;
1177 signed char naf[ MAX_NAF_LEN ];
1178 ecp_point Q, T[ MAX_PRE_LEN ];
1179 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001180
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001181 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001182 return( POLARSSL_ERR_ECP_GENERIC );
1183
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001184 w = grp->nbits >= 521 ? 6 :
1185 grp->nbits >= 224 ? 5 :
1186 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001187
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001188 /*
1189 * Make sure w is within the limits.
1190 * The last test ensures that none of the precomputed points is zero,
1191 * which wouldn't be handled correctly by ecp_normalize_many().
1192 * It is only useful for small curves, as used in the test suite.
1193 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001194 if( w > POLARSSL_ECP_WINDOW_SIZE )
1195 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001196 if( w < 2 || w >= grp->nbits )
1197 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001198
1199 pre_len = 1 << ( w - 1 );
1200 naf_len = grp->nbits / w + 1;
1201
1202 mpi_init( &M );
1203 ecp_point_init( &Q );
1204 for( i = 0; i < pre_len; i++ )
1205 ecp_point_init( &T[i] );
1206
1207 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1208
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001209 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001210 * Make sure M is odd:
1211 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001212 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001213 MPI_CHK( mpi_copy( &M, m ) );
1214 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001215
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001216 /*
1217 * Compute the fixed-pattern NAF and precompute odd multiples
1218 */
1219 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1220 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001221
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001222 /*
1223 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1224 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1225 *
1226 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1227 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1228 * == T[ - naf[i] - 1 ]
1229 */
1230 MPI_CHK( ecp_set_zero( &Q ) );
1231 i = naf_len - 1;
1232 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001233 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001234 if( naf[i] < 0 )
1235 {
1236 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1237 }
1238 else
1239 {
1240 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1241 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001242
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001243 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001244 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001245 i--;
1246
1247 for( j = 0; j < w; j++ )
1248 {
1249 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1250 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001251 }
1252
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001253 /*
1254 * Now get m * P from M * P.
1255 * Since we don't need T[] any more, we can recycle it:
1256 * we already have T[0] = P, now set T[1] = 2 * P.
1257 */
1258 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1259 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001260
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001261
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001262cleanup:
1263
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001264 mpi_free( &M );
1265 ecp_point_free( &Q );
1266 for( i = 0; i < pre_len; i++ )
1267 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001268
1269 return( ret );
1270}
1271
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001272/*
1273 * Generate a keypair (SEC1 3.2.1)
1274 */
1275int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1276 int (*f_rng)(void *, unsigned char *, size_t),
1277 void *p_rng )
1278{
1279 int count = 0;
1280 size_t n_size = (grp->nbits + 7) / 8;
1281
1282 /*
1283 * Generate d such that 1 <= n < N
1284 */
1285 do
1286 {
1287 mpi_fill_random( d, n_size, f_rng, p_rng );
1288
1289 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1290 mpi_shift_r( d, 1 );
1291
1292 if( count++ > 10 )
1293 return( POLARSSL_ERR_ECP_GENERIC );
1294 }
1295 while( mpi_cmp_int( d, 1 ) < 0 );
1296
1297 return( ecp_mul( grp, Q, d, &grp->G ) );
1298}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001299
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001300#if defined(POLARSSL_SELF_TEST)
1301
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001302/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001303 * Checkup routine
1304 */
1305int ecp_self_test( int verbose )
1306{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001307 int ret;
1308 size_t i;
1309 ecp_group grp;
1310 ecp_point R;
1311 mpi m;
1312 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001313 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001314 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001315 "000000000000000000000000000000000000000000000000", /* zero */
1316 "000000000000000000000000000000000000000000000001", /* one */
1317 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1318 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001319 "400000000000000000000000000000000000000000000000",
1320 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1321 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001322 };
1323
1324 ecp_group_init( &grp );
1325 ecp_point_init( &R );
1326 mpi_init( &m );
1327
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001328#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001329 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001330#else
1331#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1332 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1333#else
1334#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1335 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1336#else
1337#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1338 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1339#else
1340#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1341 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1342#else
1343#error No curves defines
1344#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1345#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1346#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1347#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1348#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001349
1350 if( verbose != 0 )
1351 printf( " ECP test #1 (SPA resistance): " );
1352
1353 add_count = 0;
1354 dbl_count = 0;
1355 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1356 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1357
1358 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1359 {
1360 add_c_prev = add_count;
1361 dbl_c_prev = dbl_count;
1362 add_count = 0;
1363 dbl_count = 0;
1364
1365 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1366 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1367
1368 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1369 {
1370 if( verbose != 0 )
1371 printf( "failed (%zu)\n", i );
1372
1373 ret = 1;
1374 goto cleanup;
1375 }
1376 }
1377
1378 if( verbose != 0 )
1379 printf( "passed\n" );
1380
1381cleanup:
1382
1383 if( ret < 0 && verbose != 0 )
1384 printf( "Unexpected error, return code = %08X\n", ret );
1385
1386 ecp_group_free( &grp );
1387 ecp_point_free( &R );
1388 mpi_free( &m );
1389
1390 if( verbose != 0 )
1391 printf( "\n" );
1392
1393 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001394}
1395
1396#endif
1397
1398#endif