blob: a2d13c44478c6632a4a5201497f2c149e6776e9b [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é-Gonnardb8c6e0e2013-07-01 13:40:52 +020094 * Initialize (the components of) a key pair
95 */
96void ecp_keypair_init( ecp_keypair *key )
97{
98 if ( key == NULL )
99 return;
100
101 ecp_group_init( &key->grp );
102 mpi_init( &key->d );
103 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200104}
105
106/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100107 * Unallocate (the components of) a point
108 */
109void ecp_point_free( ecp_point *pt )
110{
111 if( pt == NULL )
112 return;
113
114 mpi_free( &( pt->X ) );
115 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100116 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100117}
118
119/*
120 * Unallocate (the components of) a group
121 */
122void ecp_group_free( ecp_group *grp )
123{
124 if( grp == NULL )
125 return;
126
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100127 mpi_free( &grp->P );
128 mpi_free( &grp->B );
129 ecp_point_free( &grp->G );
130 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100131}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100132
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100133/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200134 * Unallocate (the components of) a key pair
135 */
136void ecp_keypair_free( ecp_keypair *key )
137{
138 if ( key == NULL )
139 return;
140
141 ecp_group_free( &key->grp );
142 mpi_free( &key->d );
143 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200144}
145
146/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100147 * Set point to zero
148 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100149int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100150{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100151 int ret;
152
153 MPI_CHK( mpi_lset( &pt->X , 1 ) );
154 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
155 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
156
157cleanup:
158 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100159}
160
161/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100162 * Tell if a point is zero
163 */
164int ecp_is_zero( ecp_point *pt )
165{
166 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
167}
168
169/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100170 * Copy the contents of Q into P
171 */
172int ecp_copy( ecp_point *P, const ecp_point *Q )
173{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100174 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100175
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100176 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
177 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100178 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100179
180cleanup:
181 return( ret );
182}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100183
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100184/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100185 * Import a non-zero point from ASCII strings
186 */
187int ecp_point_read_string( ecp_point *P, int radix,
188 const char *x, const char *y )
189{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100190 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100191
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100192 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
193 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100194 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100195
196cleanup:
197 return( ret );
198}
199
200/*
201 * Import an ECP group from ASCII strings
202 */
203int ecp_group_read_string( ecp_group *grp, int radix,
204 const char *p, const char *b,
205 const char *gx, const char *gy, const char *n)
206{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100207 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100208
209 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
210 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
211 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
212 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
213
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100214 grp->pbits = mpi_msb( &grp->P );
215 grp->nbits = mpi_msb( &grp->N );
216
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100217cleanup:
218 return( ret );
219}
220
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100221/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100222 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100223 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100224int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100225 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100226 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100227{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200228 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100229 size_t plen;
230
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100231 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
232 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100233 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100234
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100235 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100236 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100237 */
238 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
239 {
240 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100241 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100242
243 buf[0] = 0x00;
244 *olen = 1;
245
246 return( 0 );
247 }
248
249 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100250
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100251 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
252 {
253 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100254
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100255 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100256 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100257
258 buf[0] = 0x04;
259 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
260 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
261 }
262 else if( format == POLARSSL_ECP_PF_COMPRESSED )
263 {
264 *olen = plen + 1;
265
266 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100267 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100268
269 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
270 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
271 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100272
273cleanup:
274 return( ret );
275}
276
277/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100278 * Import a point from unsigned binary data (SEC1 2.3.4)
279 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100280int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
281 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100282 int ret;
283 size_t plen;
284
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100285 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100286 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100287
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100288 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100289
290 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100291 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100292
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100293 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
294 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
295 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100296
297cleanup:
298 return( ret );
299}
300
301/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100302 * Import a point from a TLS ECPoint record (RFC 4492)
303 * struct {
304 * opaque point <1..2^8-1>;
305 * } ECPoint;
306 */
307int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100308 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100309{
310 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100311 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100312
313 /*
314 * We must have at least two bytes (1 for length, at least of for data)
315 */
316 if( buf_len < 2 )
317 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
318
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100319 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100320 if( data_len < 1 || data_len > buf_len - 1 )
321 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
322
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100323 /*
324 * Save buffer start for read_binary and update buf
325 */
326 buf_start = *buf;
327 *buf += data_len;
328
329 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100330}
331
332/*
333 * Export a point as a TLS ECPoint record (RFC 4492)
334 * struct {
335 * opaque point <1..2^8-1>;
336 * } ECPoint;
337 */
338int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100339 int format, size_t *olen,
340 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100341{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100342 int ret;
343
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100344 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100345 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100346 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100347 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100348 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
349
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100350 if( ( ret = ecp_point_write_binary( grp, pt, format,
351 olen, buf + 1, blen - 1) ) != 0 )
352 return( ret );
353
354 /*
355 * write length to the first byte and update total length
356 */
357 buf[0] = *olen;
358 ++*olen;
359
360 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100361}
362
363/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100364 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
365 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100366 */
367static int ecp_modp( mpi *N, const ecp_group *grp )
368{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100369 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100370
371 if( grp->modp == NULL )
372 return( mpi_mod_mpi( N, N, &grp->P ) );
373
374 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
375 return( POLARSSL_ERR_ECP_GENERIC );
376
377 MPI_CHK( grp->modp( N ) );
378
379 while( mpi_cmp_int( N, 0 ) < 0 )
380 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
381
382 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
383 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
384
385cleanup:
386 return( ret );
387}
388
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200389#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100390/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100391 * 192 bits in terms of t_uint
392 */
393#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
394
395/*
396 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
397 * -1 means let this chunk be 0
398 * a positive value i means A_i.
399 */
400#define P192_CHUNKS 3
401#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
402#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
403
404const signed char p192_tbl[][P192_CHUNKS] = {
405 { -1, 3, 3 }, /* S1 */
406 { 4, 4, -1 }, /* S2 */
407 { 5, 5, 5 }, /* S3 */
408};
409
410/*
411 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
412 */
413static int ecp_mod_p192( mpi *N )
414{
415 int ret;
416 unsigned char i, j, offset;
417 signed char chunk;
418 mpi tmp, acc;
419 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
420
421 tmp.s = 1;
422 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
423 tmp.p = tmp_p;
424
425 acc.s = 1;
426 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
427 acc.p = acc_p;
428
429 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
430
431 /*
432 * acc = T
433 */
434 memset( acc_p, 0, sizeof( acc_p ) );
435 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
436
437 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
438 {
439 /*
440 * tmp = S_i
441 */
442 memset( tmp_p, 0, sizeof( tmp_p ) );
443 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
444 {
445 chunk = p192_tbl[i][j];
446 if( chunk >= 0 )
447 memcpy( tmp_p + offset * P192_CHUNK_INT,
448 N->p + chunk * P192_CHUNK_INT,
449 P192_CHUNK_CHAR );
450 }
451
452 /*
453 * acc += tmp
454 */
455 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
456 }
457
458 MPI_CHK( mpi_copy( N, &acc ) );
459
460cleanup:
461 return( ret );
462}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200463#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100464
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200465#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100466/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100467 * Size of p521 in terms of t_uint
468 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100469#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100470
471/*
472 * Bits to keep in the most significant t_uint
473 */
474#if defined(POLARSS_HAVE_INT8)
475#define P521_MASK 0x01
476#else
477#define P521_MASK 0x01FF
478#endif
479
480/*
481 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100482 */
483static int ecp_mod_p521( mpi *N )
484{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100485 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100486 t_uint Mp[P521_SIZE_INT];
487 mpi M;
488
489 if( N->n < P521_SIZE_INT )
490 return( 0 );
491
492 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
493 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
494 Mp[P521_SIZE_INT - 1] &= P521_MASK;
495
496 M.s = 1;
497 M.n = P521_SIZE_INT;
498 M.p = Mp;
499
500 MPI_CHK( mpi_shift_r( N, 521 ) );
501
502 MPI_CHK( mpi_add_abs( N, N, &M ) );
503
504cleanup:
505 return( ret );
506}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200507#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100508
509/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100510 * Domain parameters for secp192r1
511 */
512#define SECP192R1_P \
513 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
514#define SECP192R1_B \
515 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
516#define SECP192R1_GX \
517 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
518#define SECP192R1_GY \
519 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
520#define SECP192R1_N \
521 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
522
523/*
524 * Domain parameters for secp224r1
525 */
526#define SECP224R1_P \
527 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
528#define SECP224R1_B \
529 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
530#define SECP224R1_GX \
531 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
532#define SECP224R1_GY \
533 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
534#define SECP224R1_N \
535 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
536
537/*
538 * Domain parameters for secp256r1
539 */
540#define SECP256R1_P \
541 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
542#define SECP256R1_B \
543 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
544#define SECP256R1_GX \
545 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
546#define SECP256R1_GY \
547 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
548#define SECP256R1_N \
549 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
550
551/*
552 * Domain parameters for secp384r1
553 */
554#define SECP384R1_P \
555 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
556 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
557#define SECP384R1_B \
558 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
559 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
560#define SECP384R1_GX \
561 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
562 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
563#define SECP384R1_GY \
564 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
565 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
566#define SECP384R1_N \
567 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
568 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
569
570/*
571 * Domain parameters for secp521r1
572 */
573#define SECP521R1_P \
574 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
575 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
576 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
577#define SECP521R1_B \
578 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
579 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
580 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
581#define SECP521R1_GX \
582 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
583 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
584 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
585#define SECP521R1_GY \
586 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
587 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
588 "3FAD0761353C7086A272C24088BE94769FD16650"
589#define SECP521R1_N \
590 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
591 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
592 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
593
594/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100595 * Set a group using well-known domain parameters
596 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100597int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100598{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100599 grp->id = id;
600
601 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100602 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200603#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100604 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100605 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100606 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100607 SECP192R1_P, SECP192R1_B,
608 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200609#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100610
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200611#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100612 case POLARSSL_ECP_DP_SECP224R1:
613 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100614 SECP224R1_P, SECP224R1_B,
615 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200616#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100617
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200618#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100619 case POLARSSL_ECP_DP_SECP256R1:
620 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100621 SECP256R1_P, SECP256R1_B,
622 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200623#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100624
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200625#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100626 case POLARSSL_ECP_DP_SECP384R1:
627 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100628 SECP384R1_P, SECP384R1_B,
629 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200630#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100631
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200632#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100633 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100634 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100635 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100636 SECP521R1_P, SECP521R1_B,
637 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200638#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100639 }
640
Paul Bakkerfd3eac52013-06-29 23:31:33 +0200641 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100642}
643
644/*
645 * Set a group from an ECParameters record (RFC 4492)
646 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100647int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100648{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100649 ecp_group_id id;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100650
651 /*
652 * We expect at least three bytes (see below)
653 */
654 if( len < 3 )
655 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
656
657 /*
658 * First byte is curve_type; only named_curve is handled
659 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100660 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100661 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
662
663 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100664 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100665 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100666 id = *(*buf)++;
667 id <<= 8;
668 id |= *(*buf)++;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100669 return ecp_use_known_dp( grp, id );
670}
671
672/*
673 * Write the ECParameters record corresponding to a group (RFC 4492)
674 */
675int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
676 unsigned char *buf, size_t blen )
677{
678 /*
679 * We are going to write 3 bytes (see below)
680 */
681 *olen = 3;
682 if( blen < *olen )
683 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
684
685 /*
686 * First byte is curve_type, always named_curve
687 */
688 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
689
690 /*
691 * Next two bytes are the namedcurve value
692 */
693 buf[0] = grp->id >> 8;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100694 buf[1] = grp->id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100695
696 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100697}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100698
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100699/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100700 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100701 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100702 * In order to guarantee that, we need to ensure that operands of
703 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100704 * bring the result back to this range.
705 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100706 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100707 */
708
709/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100710 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
711 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100712#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100713
714/*
715 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
716 */
717#define MOD_SUB( N ) \
718 while( mpi_cmp_int( &N, 0 ) < 0 ) \
719 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
720
721/*
722 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
723 */
724#define MOD_ADD( N ) \
725 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
726 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
727
728/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100729 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100730 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100731static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100732{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100733 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100734 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100735
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100736 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100737 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100738
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100739 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100740
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100741 /*
742 * X = X / Z^2 mod p
743 */
744 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
745 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
746 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100747
748 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100749 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100750 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100751 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
752 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100753
754 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100755 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100756 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100757 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100758
759cleanup:
760
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100761 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100762
763 return( ret );
764}
765
766/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100767 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100768 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100769 * (See for example Cohen's "A Course in Computational Algebraic Number
770 * Theory", Algorithm 10.3.4.)
771 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100772 * Warning: fails if one of the points is zero!
773 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100774 */
775static int ecp_normalize_many( const ecp_group *grp,
776 ecp_point T[], size_t t_len )
777{
778 int ret;
779 size_t i;
780 mpi *c, u, Zi, ZZi;
781
782 if( t_len < 2 )
783 return( ecp_normalize( grp, T ) );
784
Paul Bakker6e339b52013-07-03 13:37:05 +0200785 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100786 return( POLARSSL_ERR_ECP_GENERIC );
787
788 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
789 for( i = 0; i < t_len; i++ )
790 mpi_init( &c[i] );
791
792 /*
793 * c[i] = Z_0 * ... * Z_i
794 */
795 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
796 for( i = 1; i < t_len; i++ )
797 {
798 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
799 MOD_MUL( c[i] );
800 }
801
802 /*
803 * u = 1 / (Z_0 * ... * Z_n) mod P
804 */
805 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
806
807 for( i = t_len - 1; ; i-- )
808 {
809 /*
810 * Zi = 1 / Z_i mod p
811 * u = 1 / (Z_0 * ... * Z_i) mod P
812 */
813 if( i == 0 ) {
814 MPI_CHK( mpi_copy( &Zi, &u ) );
815 }
816 else
817 {
818 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
819 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
820 }
821
822 /*
823 * proceed as in normalize()
824 */
825 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
826 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
827 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
828 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
829 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
830
831 if( i == 0 )
832 break;
833 }
834
835cleanup:
836
837 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
838 for( i = 0; i < t_len; i++ )
839 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200840 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100841
842 return( ret );
843}
844
845
846/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100847 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
848 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100849static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
850 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100851{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100852 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100853 mpi T1, T2, T3, X, Y, Z;
854
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100855#if defined(POLARSSL_SELF_TEST)
856 dbl_count++;
857#endif
858
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100859 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100860 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100861
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100862 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
863 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
864
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100865 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
866 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
867 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
868 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
869 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
870 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
871 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
872 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
873 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
874 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100875
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100876 /*
877 * For Y = Y / 2 mod p, we must make sure that Y is even before
878 * using right-shift. No need to reduce mod p afterwards.
879 */
880 if( mpi_get_bit( &Y, 0 ) == 1 )
881 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
882 MPI_CHK( mpi_shift_r( &Y, 1 ) );
883
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100884 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
885 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
886 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
887 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
888 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
889 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100890
891 MPI_CHK( mpi_copy( &R->X, &X ) );
892 MPI_CHK( mpi_copy( &R->Y, &Y ) );
893 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100894
895cleanup:
896
897 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
898 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
899
900 return( ret );
901}
902
903/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100904 * Addition or subtraction: R = P + Q or R = P + Q,
905 * mixed affine-Jacobian coordinates (GECC 3.22)
906 *
907 * The coordinates of Q must be normalized (= affine),
908 * but those of P don't need to. R is not normalized.
909 *
910 * If sign >= 0, perform addition, otherwise perform subtraction,
911 * taking advantage of the fact that, for Q != 0, we have
912 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100913 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100914static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100915 const ecp_point *P, const ecp_point *Q,
916 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100917{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100918 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100919 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100920
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100921#if defined(POLARSSL_SELF_TEST)
922 add_count++;
923#endif
924
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100925 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100926 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100927 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100928 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100929 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
930 return( ecp_copy( R, P ) );
931
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100932 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
933 {
934 ret = ecp_copy( R, Q );
935
936 /*
937 * -R.Y mod P = P - R.Y unless R.Y == 0
938 */
939 if( ret == 0 && sign < 0)
940 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
941 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
942
943 return( ret );
944 }
945
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100946 /*
947 * Make sure Q coordinates are normalized
948 */
949 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
950 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100951
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100952 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
953 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100954
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100955 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
956 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
957 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
958 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100959
960 /*
961 * For subtraction, -Q.Y should have been used instead of Q.Y,
962 * so we replace T2 by -T2, which is P - T2 mod P
963 */
964 if( sign < 0 )
965 {
966 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
967 MOD_SUB( T2 );
968 }
969
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100970 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
971 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100972
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100973 if( mpi_cmp_int( &T1, 0 ) == 0 )
974 {
975 if( mpi_cmp_int( &T2, 0 ) == 0 )
976 {
977 ret = ecp_double_jac( grp, R, P );
978 goto cleanup;
979 }
980 else
981 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100982 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100983 goto cleanup;
984 }
985 }
986
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100987 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
988 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
989 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
990 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
991 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
992 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
993 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
994 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
995 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
996 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
997 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
998 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100999
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001000 MPI_CHK( mpi_copy( &R->X, &X ) );
1001 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1002 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001003
1004cleanup:
1005
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001006 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1007 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001008
1009 return( ret );
1010}
1011
1012/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001013 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001014 */
1015int ecp_add( const ecp_group *grp, ecp_point *R,
1016 const ecp_point *P, const ecp_point *Q )
1017{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001018 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001019
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001020 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1021 MPI_CHK( ecp_normalize( grp, R ) );
1022
1023cleanup:
1024 return( ret );
1025}
1026
1027/*
1028 * Subtraction: R = P - Q, result's coordinates normalized
1029 */
1030int ecp_sub( const ecp_group *grp, ecp_point *R,
1031 const ecp_point *P, const ecp_point *Q )
1032{
1033 int ret;
1034
1035 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001036 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001037
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001038cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001039 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001040}
1041
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001042/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001043 * Compute a modified width-w non-adjacent form (NAF) of a number,
1044 * with a fixed pattern for resistance to SPA/timing attacks,
1045 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1046 * (The resulting multiplication algorithm can also been seen as a
1047 * modification of 2^w-ary multiplication, with signed coefficients,
1048 * all of them odd.)
1049 *
1050 * Input:
1051 * m must be an odd positive mpi less than w * k bits long
1052 * x must be an array of k elements
1053 * w must be less than a certain maximum (currently 8)
1054 *
1055 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1056 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1057 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1058 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1059 *
1060 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1061 * p. 335 of the cited reference, here we return only u, not d_w since
1062 * it is known that the other d_w[j] will be 0. Moreover, the returned
1063 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1064 * that u_i is odd. Also, since we always select a positive value for d
1065 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1066 * does. Finally, there is an off-by-one error in the reference: the
1067 * last index should be k-1, not k.
1068 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001069static int ecp_w_naf_fixed( signed char x[], size_t k,
1070 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001071{
1072 int ret;
1073 unsigned int i, u, mask, carry;
1074 mpi M;
1075
1076 mpi_init( &M );
1077
1078 MPI_CHK( mpi_copy( &M, m ) );
1079 mask = ( 1 << w ) - 1;
1080 carry = 1 << ( w - 1 );
1081
1082 for( i = 0; i < k; i++ )
1083 {
1084 u = M.p[0] & mask;
1085
1086 if( ( u & 1 ) == 0 && i > 0 )
1087 x[i - 1] -= carry;
1088
1089 x[i] = u >> 1;
1090 mpi_shift_r( &M, w );
1091 }
1092
1093 /*
1094 * We should have consumed all the bits now
1095 */
1096 if( mpi_cmp_int( &M, 0 ) != 0 )
1097 ret = POLARSSL_ERR_ECP_GENERIC;
1098
1099cleanup:
1100
1101 mpi_free( &M );
1102
1103 return( ret );
1104}
1105
1106/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001107 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1108 * The table is filled with T[i] = (2 * i + 1) P.
1109 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001110static int ecp_precompute( const ecp_group *grp,
1111 ecp_point T[], size_t t_len,
1112 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001113{
1114 int ret;
1115 size_t i;
1116 ecp_point PP;
1117
1118 ecp_point_init( &PP );
1119
1120 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1121
1122 MPI_CHK( ecp_copy( &T[0], P ) );
1123
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001124 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001125 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1126
1127 /*
1128 * T[0] = P already has normalized coordinates
1129 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001130 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001131
1132cleanup:
1133
1134 ecp_point_free( &PP );
1135
1136 return( ret );
1137}
1138
1139/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001140 * Maximum length of the precomputed table
1141 */
1142#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1143
1144/*
1145 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1146 * (that is: grp->nbits / w + 1)
1147 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1148 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001149#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001150
1151/*
1152 * Integer multiplication: R = m * P
1153 *
1154 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1155 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1156 *
1157 * This function executes a fixed number of operations for
1158 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001159 */
1160int ecp_mul( const ecp_group *grp, ecp_point *R,
1161 const mpi *m, const ecp_point *P )
1162{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001163 int ret;
1164 unsigned char w, m_is_odd;
1165 size_t pre_len, naf_len, i, j;
1166 signed char naf[ MAX_NAF_LEN ];
1167 ecp_point Q, T[ MAX_PRE_LEN ];
1168 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001169
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001170 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001171 return( POLARSSL_ERR_ECP_GENERIC );
1172
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001173 w = grp->nbits >= 521 ? 6 :
1174 grp->nbits >= 224 ? 5 :
1175 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001176
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001177 /*
1178 * Make sure w is within the limits.
1179 * The last test ensures that none of the precomputed points is zero,
1180 * which wouldn't be handled correctly by ecp_normalize_many().
1181 * It is only useful for small curves, as used in the test suite.
1182 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001183 if( w > POLARSSL_ECP_WINDOW_SIZE )
1184 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001185 if( w < 2 || w >= grp->nbits )
1186 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001187
1188 pre_len = 1 << ( w - 1 );
1189 naf_len = grp->nbits / w + 1;
1190
1191 mpi_init( &M );
1192 ecp_point_init( &Q );
1193 for( i = 0; i < pre_len; i++ )
1194 ecp_point_init( &T[i] );
1195
1196 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1197
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001198 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001199 * Make sure M is odd:
1200 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001201 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001202 MPI_CHK( mpi_copy( &M, m ) );
1203 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001204
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001205 /*
1206 * Compute the fixed-pattern NAF and precompute odd multiples
1207 */
1208 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1209 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001210
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001211 /*
1212 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1213 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1214 *
1215 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1216 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1217 * == T[ - naf[i] - 1 ]
1218 */
1219 MPI_CHK( ecp_set_zero( &Q ) );
1220 i = naf_len - 1;
1221 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001222 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001223 if( naf[i] < 0 )
1224 {
1225 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1226 }
1227 else
1228 {
1229 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1230 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001231
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001232 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001233 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001234 i--;
1235
1236 for( j = 0; j < w; j++ )
1237 {
1238 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1239 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001240 }
1241
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001242 /*
1243 * Now get m * P from M * P.
1244 * Since we don't need T[] any more, we can recycle it:
1245 * we already have T[0] = P, now set T[1] = 2 * P.
1246 */
1247 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1248 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001249
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001250
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001251cleanup:
1252
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001253 mpi_free( &M );
1254 ecp_point_free( &Q );
1255 for( i = 0; i < pre_len; i++ )
1256 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001257
1258 return( ret );
1259}
1260
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001261/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001262 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1263 */
1264int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1265{
1266 int ret;
1267 mpi YY, RHS;
1268
1269 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
1270 return( POLARSSL_ERR_ECP_GENERIC );
1271
1272 /*
1273 * pt coordinates must be normalized for our checks
1274 */
1275 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1276 return( POLARSSL_ERR_ECP_GENERIC );
1277
1278 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1279 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1280 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1281 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
1282 return( POLARSSL_ERR_ECP_GENERIC );
1283
1284 mpi_init( &YY ); mpi_init( &RHS );
1285
1286 /*
1287 * YY = Y^2
1288 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
1289 */
1290 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1291 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
1292 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1293 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1294 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
1295
1296 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
1297 ret = POLARSSL_ERR_ECP_GENERIC;
1298
1299cleanup:
1300
1301 mpi_free( &YY ); mpi_free( &RHS );
1302
1303 return( ret );
1304}
1305
1306/*
1307 * Check that an mpi is valid as a private key (SEC1 3.2)
1308 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001309int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001310{
1311 /* We want 1 <= d <= N-1 */
1312 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
1313 return( POLARSSL_ERR_ECP_GENERIC );
1314
1315 return( 0 );
1316}
1317
1318/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001319 * Generate a keypair (SEC1 3.2.1)
1320 */
1321int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1322 int (*f_rng)(void *, unsigned char *, size_t),
1323 void *p_rng )
1324{
1325 int count = 0;
1326 size_t n_size = (grp->nbits + 7) / 8;
1327
1328 /*
1329 * Generate d such that 1 <= n < N
1330 */
1331 do
1332 {
1333 mpi_fill_random( d, n_size, f_rng, p_rng );
1334
1335 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1336 mpi_shift_r( d, 1 );
1337
1338 if( count++ > 10 )
1339 return( POLARSSL_ERR_ECP_GENERIC );
1340 }
1341 while( mpi_cmp_int( d, 1 ) < 0 );
1342
1343 return( ecp_mul( grp, Q, d, &grp->G ) );
1344}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001345
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001346#if defined(POLARSSL_SELF_TEST)
1347
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001348/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001349 * Checkup routine
1350 */
1351int ecp_self_test( int verbose )
1352{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001353 int ret;
1354 size_t i;
1355 ecp_group grp;
1356 ecp_point R;
1357 mpi m;
1358 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001359 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001360 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001361 "000000000000000000000000000000000000000000000000", /* zero */
1362 "000000000000000000000000000000000000000000000001", /* one */
1363 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1364 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001365 "400000000000000000000000000000000000000000000000",
1366 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1367 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001368 };
1369
1370 ecp_group_init( &grp );
1371 ecp_point_init( &R );
1372 mpi_init( &m );
1373
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001374#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001375 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001376#else
1377#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1378 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1379#else
1380#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1381 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1382#else
1383#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1384 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1385#else
1386#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1387 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1388#else
1389#error No curves defines
1390#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1391#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1392#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1393#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1394#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001395
1396 if( verbose != 0 )
1397 printf( " ECP test #1 (SPA resistance): " );
1398
1399 add_count = 0;
1400 dbl_count = 0;
1401 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1402 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1403
1404 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1405 {
1406 add_c_prev = add_count;
1407 dbl_c_prev = dbl_count;
1408 add_count = 0;
1409 dbl_count = 0;
1410
1411 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1412 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) );
1413
1414 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1415 {
1416 if( verbose != 0 )
1417 printf( "failed (%zu)\n", i );
1418
1419 ret = 1;
1420 goto cleanup;
1421 }
1422 }
1423
1424 if( verbose != 0 )
1425 printf( "passed\n" );
1426
1427cleanup:
1428
1429 if( ret < 0 && verbose != 0 )
1430 printf( "Unexpected error, return code = %08X\n", ret );
1431
1432 ecp_group_free( &grp );
1433 ecp_point_free( &R );
1434 mpi_free( &m );
1435
1436 if( verbose != 0 )
1437 printf( "\n" );
1438
1439 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001440}
1441
1442#endif
1443
1444#endif