blob: af18e5bee7477643d3ec24510a1f984d09774796 [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"
Paul Bakker6e339b52013-07-03 13:37:05 +020040
41#if defined(POLARSSL_MEMORY_C)
42#include "polarssl/memory.h"
43#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010048#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010049#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010050
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010051#if defined(POLARSSL_SELF_TEST)
52/*
53 * Counts of point addition and doubling operations.
54 * Used to test resistance of point multiplication to SPA/timing attacks.
55 */
56unsigned long add_count, dbl_count;
57#endif
58
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010059/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010060 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010061 */
62void ecp_point_init( ecp_point *pt )
63{
64 if( pt == NULL )
65 return;
66
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010067 mpi_init( &pt->X );
68 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010069 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010070}
71
72/*
73 * Initialize (the components of) a group
74 */
75void ecp_group_init( ecp_group *grp )
76{
77 if( grp == NULL )
78 return;
79
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +010080 grp->id = 0;
81
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010082 mpi_init( &grp->P );
83 mpi_init( &grp->B );
84 ecp_point_init( &grp->G );
85 mpi_init( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010086
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010087 grp->pbits = 0;
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010088 grp->nbits = 0;
89
90 grp->modp = NULL;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010091}
92
93/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010094 * Unallocate (the components of) a point
95 */
96void ecp_point_free( ecp_point *pt )
97{
98 if( pt == NULL )
99 return;
100
101 mpi_free( &( pt->X ) );
102 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100103 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100104}
105
106/*
107 * Unallocate (the components of) a group
108 */
109void ecp_group_free( ecp_group *grp )
110{
111 if( grp == NULL )
112 return;
113
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100114 mpi_free( &grp->P );
115 mpi_free( &grp->B );
116 ecp_point_free( &grp->G );
117 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100118}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100119
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100120/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100121 * Set point to zero
122 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100123int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100124{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100125 int ret;
126
127 MPI_CHK( mpi_lset( &pt->X , 1 ) );
128 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
129 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
130
131cleanup:
132 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100133}
134
135/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100136 * Tell if a point is zero
137 */
138int ecp_is_zero( ecp_point *pt )
139{
140 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
141}
142
143/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100144 * Copy the contents of Q into P
145 */
146int ecp_copy( ecp_point *P, const ecp_point *Q )
147{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100148 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100149
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100150 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
151 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100152 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100153
154cleanup:
155 return( ret );
156}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100157
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100158/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100159 * Import a non-zero point from ASCII strings
160 */
161int ecp_point_read_string( ecp_point *P, int radix,
162 const char *x, const char *y )
163{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100164 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100165
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100166 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
167 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100168 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100169
170cleanup:
171 return( ret );
172}
173
174/*
175 * Import an ECP group from ASCII strings
176 */
177int ecp_group_read_string( ecp_group *grp, int radix,
178 const char *p, const char *b,
179 const char *gx, const char *gy, const char *n)
180{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100181 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100182
183 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
184 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
185 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
186 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
187
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100188 grp->pbits = mpi_msb( &grp->P );
189 grp->nbits = mpi_msb( &grp->N );
190
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100191cleanup:
192 return( ret );
193}
194
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100195/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100196 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100197 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100198int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100199 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100200 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100201{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200202 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100203 size_t plen;
204
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100205 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
206 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100207 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100208
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100209 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100210 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100211 */
212 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
213 {
214 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100215 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100216
217 buf[0] = 0x00;
218 *olen = 1;
219
220 return( 0 );
221 }
222
223 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100224
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100225 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
226 {
227 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100228
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100229 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100230 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100231
232 buf[0] = 0x04;
233 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
234 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
235 }
236 else if( format == POLARSSL_ECP_PF_COMPRESSED )
237 {
238 *olen = plen + 1;
239
240 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100241 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100242
243 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
244 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
245 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100246
247cleanup:
248 return( ret );
249}
250
251/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100252 * Import a point from unsigned binary data (SEC1 2.3.4)
253 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100254int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
255 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100256 int ret;
257 size_t plen;
258
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100259 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100260 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100261
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100262 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100263
264 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100265 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100266
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100267 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
268 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
269 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100270
271cleanup:
272 return( ret );
273}
274
275/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100276 * Import a point from a TLS ECPoint record (RFC 4492)
277 * struct {
278 * opaque point <1..2^8-1>;
279 * } ECPoint;
280 */
281int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100282 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100283{
284 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100285 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100286
287 /*
288 * We must have at least two bytes (1 for length, at least of for data)
289 */
290 if( buf_len < 2 )
291 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
292
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100293 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100294 if( data_len < 1 || data_len > buf_len - 1 )
295 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
296
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100297 /*
298 * Save buffer start for read_binary and update buf
299 */
300 buf_start = *buf;
301 *buf += data_len;
302
303 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100304}
305
306/*
307 * Export a point as a TLS ECPoint record (RFC 4492)
308 * struct {
309 * opaque point <1..2^8-1>;
310 * } ECPoint;
311 */
312int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100313 int format, size_t *olen,
314 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100315{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100316 int ret;
317
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100318 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100319 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100320 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100321 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100322 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
323
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100324 if( ( ret = ecp_point_write_binary( grp, pt, format,
325 olen, buf + 1, blen - 1) ) != 0 )
326 return( ret );
327
328 /*
329 * write length to the first byte and update total length
330 */
331 buf[0] = *olen;
332 ++*olen;
333
334 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100335}
336
337/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100338 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
339 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100340 */
341static int ecp_modp( mpi *N, const ecp_group *grp )
342{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100343 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100344
345 if( grp->modp == NULL )
346 return( mpi_mod_mpi( N, N, &grp->P ) );
347
348 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
349 return( POLARSSL_ERR_ECP_GENERIC );
350
351 MPI_CHK( grp->modp( N ) );
352
353 while( mpi_cmp_int( N, 0 ) < 0 )
354 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
355
356 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
357 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
358
359cleanup:
360 return( ret );
361}
362
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200363#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100364/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100365 * 192 bits in terms of t_uint
366 */
367#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
368
369/*
370 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
371 * -1 means let this chunk be 0
372 * a positive value i means A_i.
373 */
374#define P192_CHUNKS 3
375#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
376#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
377
378const signed char p192_tbl[][P192_CHUNKS] = {
379 { -1, 3, 3 }, /* S1 */
380 { 4, 4, -1 }, /* S2 */
381 { 5, 5, 5 }, /* S3 */
382};
383
384/*
385 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
386 */
387static int ecp_mod_p192( mpi *N )
388{
389 int ret;
390 unsigned char i, j, offset;
391 signed char chunk;
392 mpi tmp, acc;
393 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
394
395 tmp.s = 1;
396 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
397 tmp.p = tmp_p;
398
399 acc.s = 1;
400 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
401 acc.p = acc_p;
402
403 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
404
405 /*
406 * acc = T
407 */
408 memset( acc_p, 0, sizeof( acc_p ) );
409 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
410
411 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
412 {
413 /*
414 * tmp = S_i
415 */
416 memset( tmp_p, 0, sizeof( tmp_p ) );
417 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
418 {
419 chunk = p192_tbl[i][j];
420 if( chunk >= 0 )
421 memcpy( tmp_p + offset * P192_CHUNK_INT,
422 N->p + chunk * P192_CHUNK_INT,
423 P192_CHUNK_CHAR );
424 }
425
426 /*
427 * acc += tmp
428 */
429 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
430 }
431
432 MPI_CHK( mpi_copy( N, &acc ) );
433
434cleanup:
435 return( ret );
436}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200437#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100438
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200439#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100440/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100441 * Size of p521 in terms of t_uint
442 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100443#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100444
445/*
446 * Bits to keep in the most significant t_uint
447 */
448#if defined(POLARSS_HAVE_INT8)
449#define P521_MASK 0x01
450#else
451#define P521_MASK 0x01FF
452#endif
453
454/*
455 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100456 */
457static int ecp_mod_p521( mpi *N )
458{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100459 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100460 t_uint Mp[P521_SIZE_INT];
461 mpi M;
462
463 if( N->n < P521_SIZE_INT )
464 return( 0 );
465
466 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
467 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
468 Mp[P521_SIZE_INT - 1] &= P521_MASK;
469
470 M.s = 1;
471 M.n = P521_SIZE_INT;
472 M.p = Mp;
473
474 MPI_CHK( mpi_shift_r( N, 521 ) );
475
476 MPI_CHK( mpi_add_abs( N, N, &M ) );
477
478cleanup:
479 return( ret );
480}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200481#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100482
483/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100484 * Domain parameters for secp192r1
485 */
486#define SECP192R1_P \
487 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
488#define SECP192R1_B \
489 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
490#define SECP192R1_GX \
491 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
492#define SECP192R1_GY \
493 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
494#define SECP192R1_N \
495 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
496
497/*
498 * Domain parameters for secp224r1
499 */
500#define SECP224R1_P \
501 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
502#define SECP224R1_B \
503 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
504#define SECP224R1_GX \
505 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
506#define SECP224R1_GY \
507 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
508#define SECP224R1_N \
509 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
510
511/*
512 * Domain parameters for secp256r1
513 */
514#define SECP256R1_P \
515 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
516#define SECP256R1_B \
517 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
518#define SECP256R1_GX \
519 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
520#define SECP256R1_GY \
521 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
522#define SECP256R1_N \
523 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
524
525/*
526 * Domain parameters for secp384r1
527 */
528#define SECP384R1_P \
529 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
530 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
531#define SECP384R1_B \
532 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
533 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
534#define SECP384R1_GX \
535 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
536 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
537#define SECP384R1_GY \
538 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
539 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
540#define SECP384R1_N \
541 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
542 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
543
544/*
545 * Domain parameters for secp521r1
546 */
547#define SECP521R1_P \
548 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
549 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
550 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
551#define SECP521R1_B \
552 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
553 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
554 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
555#define SECP521R1_GX \
556 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
557 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
558 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
559#define SECP521R1_GY \
560 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
561 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
562 "3FAD0761353C7086A272C24088BE94769FD16650"
563#define SECP521R1_N \
564 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
565 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
566 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
567
568/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100569 * Set a group using well-known domain parameters
570 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100571int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100572{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100573 grp->id = id;
574
575 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100576 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200577#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100578 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100579 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100580 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100581 SECP192R1_P, SECP192R1_B,
582 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200583#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100584
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200585#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100586 case POLARSSL_ECP_DP_SECP224R1:
587 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100588 SECP224R1_P, SECP224R1_B,
589 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200590#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100591
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200592#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100593 case POLARSSL_ECP_DP_SECP256R1:
594 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100595 SECP256R1_P, SECP256R1_B,
596 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200597#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100598
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200599#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100600 case POLARSSL_ECP_DP_SECP384R1:
601 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100602 SECP384R1_P, SECP384R1_B,
603 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200604#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100605
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200606#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100607 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100608 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100609 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100610 SECP521R1_P, SECP521R1_B,
611 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200612#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100613 }
614
Paul Bakkerfd3eac52013-06-29 23:31:33 +0200615 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100616}
617
618/*
619 * Set a group from an ECParameters record (RFC 4492)
620 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100621int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100622{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100623 ecp_group_id id;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100624
625 /*
626 * We expect at least three bytes (see below)
627 */
628 if( len < 3 )
629 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
630
631 /*
632 * First byte is curve_type; only named_curve is handled
633 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100634 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100635 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
636
637 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100638 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100639 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100640 id = *(*buf)++;
641 id <<= 8;
642 id |= *(*buf)++;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100643 return ecp_use_known_dp( grp, id );
644}
645
646/*
647 * Write the ECParameters record corresponding to a group (RFC 4492)
648 */
649int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
650 unsigned char *buf, size_t blen )
651{
652 /*
653 * We are going to write 3 bytes (see below)
654 */
655 *olen = 3;
656 if( blen < *olen )
657 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
658
659 /*
660 * First byte is curve_type, always named_curve
661 */
662 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
663
664 /*
665 * Next two bytes are the namedcurve value
666 */
667 buf[0] = grp->id >> 8;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100668 buf[1] = grp->id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100669
670 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100671}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100672
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100673/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100674 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100675 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100676 * In order to guarantee that, we need to ensure that operands of
677 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100678 * bring the result back to this range.
679 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100680 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100681 */
682
683/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100684 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
685 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100686#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100687
688/*
689 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
690 */
691#define MOD_SUB( N ) \
692 while( mpi_cmp_int( &N, 0 ) < 0 ) \
693 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
694
695/*
696 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
697 */
698#define MOD_ADD( N ) \
699 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
700 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
701
702/*
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100703 * Check that a point is valid as a public key (SEC1 3.2.3.1)
704 */
705int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
706{
707 int ret;
708 mpi YY, RHS;
709
710 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
711 return( POLARSSL_ERR_ECP_GENERIC );
712
713 /*
714 * pt coordinates must be normalized for our checks
715 */
716 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
717 return( POLARSSL_ERR_ECP_GENERIC );
718
719 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
720 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
721 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
722 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
723 return( POLARSSL_ERR_ECP_GENERIC );
724
725 mpi_init( &YY ); mpi_init( &RHS );
726
727 /*
728 * YY = Y^2
729 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
730 */
731 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
732 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
733 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
734 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
735 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
736
737 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
738 ret = POLARSSL_ERR_ECP_GENERIC;
739
740cleanup:
741
742 mpi_free( &YY ); mpi_free( &RHS );
743
744 return( ret );
745}
746
747/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100748 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100749 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100750static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100751{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100752 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100753 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100754
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100755 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100756 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100757
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100758 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100759
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100760 /*
761 * X = X / Z^2 mod p
762 */
763 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
764 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
765 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100766
767 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100768 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100769 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100770 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
771 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100772
773 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100774 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100775 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100776 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100777
778cleanup:
779
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100780 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100781
782 return( ret );
783}
784
785/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100786 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100787 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100788 * (See for example Cohen's "A Course in Computational Algebraic Number
789 * Theory", Algorithm 10.3.4.)
790 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100791 * Warning: fails if one of the points is zero!
792 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100793 */
794static int ecp_normalize_many( const ecp_group *grp,
795 ecp_point T[], size_t t_len )
796{
797 int ret;
798 size_t i;
799 mpi *c, u, Zi, ZZi;
800
801 if( t_len < 2 )
802 return( ecp_normalize( grp, T ) );
803
Paul Bakker6e339b52013-07-03 13:37:05 +0200804 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100805 return( POLARSSL_ERR_ECP_GENERIC );
806
807 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
808 for( i = 0; i < t_len; i++ )
809 mpi_init( &c[i] );
810
811 /*
812 * c[i] = Z_0 * ... * Z_i
813 */
814 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
815 for( i = 1; i < t_len; i++ )
816 {
817 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
818 MOD_MUL( c[i] );
819 }
820
821 /*
822 * u = 1 / (Z_0 * ... * Z_n) mod P
823 */
824 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
825
826 for( i = t_len - 1; ; i-- )
827 {
828 /*
829 * Zi = 1 / Z_i mod p
830 * u = 1 / (Z_0 * ... * Z_i) mod P
831 */
832 if( i == 0 ) {
833 MPI_CHK( mpi_copy( &Zi, &u ) );
834 }
835 else
836 {
837 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
838 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
839 }
840
841 /*
842 * proceed as in normalize()
843 */
844 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
845 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
846 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
847 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
848 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
849
850 if( i == 0 )
851 break;
852 }
853
854cleanup:
855
856 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
857 for( i = 0; i < t_len; i++ )
858 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200859 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100860
861 return( ret );
862}
863
864
865/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100866 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
867 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100868static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
869 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100870{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100871 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100872 mpi T1, T2, T3, X, Y, Z;
873
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100874#if defined(POLARSSL_SELF_TEST)
875 dbl_count++;
876#endif
877
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100878 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100879 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100880
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100881 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
882 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
883
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100884 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
885 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
886 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
887 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
888 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
889 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
890 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
891 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
892 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
893 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100894
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100895 /*
896 * For Y = Y / 2 mod p, we must make sure that Y is even before
897 * using right-shift. No need to reduce mod p afterwards.
898 */
899 if( mpi_get_bit( &Y, 0 ) == 1 )
900 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
901 MPI_CHK( mpi_shift_r( &Y, 1 ) );
902
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100903 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
904 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
905 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
906 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
907 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
908 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100909
910 MPI_CHK( mpi_copy( &R->X, &X ) );
911 MPI_CHK( mpi_copy( &R->Y, &Y ) );
912 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100913
914cleanup:
915
916 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
917 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
918
919 return( ret );
920}
921
922/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100923 * Addition or subtraction: R = P + Q or R = P + Q,
924 * mixed affine-Jacobian coordinates (GECC 3.22)
925 *
926 * The coordinates of Q must be normalized (= affine),
927 * but those of P don't need to. R is not normalized.
928 *
929 * If sign >= 0, perform addition, otherwise perform subtraction,
930 * taking advantage of the fact that, for Q != 0, we have
931 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100932 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100933static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100934 const ecp_point *P, const ecp_point *Q,
935 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100936{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100937 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100938 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100939
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100940#if defined(POLARSSL_SELF_TEST)
941 add_count++;
942#endif
943
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100944 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100945 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100946 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100947 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100948 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
949 return( ecp_copy( R, P ) );
950
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100951 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
952 {
953 ret = ecp_copy( R, Q );
954
955 /*
956 * -R.Y mod P = P - R.Y unless R.Y == 0
957 */
958 if( ret == 0 && sign < 0)
959 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
960 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
961
962 return( ret );
963 }
964
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100965 /*
966 * Make sure Q coordinates are normalized
967 */
968 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
969 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100970
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100971 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
972 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100973
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100974 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
975 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
976 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
977 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100978
979 /*
980 * For subtraction, -Q.Y should have been used instead of Q.Y,
981 * so we replace T2 by -T2, which is P - T2 mod P
982 */
983 if( sign < 0 )
984 {
985 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
986 MOD_SUB( T2 );
987 }
988
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100989 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
990 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100991
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100992 if( mpi_cmp_int( &T1, 0 ) == 0 )
993 {
994 if( mpi_cmp_int( &T2, 0 ) == 0 )
995 {
996 ret = ecp_double_jac( grp, R, P );
997 goto cleanup;
998 }
999 else
1000 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001001 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001002 goto cleanup;
1003 }
1004 }
1005
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001006 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1007 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1008 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1009 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1010 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1011 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1012 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1013 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1014 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1015 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1016 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1017 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001018
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001019 MPI_CHK( mpi_copy( &R->X, &X ) );
1020 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1021 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001022
1023cleanup:
1024
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001025 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1026 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001027
1028 return( ret );
1029}
1030
1031/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001032 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001033 */
1034int ecp_add( const ecp_group *grp, ecp_point *R,
1035 const ecp_point *P, const ecp_point *Q )
1036{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001037 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001038
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001039 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1040 MPI_CHK( ecp_normalize( grp, R ) );
1041
1042cleanup:
1043 return( ret );
1044}
1045
1046/*
1047 * Subtraction: R = P - Q, result's coordinates normalized
1048 */
1049int ecp_sub( const ecp_group *grp, ecp_point *R,
1050 const ecp_point *P, const ecp_point *Q )
1051{
1052 int ret;
1053
1054 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001055 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001056
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001057cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001058 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001059}
1060
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001061/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001062 * Compute a modified width-w non-adjacent form (NAF) of a number,
1063 * with a fixed pattern for resistance to SPA/timing attacks,
1064 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1065 * (The resulting multiplication algorithm can also been seen as a
1066 * modification of 2^w-ary multiplication, with signed coefficients,
1067 * all of them odd.)
1068 *
1069 * Input:
1070 * m must be an odd positive mpi less than w * k bits long
1071 * x must be an array of k elements
1072 * w must be less than a certain maximum (currently 8)
1073 *
1074 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1075 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1076 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1077 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1078 *
1079 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1080 * p. 335 of the cited reference, here we return only u, not d_w since
1081 * it is known that the other d_w[j] will be 0. Moreover, the returned
1082 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1083 * that u_i is odd. Also, since we always select a positive value for d
1084 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1085 * does. Finally, there is an off-by-one error in the reference: the
1086 * last index should be k-1, not k.
1087 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001088static int ecp_w_naf_fixed( signed char x[], size_t k,
1089 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001090{
1091 int ret;
1092 unsigned int i, u, mask, carry;
1093 mpi M;
1094
1095 mpi_init( &M );
1096
1097 MPI_CHK( mpi_copy( &M, m ) );
1098 mask = ( 1 << w ) - 1;
1099 carry = 1 << ( w - 1 );
1100
1101 for( i = 0; i < k; i++ )
1102 {
1103 u = M.p[0] & mask;
1104
1105 if( ( u & 1 ) == 0 && i > 0 )
1106 x[i - 1] -= carry;
1107
1108 x[i] = u >> 1;
1109 mpi_shift_r( &M, w );
1110 }
1111
1112 /*
1113 * We should have consumed all the bits now
1114 */
1115 if( mpi_cmp_int( &M, 0 ) != 0 )
1116 ret = POLARSSL_ERR_ECP_GENERIC;
1117
1118cleanup:
1119
1120 mpi_free( &M );
1121
1122 return( ret );
1123}
1124
1125/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001126 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1127 * The table is filled with T[i] = (2 * i + 1) P.
1128 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001129static int ecp_precompute( const ecp_group *grp,
1130 ecp_point T[], size_t t_len,
1131 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001132{
1133 int ret;
1134 size_t i;
1135 ecp_point PP;
1136
1137 ecp_point_init( &PP );
1138
1139 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1140
1141 MPI_CHK( ecp_copy( &T[0], P ) );
1142
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001143 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001144 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1145
1146 /*
1147 * T[0] = P already has normalized coordinates
1148 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001149 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001150
1151cleanup:
1152
1153 ecp_point_free( &PP );
1154
1155 return( ret );
1156}
1157
1158/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001159 * Maximum length of the precomputed table
1160 */
1161#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1162
1163/*
1164 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1165 * (that is: grp->nbits / w + 1)
1166 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1167 */
1168#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
1169
1170/*
1171 * Integer multiplication: R = m * P
1172 *
1173 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1174 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1175 *
1176 * This function executes a fixed number of operations for
1177 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001178 */
1179int ecp_mul( const ecp_group *grp, ecp_point *R,
1180 const mpi *m, const ecp_point *P )
1181{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001182 int ret;
1183 unsigned char w, m_is_odd;
1184 size_t pre_len, naf_len, i, j;
1185 signed char naf[ MAX_NAF_LEN ];
1186 ecp_point Q, T[ MAX_PRE_LEN ];
1187 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001188
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001189 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001190 return( POLARSSL_ERR_ECP_GENERIC );
1191
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001192 w = grp->nbits >= 521 ? 6 :
1193 grp->nbits >= 224 ? 5 :
1194 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001195
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001196 /*
1197 * Make sure w is within the limits.
1198 * The last test ensures that none of the precomputed points is zero,
1199 * which wouldn't be handled correctly by ecp_normalize_many().
1200 * It is only useful for small curves, as used in the test suite.
1201 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001202 if( w > POLARSSL_ECP_WINDOW_SIZE )
1203 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001204 if( w < 2 || w >= grp->nbits )
1205 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001206
1207 pre_len = 1 << ( w - 1 );
1208 naf_len = grp->nbits / w + 1;
1209
1210 mpi_init( &M );
1211 ecp_point_init( &Q );
1212 for( i = 0; i < pre_len; i++ )
1213 ecp_point_init( &T[i] );
1214
1215 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1216
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001217 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001218 * Make sure M is odd:
1219 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001220 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001221 MPI_CHK( mpi_copy( &M, m ) );
1222 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001223
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001224 /*
1225 * Compute the fixed-pattern NAF and precompute odd multiples
1226 */
1227 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1228 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001229
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001230 /*
1231 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1232 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1233 *
1234 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1235 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1236 * == T[ - naf[i] - 1 ]
1237 */
1238 MPI_CHK( ecp_set_zero( &Q ) );
1239 i = naf_len - 1;
1240 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001241 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001242 if( naf[i] < 0 )
1243 {
1244 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1245 }
1246 else
1247 {
1248 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1249 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001250
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001251 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001252 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001253 i--;
1254
1255 for( j = 0; j < w; j++ )
1256 {
1257 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1258 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001259 }
1260
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001261 /*
1262 * Now get m * P from M * P.
1263 * Since we don't need T[] any more, we can recycle it:
1264 * we already have T[0] = P, now set T[1] = 2 * P.
1265 */
1266 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1267 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001268
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001269
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001270cleanup:
1271
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001272 mpi_free( &M );
1273 ecp_point_free( &Q );
1274 for( i = 0; i < pre_len; i++ )
1275 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001276
1277 return( ret );
1278}
1279
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001280/*
1281 * Generate a keypair (SEC1 3.2.1)
1282 */
1283int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1284 int (*f_rng)(void *, unsigned char *, size_t),
1285 void *p_rng )
1286{
1287 int count = 0;
1288 size_t n_size = (grp->nbits + 7) / 8;
1289
1290 /*
1291 * Generate d such that 1 <= n < N
1292 */
1293 do
1294 {
1295 mpi_fill_random( d, n_size, f_rng, p_rng );
1296
1297 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1298 mpi_shift_r( d, 1 );
1299
1300 if( count++ > 10 )
1301 return( POLARSSL_ERR_ECP_GENERIC );
1302 }
1303 while( mpi_cmp_int( d, 1 ) < 0 );
1304
1305 return( ecp_mul( grp, Q, d, &grp->G ) );
1306}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001307
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001308#if defined(POLARSSL_SELF_TEST)
1309
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001310/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001311 * Checkup routine
1312 */
1313int ecp_self_test( int verbose )
1314{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001315 int ret;
1316 size_t i;
1317 ecp_group grp;
1318 ecp_point R;
1319 mpi m;
1320 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001321 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001322 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001323 "000000000000000000000000000000000000000000000000", /* zero */
1324 "000000000000000000000000000000000000000000000001", /* one */
1325 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1326 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001327 "400000000000000000000000000000000000000000000000",
1328 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1329 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001330 };
1331
1332 ecp_group_init( &grp );
1333 ecp_point_init( &R );
1334 mpi_init( &m );
1335
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001336#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001337 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001338#else
1339#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1340 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1341#else
1342#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1343 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1344#else
1345#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1346 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1347#else
1348#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1349 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1350#else
1351#error No curves defines
1352#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1353#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1354#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1355#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1356#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001357
1358 if( verbose != 0 )
1359 printf( " ECP test #1 (SPA resistance): " );
1360
1361 add_count = 0;
1362 dbl_count = 0;
1363 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1364 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1365
1366 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1367 {
1368 add_c_prev = add_count;
1369 dbl_c_prev = dbl_count;
1370 add_count = 0;
1371 dbl_count = 0;
1372
1373 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1374 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1375
1376 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1377 {
1378 if( verbose != 0 )
1379 printf( "failed (%zu)\n", i );
1380
1381 ret = 1;
1382 goto cleanup;
1383 }
1384 }
1385
1386 if( verbose != 0 )
1387 printf( "passed\n" );
1388
1389cleanup:
1390
1391 if( ret < 0 && verbose != 0 )
1392 printf( "Unexpected error, return code = %08X\n", ret );
1393
1394 ecp_group_free( &grp );
1395 ecp_point_free( &R );
1396 mpi_free( &m );
1397
1398 if( verbose != 0 )
1399 printf( "\n" );
1400
1401 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001402}
1403
1404#endif
1405
1406#endif