blob: b4ee042d2cf465bc9b53b4896b841050305c2011 [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é-Gonnarde09631b2013-08-12 15:44:31 +0200185 * Copy the contents of a group object
186 */
187int ecp_group_copy( ecp_group *dst, const ecp_group *src )
188{
189 return ecp_use_known_dp( dst, src->id );
190}
191
192/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100193 * Import a non-zero point from ASCII strings
194 */
195int ecp_point_read_string( ecp_point *P, int radix,
196 const char *x, const char *y )
197{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100198 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100199
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100200 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
201 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100202 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100203
204cleanup:
205 return( ret );
206}
207
208/*
209 * Import an ECP group from ASCII strings
210 */
211int ecp_group_read_string( ecp_group *grp, int radix,
212 const char *p, const char *b,
213 const char *gx, const char *gy, const char *n)
214{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100215 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100216
217 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
218 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
219 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
220 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
221
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100222 grp->pbits = mpi_msb( &grp->P );
223 grp->nbits = mpi_msb( &grp->N );
224
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100225cleanup:
226 return( ret );
227}
228
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100229/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100230 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100231 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100232int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100233 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100234 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100235{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200236 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100237 size_t plen;
238
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100239 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
240 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100241 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100242
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100243 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100244 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100245 */
246 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
247 {
248 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100249 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100250
251 buf[0] = 0x00;
252 *olen = 1;
253
254 return( 0 );
255 }
256
257 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100258
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100259 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
260 {
261 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100262
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100263 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100264 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100265
266 buf[0] = 0x04;
267 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
268 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
269 }
270 else if( format == POLARSSL_ECP_PF_COMPRESSED )
271 {
272 *olen = plen + 1;
273
274 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100275 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100276
277 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
278 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
279 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100280
281cleanup:
282 return( ret );
283}
284
285/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100286 * Import a point from unsigned binary data (SEC1 2.3.4)
287 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100288int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
289 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100290 int ret;
291 size_t plen;
292
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100293 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100294 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100295
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100296 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100297
298 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100299 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100300
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100301 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
302 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
303 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100304
305cleanup:
306 return( ret );
307}
308
309/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100310 * Import a point from a TLS ECPoint record (RFC 4492)
311 * struct {
312 * opaque point <1..2^8-1>;
313 * } ECPoint;
314 */
315int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100316 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100317{
318 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100319 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100320
321 /*
322 * We must have at least two bytes (1 for length, at least of for data)
323 */
324 if( buf_len < 2 )
325 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
326
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100327 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100328 if( data_len < 1 || data_len > buf_len - 1 )
329 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
330
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100331 /*
332 * Save buffer start for read_binary and update buf
333 */
334 buf_start = *buf;
335 *buf += data_len;
336
337 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100338}
339
340/*
341 * Export a point as a TLS ECPoint record (RFC 4492)
342 * struct {
343 * opaque point <1..2^8-1>;
344 * } ECPoint;
345 */
346int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100347 int format, size_t *olen,
348 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100349{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100350 int ret;
351
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100352 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100353 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100354 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100355 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100356 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
357
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100358 if( ( ret = ecp_point_write_binary( grp, pt, format,
359 olen, buf + 1, blen - 1) ) != 0 )
360 return( ret );
361
362 /*
363 * write length to the first byte and update total length
364 */
365 buf[0] = *olen;
366 ++*olen;
367
368 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100369}
370
371/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100372 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
373 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100374 */
375static int ecp_modp( mpi *N, const ecp_group *grp )
376{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100377 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100378
379 if( grp->modp == NULL )
380 return( mpi_mod_mpi( N, N, &grp->P ) );
381
382 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
383 return( POLARSSL_ERR_ECP_GENERIC );
384
385 MPI_CHK( grp->modp( N ) );
386
387 while( mpi_cmp_int( N, 0 ) < 0 )
388 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
389
390 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
391 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
392
393cleanup:
394 return( ret );
395}
396
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200397#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100398/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100399 * 192 bits in terms of t_uint
400 */
401#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
402
403/*
404 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
405 * -1 means let this chunk be 0
406 * a positive value i means A_i.
407 */
408#define P192_CHUNKS 3
409#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
410#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
411
412const signed char p192_tbl[][P192_CHUNKS] = {
413 { -1, 3, 3 }, /* S1 */
414 { 4, 4, -1 }, /* S2 */
415 { 5, 5, 5 }, /* S3 */
416};
417
418/*
419 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
420 */
421static int ecp_mod_p192( mpi *N )
422{
423 int ret;
424 unsigned char i, j, offset;
425 signed char chunk;
426 mpi tmp, acc;
427 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
428
429 tmp.s = 1;
430 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
431 tmp.p = tmp_p;
432
433 acc.s = 1;
434 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
435 acc.p = acc_p;
436
437 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
438
439 /*
440 * acc = T
441 */
442 memset( acc_p, 0, sizeof( acc_p ) );
443 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
444
445 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
446 {
447 /*
448 * tmp = S_i
449 */
450 memset( tmp_p, 0, sizeof( tmp_p ) );
451 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
452 {
453 chunk = p192_tbl[i][j];
454 if( chunk >= 0 )
455 memcpy( tmp_p + offset * P192_CHUNK_INT,
456 N->p + chunk * P192_CHUNK_INT,
457 P192_CHUNK_CHAR );
458 }
459
460 /*
461 * acc += tmp
462 */
463 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
464 }
465
466 MPI_CHK( mpi_copy( N, &acc ) );
467
468cleanup:
469 return( ret );
470}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200471#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100472
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200473#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100474/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100475 * Size of p521 in terms of t_uint
476 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100477#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100478
479/*
480 * Bits to keep in the most significant t_uint
481 */
482#if defined(POLARSS_HAVE_INT8)
483#define P521_MASK 0x01
484#else
485#define P521_MASK 0x01FF
486#endif
487
488/*
489 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100490 */
491static int ecp_mod_p521( mpi *N )
492{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100493 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100494 t_uint Mp[P521_SIZE_INT];
495 mpi M;
496
497 if( N->n < P521_SIZE_INT )
498 return( 0 );
499
500 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
501 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
502 Mp[P521_SIZE_INT - 1] &= P521_MASK;
503
504 M.s = 1;
505 M.n = P521_SIZE_INT;
506 M.p = Mp;
507
508 MPI_CHK( mpi_shift_r( N, 521 ) );
509
510 MPI_CHK( mpi_add_abs( N, N, &M ) );
511
512cleanup:
513 return( ret );
514}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200515#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100516
517/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100518 * Domain parameters for secp192r1
519 */
520#define SECP192R1_P \
521 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
522#define SECP192R1_B \
523 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
524#define SECP192R1_GX \
525 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
526#define SECP192R1_GY \
527 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
528#define SECP192R1_N \
529 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
530
531/*
532 * Domain parameters for secp224r1
533 */
534#define SECP224R1_P \
535 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
536#define SECP224R1_B \
537 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
538#define SECP224R1_GX \
539 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
540#define SECP224R1_GY \
541 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
542#define SECP224R1_N \
543 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
544
545/*
546 * Domain parameters for secp256r1
547 */
548#define SECP256R1_P \
549 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
550#define SECP256R1_B \
551 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
552#define SECP256R1_GX \
553 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
554#define SECP256R1_GY \
555 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
556#define SECP256R1_N \
557 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
558
559/*
560 * Domain parameters for secp384r1
561 */
562#define SECP384R1_P \
563 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
564 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
565#define SECP384R1_B \
566 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
567 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
568#define SECP384R1_GX \
569 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
570 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
571#define SECP384R1_GY \
572 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
573 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
574#define SECP384R1_N \
575 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
576 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
577
578/*
579 * Domain parameters for secp521r1
580 */
581#define SECP521R1_P \
582 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
583 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
584 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
585#define SECP521R1_B \
586 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
587 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
588 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
589#define SECP521R1_GX \
590 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
591 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
592 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
593#define SECP521R1_GY \
594 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
595 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
596 "3FAD0761353C7086A272C24088BE94769FD16650"
597#define SECP521R1_N \
598 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
599 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
600 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
601
602/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100603 * Set a group using well-known domain parameters
604 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100605int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100606{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100607 grp->id = id;
608
609 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100610 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200611#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100612 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100613 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100614 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100615 SECP192R1_P, SECP192R1_B,
616 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200617#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100618
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200619#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100620 case POLARSSL_ECP_DP_SECP224R1:
621 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100622 SECP224R1_P, SECP224R1_B,
623 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200624#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100625
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200626#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100627 case POLARSSL_ECP_DP_SECP256R1:
628 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100629 SECP256R1_P, SECP256R1_B,
630 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200631#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100632
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200633#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100634 case POLARSSL_ECP_DP_SECP384R1:
635 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100636 SECP384R1_P, SECP384R1_B,
637 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200638#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100639
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200640#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100641 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100642 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100643 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100644 SECP521R1_P, SECP521R1_B,
645 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200646#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100647 }
648
Paul Bakkerfd3eac52013-06-29 23:31:33 +0200649 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100650}
651
652/*
653 * Set a group from an ECParameters record (RFC 4492)
654 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100655int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100656{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100657 ecp_group_id id;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100658
659 /*
660 * We expect at least three bytes (see below)
661 */
662 if( len < 3 )
663 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
664
665 /*
666 * First byte is curve_type; only named_curve is handled
667 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100668 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100669 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
670
671 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100672 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100673 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100674 id = *(*buf)++;
675 id <<= 8;
676 id |= *(*buf)++;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100677 return ecp_use_known_dp( grp, id );
678}
679
680/*
681 * Write the ECParameters record corresponding to a group (RFC 4492)
682 */
683int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
684 unsigned char *buf, size_t blen )
685{
686 /*
687 * We are going to write 3 bytes (see below)
688 */
689 *olen = 3;
690 if( blen < *olen )
691 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
692
693 /*
694 * First byte is curve_type, always named_curve
695 */
696 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
697
698 /*
699 * Next two bytes are the namedcurve value
700 */
701 buf[0] = grp->id >> 8;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100702 buf[1] = grp->id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100703
704 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100705}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100706
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100707/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100708 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100709 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100710 * In order to guarantee that, we need to ensure that operands of
711 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100712 * bring the result back to this range.
713 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100714 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100715 */
716
717/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100718 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
719 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100720#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100721
722/*
723 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
724 */
725#define MOD_SUB( N ) \
726 while( mpi_cmp_int( &N, 0 ) < 0 ) \
727 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
728
729/*
730 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
731 */
732#define MOD_ADD( N ) \
733 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
734 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
735
736/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100737 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100738 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100739static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100740{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100741 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100742 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100743
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100744 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100745 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100746
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100747 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100748
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100749 /*
750 * X = X / Z^2 mod p
751 */
752 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
753 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
754 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100755
756 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100757 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100758 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100759 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
760 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100761
762 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100763 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100764 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100765 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100766
767cleanup:
768
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100769 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100770
771 return( ret );
772}
773
774/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100775 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100776 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100777 * (See for example Cohen's "A Course in Computational Algebraic Number
778 * Theory", Algorithm 10.3.4.)
779 *
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100780 * Warning: fails if one of the points is zero!
781 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100782 */
783static int ecp_normalize_many( const ecp_group *grp,
784 ecp_point T[], size_t t_len )
785{
786 int ret;
787 size_t i;
788 mpi *c, u, Zi, ZZi;
789
790 if( t_len < 2 )
791 return( ecp_normalize( grp, T ) );
792
Paul Bakker6e339b52013-07-03 13:37:05 +0200793 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100794 return( POLARSSL_ERR_ECP_GENERIC );
795
796 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
797 for( i = 0; i < t_len; i++ )
798 mpi_init( &c[i] );
799
800 /*
801 * c[i] = Z_0 * ... * Z_i
802 */
803 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
804 for( i = 1; i < t_len; i++ )
805 {
806 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
807 MOD_MUL( c[i] );
808 }
809
810 /*
811 * u = 1 / (Z_0 * ... * Z_n) mod P
812 */
813 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
814
815 for( i = t_len - 1; ; i-- )
816 {
817 /*
818 * Zi = 1 / Z_i mod p
819 * u = 1 / (Z_0 * ... * Z_i) mod P
820 */
821 if( i == 0 ) {
822 MPI_CHK( mpi_copy( &Zi, &u ) );
823 }
824 else
825 {
826 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
827 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
828 }
829
830 /*
831 * proceed as in normalize()
832 */
833 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
834 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
835 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
836 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
837 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
838
839 if( i == 0 )
840 break;
841 }
842
843cleanup:
844
845 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
846 for( i = 0; i < t_len; i++ )
847 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200848 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100849
850 return( ret );
851}
852
853
854/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100855 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
856 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100857static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
858 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100859{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100860 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100861 mpi T1, T2, T3, X, Y, Z;
862
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100863#if defined(POLARSSL_SELF_TEST)
864 dbl_count++;
865#endif
866
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100867 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100868 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100869
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100870 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
871 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
872
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100873 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
874 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
875 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
876 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
877 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
878 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
879 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
880 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
881 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
882 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100883
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100884 /*
885 * For Y = Y / 2 mod p, we must make sure that Y is even before
886 * using right-shift. No need to reduce mod p afterwards.
887 */
888 if( mpi_get_bit( &Y, 0 ) == 1 )
889 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
890 MPI_CHK( mpi_shift_r( &Y, 1 ) );
891
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100892 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
893 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
894 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
895 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
896 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
897 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100898
899 MPI_CHK( mpi_copy( &R->X, &X ) );
900 MPI_CHK( mpi_copy( &R->Y, &Y ) );
901 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100902
903cleanup:
904
905 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
906 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
907
908 return( ret );
909}
910
911/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100912 * Addition or subtraction: R = P + Q or R = P + Q,
913 * mixed affine-Jacobian coordinates (GECC 3.22)
914 *
915 * The coordinates of Q must be normalized (= affine),
916 * but those of P don't need to. R is not normalized.
917 *
918 * If sign >= 0, perform addition, otherwise perform subtraction,
919 * taking advantage of the fact that, for Q != 0, we have
920 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100921 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100922static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100923 const ecp_point *P, const ecp_point *Q,
924 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100925{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100926 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100927 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100928
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100929#if defined(POLARSSL_SELF_TEST)
930 add_count++;
931#endif
932
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100933 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100934 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100935 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100936 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100937 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
938 return( ecp_copy( R, P ) );
939
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100940 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
941 {
942 ret = ecp_copy( R, Q );
943
944 /*
945 * -R.Y mod P = P - R.Y unless R.Y == 0
946 */
947 if( ret == 0 && sign < 0)
948 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
949 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
950
951 return( ret );
952 }
953
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100954 /*
955 * Make sure Q coordinates are normalized
956 */
957 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
958 return( POLARSSL_ERR_ECP_GENERIC );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100959
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100960 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
961 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100962
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100963 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
964 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
965 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
966 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100967
968 /*
969 * For subtraction, -Q.Y should have been used instead of Q.Y,
970 * so we replace T2 by -T2, which is P - T2 mod P
971 */
972 if( sign < 0 )
973 {
974 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
975 MOD_SUB( T2 );
976 }
977
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100978 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
979 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100980
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100981 if( mpi_cmp_int( &T1, 0 ) == 0 )
982 {
983 if( mpi_cmp_int( &T2, 0 ) == 0 )
984 {
985 ret = ecp_double_jac( grp, R, P );
986 goto cleanup;
987 }
988 else
989 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100990 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100991 goto cleanup;
992 }
993 }
994
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100995 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
996 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
997 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
998 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
999 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1000 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1001 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1002 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1003 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1004 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1005 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1006 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001007
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001008 MPI_CHK( mpi_copy( &R->X, &X ) );
1009 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1010 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001011
1012cleanup:
1013
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001014 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1015 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001016
1017 return( ret );
1018}
1019
1020/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001021 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001022 */
1023int ecp_add( const ecp_group *grp, ecp_point *R,
1024 const ecp_point *P, const ecp_point *Q )
1025{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001026 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001027
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001028 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1029 MPI_CHK( ecp_normalize( grp, R ) );
1030
1031cleanup:
1032 return( ret );
1033}
1034
1035/*
1036 * Subtraction: R = P - Q, result's coordinates normalized
1037 */
1038int ecp_sub( const ecp_group *grp, ecp_point *R,
1039 const ecp_point *P, const ecp_point *Q )
1040{
1041 int ret;
1042
1043 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001044 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001045
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001046cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001047 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001048}
1049
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001050/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001051 * Compute a modified width-w non-adjacent form (NAF) of a number,
1052 * with a fixed pattern for resistance to SPA/timing attacks,
1053 * see <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1054 * (The resulting multiplication algorithm can also been seen as a
1055 * modification of 2^w-ary multiplication, with signed coefficients,
1056 * all of them odd.)
1057 *
1058 * Input:
1059 * m must be an odd positive mpi less than w * k bits long
1060 * x must be an array of k elements
1061 * w must be less than a certain maximum (currently 8)
1062 *
1063 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1064 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1065 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1066 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1067 *
1068 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1069 * p. 335 of the cited reference, here we return only u, not d_w since
1070 * it is known that the other d_w[j] will be 0. Moreover, the returned
1071 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1072 * that u_i is odd. Also, since we always select a positive value for d
1073 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1074 * does. Finally, there is an off-by-one error in the reference: the
1075 * last index should be k-1, not k.
1076 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001077static int ecp_w_naf_fixed( signed char x[], size_t k,
1078 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001079{
1080 int ret;
1081 unsigned int i, u, mask, carry;
1082 mpi M;
1083
1084 mpi_init( &M );
1085
1086 MPI_CHK( mpi_copy( &M, m ) );
1087 mask = ( 1 << w ) - 1;
1088 carry = 1 << ( w - 1 );
1089
1090 for( i = 0; i < k; i++ )
1091 {
1092 u = M.p[0] & mask;
1093
1094 if( ( u & 1 ) == 0 && i > 0 )
1095 x[i - 1] -= carry;
1096
1097 x[i] = u >> 1;
1098 mpi_shift_r( &M, w );
1099 }
1100
1101 /*
1102 * We should have consumed all the bits now
1103 */
1104 if( mpi_cmp_int( &M, 0 ) != 0 )
1105 ret = POLARSSL_ERR_ECP_GENERIC;
1106
1107cleanup:
1108
1109 mpi_free( &M );
1110
1111 return( ret );
1112}
1113
1114/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001115 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1116 * The table is filled with T[i] = (2 * i + 1) P.
1117 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001118static int ecp_precompute( const ecp_group *grp,
1119 ecp_point T[], size_t t_len,
1120 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001121{
1122 int ret;
1123 size_t i;
1124 ecp_point PP;
1125
1126 ecp_point_init( &PP );
1127
1128 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1129
1130 MPI_CHK( ecp_copy( &T[0], P ) );
1131
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001132 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001133 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1134
1135 /*
1136 * T[0] = P already has normalized coordinates
1137 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001138 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001139
1140cleanup:
1141
1142 ecp_point_free( &PP );
1143
1144 return( ret );
1145}
1146
1147/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001148 * Maximum length of the precomputed table
1149 */
1150#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1151
1152/*
1153 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1154 * (that is: grp->nbits / w + 1)
1155 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1156 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001157#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001158
1159/*
1160 * Integer multiplication: R = m * P
1161 *
1162 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed()
1163 * and <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
1164 *
1165 * This function executes a fixed number of operations for
1166 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001167 */
1168int ecp_mul( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001169 const mpi *m, const ecp_point *P,
1170 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001171{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001172 int ret;
1173 unsigned char w, m_is_odd;
1174 size_t pre_len, naf_len, i, j;
1175 signed char naf[ MAX_NAF_LEN ];
1176 ecp_point Q, T[ MAX_PRE_LEN ];
1177 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001178
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001179 ((void) f_rng);
1180 ((void) p_rng);
1181
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001182 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001183 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001184
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001185 w = grp->nbits >= 521 ? 6 :
1186 grp->nbits >= 224 ? 5 :
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001187 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001188
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001189 /*
1190 * Make sure w is within the limits.
1191 * The last test ensures that none of the precomputed points is zero,
1192 * which wouldn't be handled correctly by ecp_normalize_many().
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001193 * It is only useful for very small curves, as used in the test suite.
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001194 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001195 if( w > POLARSSL_ECP_WINDOW_SIZE )
1196 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001197 if( w < 2 || w >= grp->nbits )
1198 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001199
1200 pre_len = 1 << ( w - 1 );
1201 naf_len = grp->nbits / w + 1;
1202
1203 mpi_init( &M );
1204 ecp_point_init( &Q );
1205 for( i = 0; i < pre_len; i++ )
1206 ecp_point_init( &T[i] );
1207
1208 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1209
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001210 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001211 * Make sure M is odd:
1212 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001213 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001214 MPI_CHK( mpi_copy( &M, m ) );
1215 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001216
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001217 /*
1218 * Compute the fixed-pattern NAF and precompute odd multiples
1219 */
1220 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1221 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001222
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001223 /*
1224 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1225 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1226 *
1227 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1228 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1229 * == T[ - naf[i] - 1 ]
1230 */
1231 MPI_CHK( ecp_set_zero( &Q ) );
1232 i = naf_len - 1;
1233 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001234 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001235 if( naf[i] < 0 )
1236 {
1237 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1238 }
1239 else
1240 {
1241 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1242 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001243
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001244 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001245 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001246 i--;
1247
1248 for( j = 0; j < w; j++ )
1249 {
1250 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1251 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001252 }
1253
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001254 /*
1255 * Now get m * P from M * P.
1256 * Since we don't need T[] any more, we can recycle it:
1257 * we already have T[0] = P, now set T[1] = 2 * P.
1258 */
1259 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1260 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001261
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001262
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001263cleanup:
1264
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001265 mpi_free( &M );
1266 ecp_point_free( &Q );
1267 for( i = 0; i < pre_len; i++ )
1268 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001269
1270 return( ret );
1271}
1272
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001273/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001274 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1275 */
1276int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1277{
1278 int ret;
1279 mpi YY, RHS;
1280
1281 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
1282 return( POLARSSL_ERR_ECP_GENERIC );
1283
1284 /*
1285 * pt coordinates must be normalized for our checks
1286 */
1287 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1288 return( POLARSSL_ERR_ECP_GENERIC );
1289
1290 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1291 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1292 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1293 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
1294 return( POLARSSL_ERR_ECP_GENERIC );
1295
1296 mpi_init( &YY ); mpi_init( &RHS );
1297
1298 /*
1299 * YY = Y^2
1300 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
1301 */
1302 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1303 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
1304 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1305 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1306 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
1307
1308 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
1309 ret = POLARSSL_ERR_ECP_GENERIC;
1310
1311cleanup:
1312
1313 mpi_free( &YY ); mpi_free( &RHS );
1314
1315 return( ret );
1316}
1317
1318/*
1319 * Check that an mpi is valid as a private key (SEC1 3.2)
1320 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001321int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001322{
1323 /* We want 1 <= d <= N-1 */
1324 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
1325 return( POLARSSL_ERR_ECP_GENERIC );
1326
1327 return( 0 );
1328}
1329
1330/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001331 * Generate a keypair (SEC1 3.2.1)
1332 */
1333int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1334 int (*f_rng)(void *, unsigned char *, size_t),
1335 void *p_rng )
1336{
1337 int count = 0;
1338 size_t n_size = (grp->nbits + 7) / 8;
1339
1340 /*
1341 * Generate d such that 1 <= n < N
1342 */
1343 do
1344 {
1345 mpi_fill_random( d, n_size, f_rng, p_rng );
1346
1347 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1348 mpi_shift_r( d, 1 );
1349
1350 if( count++ > 10 )
1351 return( POLARSSL_ERR_ECP_GENERIC );
1352 }
1353 while( mpi_cmp_int( d, 1 ) < 0 );
1354
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001355 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001356}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001357
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001358#if defined(POLARSSL_SELF_TEST)
1359
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001360/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001361 * Checkup routine
1362 */
1363int ecp_self_test( int verbose )
1364{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001365 int ret;
1366 size_t i;
1367 ecp_group grp;
1368 ecp_point R;
1369 mpi m;
1370 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001371 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001372 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001373 "000000000000000000000000000000000000000000000000", /* zero */
1374 "000000000000000000000000000000000000000000000001", /* one */
1375 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1376 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001377 "400000000000000000000000000000000000000000000000",
1378 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1379 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001380 };
1381
1382 ecp_group_init( &grp );
1383 ecp_point_init( &R );
1384 mpi_init( &m );
1385
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001386#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001387 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001388#else
1389#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1390 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1391#else
1392#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1393 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1394#else
1395#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1396 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1397#else
1398#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1399 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1400#else
1401#error No curves defines
1402#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1403#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1404#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1405#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1406#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001407
1408 if( verbose != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001409 printf( " ECP test #1 (resistance to simple timing attacks): " );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001410
1411 add_count = 0;
1412 dbl_count = 0;
1413 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001414 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001415
1416 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1417 {
1418 add_c_prev = add_count;
1419 dbl_c_prev = dbl_count;
1420 add_count = 0;
1421 dbl_count = 0;
1422
1423 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001424 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001425
1426 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1427 {
1428 if( verbose != 0 )
1429 printf( "failed (%zu)\n", i );
1430
1431 ret = 1;
1432 goto cleanup;
1433 }
1434 }
1435
1436 if( verbose != 0 )
1437 printf( "passed\n" );
1438
1439cleanup:
1440
1441 if( ret < 0 && verbose != 0 )
1442 printf( "Unexpected error, return code = %08X\n", ret );
1443
1444 ecp_group_free( &grp );
1445 ecp_point_free( &R );
1446 mpi_free( &m );
1447
1448 if( verbose != 0 )
1449 printf( "\n" );
1450
1451 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001452}
1453
1454#endif
1455
1456#endif