blob: 08ca68250807144e83d2c7ce4f3011d263cf3c2d [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
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é-Gonnard07de4b12013-09-02 16:26:04 +020033 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020034 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
35 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
36 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
37 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010038 *
39 * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
40 * render ECC resistant against Side Channel Attacks. IACR Cryptology
41 * ePrint Archive, 2004, vol. 2004, p. 342.
42 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010043 */
44
45#include "polarssl/config.h"
46
47#if defined(POLARSSL_ECP_C)
48
49#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020050
51#if defined(POLARSSL_MEMORY_C)
52#include "polarssl/memory.h"
53#else
54#define polarssl_malloc malloc
55#define polarssl_free free
56#endif
57
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010058#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010059
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010060#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
61 !defined(EFI32)
62#define strcasecmp _stricmp
63#endif
64
Paul Bakker6a6087e2013-10-28 18:53:08 +010065#if defined(_MSC_VER) && !defined(inline)
66#define inline _inline
67#else
68#if defined(__ARMCC_VERSION) && !defined(inline)
69#define inline __inline
70#endif /* __ARMCC_VERSION */
71#endif /*_MSC_VER */
72
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010073#if defined(POLARSSL_SELF_TEST)
74/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010075 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020076 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010077 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010078static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010079#endif
80
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010081/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020082 * List of supported curves:
83 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020084 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020085 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020086 * - readable name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020087 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010088static const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020089{
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020090#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010091 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020092#endif
93#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010094 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020095#endif
96#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010097 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020098#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020099#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200100 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200101#endif
102#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200103 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200104#endif
105#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200106 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200107#endif
108#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200109 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200110#endif
111#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200112 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200113#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200114 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200115};
116
117/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200118 * List of supported curves and associated info
119 */
120const ecp_curve_info *ecp_curve_list( void )
121{
122 return ecp_supported_curves;
123}
124
125/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200126 * Get the curve info for the internal identifer
127 */
128const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
129{
130 const ecp_curve_info *curve_info;
131
132 for( curve_info = ecp_curve_list();
133 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
134 curve_info++ )
135 {
136 if( curve_info->grp_id == grp_id )
137 return( curve_info );
138 }
139
140 return( NULL );
141}
142
143/*
144 * Get the curve info from the TLS identifier
145 */
146const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
147{
148 const ecp_curve_info *curve_info;
149
150 for( curve_info = ecp_curve_list();
151 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
152 curve_info++ )
153 {
154 if( curve_info->tls_id == tls_id )
155 return( curve_info );
156 }
157
158 return( NULL );
159}
160
161/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100162 * Get the curve info from the name
163 */
164const ecp_curve_info *ecp_curve_info_from_name( const char *name )
165{
166 const ecp_curve_info *curve_info;
167
168 for( curve_info = ecp_curve_list();
169 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
170 curve_info++ )
171 {
172 if( strcasecmp( curve_info->name, name ) == 0 )
173 return( curve_info );
174 }
175
176 return( NULL );
177}
178
179/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100180 * Tell if a group structure is associated to a Montgomery curve
181 */
182static inline int ecp_is_montgomery( const ecp_group *grp )
183{
184 return( grp->G.X.p != NULL && grp->G.Y.p == NULL );
185}
186
187/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100188 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100189 */
190void ecp_point_init( ecp_point *pt )
191{
192 if( pt == NULL )
193 return;
194
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100195 mpi_init( &pt->X );
196 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100197 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100198}
199
200/*
201 * Initialize (the components of) a group
202 */
203void ecp_group_init( ecp_group *grp )
204{
205 if( grp == NULL )
206 return;
207
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200208 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100209}
210
211/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200212 * Initialize (the components of) a key pair
213 */
214void ecp_keypair_init( ecp_keypair *key )
215{
216 if ( key == NULL )
217 return;
218
219 ecp_group_init( &key->grp );
220 mpi_init( &key->d );
221 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200222}
223
224/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100225 * Unallocate (the components of) a point
226 */
227void ecp_point_free( ecp_point *pt )
228{
229 if( pt == NULL )
230 return;
231
232 mpi_free( &( pt->X ) );
233 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100234 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100235}
236
237/*
238 * Unallocate (the components of) a group
239 */
240void ecp_group_free( ecp_group *grp )
241{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200242 size_t i;
243
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100244 if( grp == NULL )
245 return;
246
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100247 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200248 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100249 mpi_free( &grp->B );
250 ecp_point_free( &grp->G );
251 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200252
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200253 if( grp->T != NULL )
254 {
255 for( i = 0; i < grp->T_size; i++ )
256 ecp_point_free( &grp->T[i] );
257 polarssl_free( grp->T );
258 }
259
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200260 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100261}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100262
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100263/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200264 * Unallocate (the components of) a key pair
265 */
266void ecp_keypair_free( ecp_keypair *key )
267{
268 if ( key == NULL )
269 return;
270
271 ecp_group_free( &key->grp );
272 mpi_free( &key->d );
273 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200274}
275
276/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200277 * Copy the contents of a point
278 */
279int ecp_copy( ecp_point *P, const ecp_point *Q )
280{
281 int ret;
282
283 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
284 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
285 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
286
287cleanup:
288 return( ret );
289}
290
291/*
292 * Copy the contents of a group object
293 */
294int ecp_group_copy( ecp_group *dst, const ecp_group *src )
295{
296 return ecp_use_known_dp( dst, src->id );
297}
298
299/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100300 * Set point to zero
301 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100302int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100303{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100304 int ret;
305
306 MPI_CHK( mpi_lset( &pt->X , 1 ) );
307 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
308 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
309
310cleanup:
311 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100312}
313
314/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100315 * Tell if a point is zero
316 */
317int ecp_is_zero( ecp_point *pt )
318{
319 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
320}
321
322/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100323 * Import a non-zero point from ASCII strings
324 */
325int ecp_point_read_string( ecp_point *P, int radix,
326 const char *x, const char *y )
327{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100328 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100329
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100330 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
331 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100332 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100333
334cleanup:
335 return( ret );
336}
337
338/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100339 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100340 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100341int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100342 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100343 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100344{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200345 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100346 size_t plen;
347
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100348 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
349 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100350 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100351
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100352 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100353 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100354 */
355 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
356 {
357 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100358 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100359
360 buf[0] = 0x00;
361 *olen = 1;
362
363 return( 0 );
364 }
365
366 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100367
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100368 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
369 {
370 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100371
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100372 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100373 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100374
375 buf[0] = 0x04;
376 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
377 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
378 }
379 else if( format == POLARSSL_ECP_PF_COMPRESSED )
380 {
381 *olen = plen + 1;
382
383 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100384 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100385
386 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
387 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
388 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100389
390cleanup:
391 return( ret );
392}
393
394/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100395 * Import a point from unsigned binary data (SEC1 2.3.4)
396 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100397int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
398 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100399 int ret;
400 size_t plen;
401
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100402 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100403 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100404
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100405 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100406
407 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100408 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100409
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100410 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
411 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
412 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100413
414cleanup:
415 return( ret );
416}
417
418/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100419 * Import a point from a TLS ECPoint record (RFC 4492)
420 * struct {
421 * opaque point <1..2^8-1>;
422 * } ECPoint;
423 */
424int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100425 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100426{
427 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100428 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100429
430 /*
431 * We must have at least two bytes (1 for length, at least of for data)
432 */
433 if( buf_len < 2 )
434 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
435
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100436 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100437 if( data_len < 1 || data_len > buf_len - 1 )
438 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
439
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100440 /*
441 * Save buffer start for read_binary and update buf
442 */
443 buf_start = *buf;
444 *buf += data_len;
445
446 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100447}
448
449/*
450 * Export a point as a TLS ECPoint record (RFC 4492)
451 * struct {
452 * opaque point <1..2^8-1>;
453 * } ECPoint;
454 */
455int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100456 int format, size_t *olen,
457 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100458{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100459 int ret;
460
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100461 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100462 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100463 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100464 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100465 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
466
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100467 if( ( ret = ecp_point_write_binary( grp, pt, format,
468 olen, buf + 1, blen - 1) ) != 0 )
469 return( ret );
470
471 /*
472 * write length to the first byte and update total length
473 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200474 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100475 ++*olen;
476
477 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100478}
479
480/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200481 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200482 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200483int ecp_group_read_string( ecp_group *grp, int radix,
484 const char *p, const char *b,
485 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100486{
487 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100488
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100489 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200490 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100491 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
492 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
493 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
494
495 grp->pbits = mpi_msb( &grp->P );
496 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100497
498cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200499 if( ret != 0 )
500 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200501
502 return( ret );
503}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200504
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100505/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100506 * Set a group from an ECParameters record (RFC 4492)
507 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100508int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100509{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200510 uint16_t tls_id;
511 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100512
513 /*
514 * We expect at least three bytes (see below)
515 */
516 if( len < 3 )
517 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
518
519 /*
520 * First byte is curve_type; only named_curve is handled
521 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100522 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100523 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
524
525 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100526 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100527 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200528 tls_id = *(*buf)++;
529 tls_id <<= 8;
530 tls_id |= *(*buf)++;
531
532 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
533 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
534
535 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100536}
537
538/*
539 * Write the ECParameters record corresponding to a group (RFC 4492)
540 */
541int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
542 unsigned char *buf, size_t blen )
543{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200544 const ecp_curve_info *curve_info;
545
546 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
547 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200548
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100549 /*
550 * We are going to write 3 bytes (see below)
551 */
552 *olen = 3;
553 if( blen < *olen )
554 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
555
556 /*
557 * First byte is curve_type, always named_curve
558 */
559 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
560
561 /*
562 * Next two bytes are the namedcurve value
563 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200564 buf[0] = curve_info->tls_id >> 8;
565 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100566
567 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100568}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100569
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200570/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200571 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
572 * See the documentation of struct ecp_group.
573 *
574 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200575 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200576static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200577{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200578 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200579
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200580 if( grp->modp == NULL )
581 return( mpi_mod_mpi( N, N, &grp->P ) );
582
583 /* N->s < 0 is a much faster test, which fails only if N is 0 */
584 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
585 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200586 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200587 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200588 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200589
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200590 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200591
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200592 /* N->s < 0 is a much faster test, which fails only if N is 0 */
593 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
594 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200595
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200596 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
597 /* we known P, N and the result are positive */
598 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200599
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200600cleanup:
601 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200602}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200603
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100604/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100605 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100606 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100607 * In order to guarantee that, we need to ensure that operands of
608 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100609 * bring the result back to this range.
610 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100611 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100612 */
613
614/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100615 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
616 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100617#if defined(POLARSSL_SELF_TEST)
618#define INC_MUL_COUNT mul_count++;
619#else
620#define INC_MUL_COUNT
621#endif
622
623#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
624 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100625
626/*
627 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200628 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100629 */
630#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200631 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100632 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
633
634/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200635 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
636 * We known P, N and the result are positive, so sub_abs is correct, and
637 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100638 */
639#define MOD_ADD( N ) \
640 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200641 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100642
643/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100644 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100645 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100646 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100647static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100648{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100649 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100650 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100651
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100652 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100653 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100654
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100655 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100656
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100657 /*
658 * X = X / Z^2 mod p
659 */
660 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
661 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
662 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100663
664 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100665 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100666 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100667 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
668 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100669
670 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100671 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100672 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100673 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100674
675cleanup:
676
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100677 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100678
679 return( ret );
680}
681
682/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100683 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100684 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100685 * (See for example Cohen's "A Course in Computational Algebraic Number
686 * Theory", Algorithm 10.3.4.)
687 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200688 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100689 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100690 *
691 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100692 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100693static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100694 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100695{
696 int ret;
697 size_t i;
698 mpi *c, u, Zi, ZZi;
699
700 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100701 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100702
Paul Bakker6e339b52013-07-03 13:37:05 +0200703 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200704 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100705
706 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
707 for( i = 0; i < t_len; i++ )
708 mpi_init( &c[i] );
709
710 /*
711 * c[i] = Z_0 * ... * Z_i
712 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100713 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100714 for( i = 1; i < t_len; i++ )
715 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100716 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100717 MOD_MUL( c[i] );
718 }
719
720 /*
721 * u = 1 / (Z_0 * ... * Z_n) mod P
722 */
723 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
724
725 for( i = t_len - 1; ; i-- )
726 {
727 /*
728 * Zi = 1 / Z_i mod p
729 * u = 1 / (Z_0 * ... * Z_i) mod P
730 */
731 if( i == 0 ) {
732 MPI_CHK( mpi_copy( &Zi, &u ) );
733 }
734 else
735 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100736 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
737 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100738 }
739
740 /*
741 * proceed as in normalize()
742 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100743 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
744 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
745 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
746 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
747 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100748
749 if( i == 0 )
750 break;
751 }
752
753cleanup:
754
755 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
756 for( i = 0; i < t_len; i++ )
757 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200758 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100759
760 return( ret );
761}
762
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100763/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100764 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
765 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
766 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100767static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100768 ecp_point *Q,
769 unsigned char inv )
770{
771 int ret;
772 unsigned char nonzero;
773 mpi mQY;
774
775 mpi_init( &mQY );
776
777 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
778 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
779 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
780 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
781
782cleanup:
783 mpi_free( &mQY );
784
785 return( ret );
786}
787
788/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200789 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200790 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200791 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200792 * with heavy variable renaming, some reordering and one minor modification
793 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
794 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100795 *
796 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200797 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200798static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
799 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200800{
801 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200802 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200803
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200804#if defined(POLARSSL_SELF_TEST)
805 dbl_count++;
806#endif
807
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200808 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
809 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200810
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200811 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
812 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
813 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
814 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
815 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
816 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
817 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
818 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
819 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
820 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
821 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
822 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
823 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
824 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
825 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
826 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
827 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
828 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
829 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
830 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
831 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
832 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
833 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
834 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200835
836 MPI_CHK( mpi_copy( &R->X, &X3 ) );
837 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
838 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
839
840cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200841 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
842 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200843
844 return( ret );
845}
846
847/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100848 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100849 *
850 * The coordinates of Q must be normalized (= affine),
851 * but those of P don't need to. R is not normalized.
852 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100853 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
854 * None of these cases can happen as intermediate step in ecp_mul():
855 * - at each step, P, Q and R are multiples of the base point, the factor
856 * being less than its order, so none of them is zero;
857 * - Q is an odd multiple of the base point, P an even multiple,
858 * due to the choice of precomputed points in the modified comb method.
859 * So branches for these cases do not leak secret information.
860 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100861 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100862 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100863static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100864 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100865{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100866 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100867 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100868
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100869#if defined(POLARSSL_SELF_TEST)
870 add_count++;
871#endif
872
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100873 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100874 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100875 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100876 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
877 return( ecp_copy( R, Q ) );
878
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100879 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
880 return( ecp_copy( R, P ) );
881
882 /*
883 * Make sure Q coordinates are normalized
884 */
885 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200886 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100887
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100888 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
889 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100890
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100891 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
892 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
893 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
894 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
895 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
896 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100897
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100898 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100899 if( mpi_cmp_int( &T1, 0 ) == 0 )
900 {
901 if( mpi_cmp_int( &T2, 0 ) == 0 )
902 {
903 ret = ecp_double_jac( grp, R, P );
904 goto cleanup;
905 }
906 else
907 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100908 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100909 goto cleanup;
910 }
911 }
912
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100913 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
914 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
915 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
916 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
917 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
918 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
919 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
920 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
921 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
922 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
923 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
924 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100925
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100926 MPI_CHK( mpi_copy( &R->X, &X ) );
927 MPI_CHK( mpi_copy( &R->Y, &Y ) );
928 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100929
930cleanup:
931
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100932 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
933 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100934
935 return( ret );
936}
937
938/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100939 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100940 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100941 */
942int ecp_add( const ecp_group *grp, ecp_point *R,
943 const ecp_point *P, const ecp_point *Q )
944{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100945 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100946
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100947 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100948 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100949
950cleanup:
951 return( ret );
952}
953
954/*
955 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100956 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100957 */
958int ecp_sub( const ecp_group *grp, ecp_point *R,
959 const ecp_point *P, const ecp_point *Q )
960{
961 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100962 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100963
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100964 ecp_point_init( &mQ );
965
966 /* mQ = - Q */
967 ecp_copy( &mQ, Q );
968 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
969 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
970
971 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100972 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100973
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100974cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100975 ecp_point_free( &mQ );
976
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100977 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100978}
979
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100980/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200981 * Randomize jacobian coordinates:
982 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100983 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +0100984 *
985 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200986 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +0100987static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200988 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
989{
990 int ret;
991 mpi l, ll;
992 size_t p_size = (grp->pbits + 7) / 8;
993 int count = 0;
994
995 mpi_init( &l ); mpi_init( &ll );
996
997 /* Generate l such that 1 < l < p */
998 do
999 {
1000 mpi_fill_random( &l, p_size, f_rng, p_rng );
1001
1002 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1003 mpi_shift_r( &l, 1 );
1004
1005 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001006 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001007 }
1008 while( mpi_cmp_int( &l, 1 ) <= 0 );
1009
1010 /* Z = l * Z */
1011 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1012
1013 /* X = l^2 * X */
1014 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1015 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1016
1017 /* Y = l^3 * Y */
1018 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1019 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1020
1021cleanup:
1022 mpi_free( &l ); mpi_free( &ll );
1023
1024 return( ret );
1025}
1026
1027/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001028 * Check and define parameters used by the comb method (see below for details)
1029 */
1030#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1031#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1032#endif
1033
1034/* d = ceil( n / w ) */
1035#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1036
1037/* number of precomputed points */
1038#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1039
1040/*
1041 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001042 *
1043 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001044 * modified version that provides resistance to SPA by avoiding zero
1045 * digits in the representation as in [3]. We modify the method further by
1046 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001047 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001048 *
1049 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1050 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1051 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001052 *
1053 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001054 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001055 * - w is the size, ie number of teeth, of the comb, and must be between
1056 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001057 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1058 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001059 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001060static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001061 unsigned char w, const mpi *m )
1062{
1063 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001064 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001065
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001066 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001067
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001068 /* First get the classical comb values (except for x_d = 0) */
1069 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001070 for( j = 0; j < w; j++ )
1071 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1072
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001073 /* Now make sure x_1 .. x_d are odd */
1074 c = 0;
1075 for( i = 1; i <= d; i++ )
1076 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001077 /* Add carry and update it */
1078 cc = x[i] & c;
1079 x[i] = x[i] ^ c;
1080 c = cc;
1081
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001082 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001083 adjust = 1 - ( x[i] & 0x01 );
1084 c |= x[i] & ( x[i-1] * adjust );
1085 x[i] = x[i] ^ ( x[i-1] * adjust );
1086 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001087 }
1088}
1089
1090/*
1091 * Precompute points for the comb method
1092 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001093 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1094 * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001095 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001096 * T must be able to hold 2^{w - 1} elements
1097 *
1098 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001099 */
1100static int ecp_precompute_comb( const ecp_group *grp,
1101 ecp_point T[], const ecp_point *P,
1102 unsigned char w, size_t d )
1103{
1104 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001105 unsigned char i, k;
1106 size_t j;
1107 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001108
1109 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001110 * Set T[0] = P and
1111 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001112 */
1113 MPI_CHK( ecp_copy( &T[0], P ) );
1114
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001115 k = 0;
1116 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001117 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001118 cur = T + i;
1119 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001120 for( j = 0; j < d; j++ )
1121 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001122
1123 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001124 }
1125
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001126 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001127
1128 /*
1129 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001130 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001131 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001132 k = 0;
1133 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001134 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001135 j = i;
1136 while( j-- )
1137 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001138 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001139 TT[k++] = &T[i + j];
1140 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001141 }
1142
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001143 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001144
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001145 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001146 * Post-precessing: reclaim some memory by
1147 * - not storing Z (always 1)
1148 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001149 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001150 */
1151 for( i = 0; i < ( 1U << (w-1) ); i++ )
1152 {
1153 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001154 mpi_shrink( &T[i].X, grp->P.n );
1155 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001156 }
1157
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001158cleanup:
1159 return( ret );
1160}
1161
1162/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001163 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001164 */
1165static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001166 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001167 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001168{
1169 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001170 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001171
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001172 /* Ignore the "sign" bit and scale down */
1173 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001174
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001175 /* Read the whole table to thwart cache-based timing attacks */
1176 for( j = 0; j < t_len; j++ )
1177 {
1178 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1179 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1180 }
1181
1182 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001183 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1184
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001185 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001186 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001187
1188cleanup:
1189 return( ret );
1190}
1191
1192/*
1193 * Core multiplication algorithm for the (modified) comb method.
1194 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001195 *
1196 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001197 */
1198static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001199 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001200 const unsigned char x[], size_t d,
1201 int (*f_rng)(void *, unsigned char *, size_t),
1202 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001203{
1204 int ret;
1205 ecp_point Txi;
1206 size_t i;
1207
1208 ecp_point_init( &Txi );
1209
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001210 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001211 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001212 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001213 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001214 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001215
1216 while( i-- != 0 )
1217 {
1218 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001219 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001220 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001221 }
1222
1223cleanup:
1224 ecp_point_free( &Txi );
1225
1226 return( ret );
1227}
1228
1229/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001230 * Multiplication using the comb method,
1231 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001232 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001233static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1234 const mpi *m, const ecp_point *P,
1235 int (*f_rng)(void *, unsigned char *, size_t),
1236 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001237{
1238 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001239 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1240 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001241 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001242 ecp_point *T;
1243 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001244
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001245 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001246 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001247
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001248 /* we need N to be odd to trnaform m in an odd number, check now */
1249 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1250 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1251
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001252 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001253 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001254 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001255 * (see costs of the various parts, with 1S = 1M)
1256 */
1257 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001258
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001259 /*
1260 * If P == G, pre-compute a bit more, since this may be re-used later.
1261 * Just adding one ups the cost of the first mul by at most 3%.
1262 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001263 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1264 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001265 if( p_eq_g )
1266 w++;
1267
1268 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001269 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001270 * (The last test is useful only for very small curves in the test suite.)
1271 */
1272 if( w > POLARSSL_ECP_WINDOW_SIZE )
1273 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001274 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001275 w = 2;
1276
1277 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001278 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001279 d = ( grp->nbits + w - 1 ) / w;
1280
1281 /*
1282 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001283 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001284 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001285 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001286
1287 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001288 {
1289 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1290 if( T == NULL )
1291 {
1292 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1293 goto cleanup;
1294 }
1295
1296 for( i = 0; i < pre_len; i++ )
1297 ecp_point_init( &T[i] );
1298
1299 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1300
1301 if( p_eq_g )
1302 {
1303 grp->T = T;
1304 grp->T_size = pre_len;
1305 }
1306 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001307
1308 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001309 * Make sure M is odd (M = m or M = N - m, since N is odd)
1310 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001311 */
1312 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001313 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001314 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1315 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001316
1317 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001318 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001319 */
1320 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001321 MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001322
1323 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001324 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001325 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001326 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1327 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001328
1329cleanup:
1330
1331 if( T != NULL && ! p_eq_g )
1332 {
1333 for( i = 0; i < pre_len; i++ )
1334 ecp_point_free( &T[i] );
1335 polarssl_free( T );
1336 }
1337
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001338 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001339 mpi_free( &mm );
1340
1341 if( ret != 0 )
1342 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001343
1344 return( ret );
1345}
1346
1347/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001348 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1349 * Cost: 1M + 1I
1350 */
1351static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1352{
1353 int ret;
1354
1355 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1356 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1357 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1358
1359cleanup:
1360 return( ret );
1361}
1362
1363/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001364 * Randomize projective x/z coordinates:
1365 * (X, Z) -> (l X, l Z) for random l
1366 * This is sort of the reverse operation of ecp_normalize_mxz().
1367 *
1368 * This countermeasure was first suggested in [2].
1369 * Cost: 2M
1370 */
1371static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1372 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1373{
1374 int ret;
1375 mpi l;
1376 size_t p_size = (grp->pbits + 7) / 8;
1377 int count = 0;
1378
1379 mpi_init( &l );
1380
1381 /* Generate l such that 1 < l < p */
1382 do
1383 {
1384 mpi_fill_random( &l, p_size, f_rng, p_rng );
1385
1386 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1387 mpi_shift_r( &l, 1 );
1388
1389 if( count++ > 10 )
1390 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1391 }
1392 while( mpi_cmp_int( &l, 1 ) <= 0 );
1393
1394 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1395 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1396
1397cleanup:
1398 mpi_free( &l );
1399
1400 return( ret );
1401}
1402
1403/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001404 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1405 * for Montgomery curves in x/z coordinates.
1406 *
1407 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1408 * with
1409 * d = X1
1410 * P = (X2, Z2)
1411 * Q = (X3, Z3)
1412 * R = (X4, Z4)
1413 * S = (X5, Z5)
1414 * and eliminating temporary variables tO, ..., t4.
1415 *
1416 * Cost: 5M + 4S
1417 */
1418static int ecp_double_add_mxz( const ecp_group *grp,
1419 ecp_point *R, ecp_point *S,
1420 const ecp_point *P, const ecp_point *Q,
1421 const mpi *d )
1422{
1423 int ret;
1424 mpi A, AA, B, BB, E, C, D, DA, CB;
1425
1426 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1427 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1428 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1429
1430 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1431 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1432 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1433 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1434 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1435 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1436 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1437 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1438 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1439 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1440 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1441 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1442 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1443 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1444 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1445 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1446 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1447 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1448
1449cleanup:
1450 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1451 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1452 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1453
1454 return( ret );
1455}
1456
1457/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001458 * Multiplication with Montgomery ladder in x/z coordinates,
1459 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001460 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001461static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1462 const mpi *m, const ecp_point *P,
1463 int (*f_rng)(void *, unsigned char *, size_t),
1464 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001465{
1466 int ret;
1467 size_t i;
1468 ecp_point RP;
1469 mpi PX;
1470
1471 ecp_point_init( &RP ); mpi_init( &PX );
1472
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001473 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001474 mpi_copy( &PX, &P->X );
1475 MPI_CHK( ecp_copy( &RP, P ) );
1476 MPI_CHK( ecp_set_zero( R ) );
1477
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001478 /* Randomize coordinates of the starting point */
1479 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001480
1481 i = mpi_msb( m ) + 1;
1482 while( i-- > 0 )
1483 {
1484 // TODO: no branch, and constant memory-access pattern
1485 if( mpi_get_bit( m, i ) )
1486 MPI_CHK( ecp_double_add_mxz( grp, &RP, R, &RP, R, &PX ) );
1487 else
1488 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1489 }
1490
1491 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1492
1493cleanup:
1494 ecp_point_free( &RP ); mpi_free( &PX );
1495
1496 return( ret );
1497}
1498
1499/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001500 * Multiplication R = m * P
1501 */
1502int ecp_mul( ecp_group *grp, ecp_point *R,
1503 const mpi *m, const ecp_point *P,
1504 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1505{
1506 int ret;
1507
1508 /* Common sanity checks */
1509 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1510 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1511
1512 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1513 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1514 return( ret );
1515
1516 /* Actual multiplication aglorithm depending of curve type */
1517 if( ecp_is_montgomery( grp ) )
1518 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
1519 else
1520 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
1521}
1522
1523/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001524 * Check that a point is valid as a public key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001525 */
1526int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1527{
1528 int ret;
1529 mpi YY, RHS;
1530
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001531 /* Must use affine coordinates */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001532 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001533 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001534
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001535 if( ecp_is_montgomery( grp ) )
1536 {
1537 /* Just check X is the correct number of bytes */
1538 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1539 return( POLARSSL_ERR_ECP_INVALID_KEY );
1540
1541 return( 0 );
1542 }
1543
1544 /*
1545 * Ok, so we have a short Weierstrass curve as in SEC1 3.2.3.1
1546 */
1547
1548 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001549 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1550 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1551 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1552 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001553 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001554
1555 mpi_init( &YY ); mpi_init( &RHS );
1556
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001557 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1558 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1559 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1560 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
1561 return( POLARSSL_ERR_ECP_INVALID_KEY );
1562
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001563 /*
1564 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001565 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001566 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001567 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1568 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001569 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001570 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1571 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001572
1573 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001574 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001575
1576cleanup:
1577
1578 mpi_free( &YY ); mpi_free( &RHS );
1579
1580 return( ret );
1581}
1582
1583/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001584 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001585 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001586int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001587{
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001588 if( ecp_is_montgomery( grp ) )
1589 {
1590 /* see the Curve25519 paper */
1591 if( mpi_get_bit( d, 0 ) != 0 ||
1592 mpi_get_bit( d, 1 ) != 0 ||
1593 mpi_get_bit( d, 2 ) != 0 ||
1594 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1595 return( POLARSSL_ERR_ECP_INVALID_KEY );
1596 }
1597 else
1598 {
1599 /* see SEC1 3.2 */
1600 if( mpi_cmp_int( d, 1 ) < 0 ||
1601 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1602 return( POLARSSL_ERR_ECP_INVALID_KEY );
1603 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001604
1605 return( 0 );
1606}
1607
1608/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001609 * Generate a keypair (SEC1 3.2.1)
1610 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001611int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001612 int (*f_rng)(void *, unsigned char *, size_t),
1613 void *p_rng )
1614{
1615 int count = 0;
1616 size_t n_size = (grp->nbits + 7) / 8;
1617
1618 /*
1619 * Generate d such that 1 <= n < N
1620 */
1621 do
1622 {
1623 mpi_fill_random( d, n_size, f_rng, p_rng );
1624
1625 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1626 mpi_shift_r( d, 1 );
1627
1628 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001629 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001630 }
1631 while( mpi_cmp_int( d, 1 ) < 0 );
1632
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001633 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001634}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001635
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001636/*
1637 * Generate a keypair, prettier wrapper
1638 */
1639int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1640 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1641{
1642 int ret;
1643
1644 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1645 return( ret );
1646
1647 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1648}
1649
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001650#if defined(POLARSSL_SELF_TEST)
1651
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001652/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001653 * Checkup routine
1654 */
1655int ecp_self_test( int verbose )
1656{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001657 int ret;
1658 size_t i;
1659 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001660 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001661 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001662 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001663 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001664 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001665 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001666 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001667 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001668 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001669 "400000000000000000000000000000000000000000000000", /* one and zeros */
1670 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1671 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001672 };
1673
1674 ecp_group_init( &grp );
1675 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001676 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001677 mpi_init( &m );
1678
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001679 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001680#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001681 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001682#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001683 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1684#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001685
1686 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001687 printf( " ECP test #1 (constant op_count, base point G): " );
1688
1689 /* Do a dummy multiplication first to trigger precomputation */
1690 MPI_CHK( mpi_lset( &m, 2 ) );
1691 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001692
1693 add_count = 0;
1694 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001695 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001696 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001697 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001698
1699 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1700 {
1701 add_c_prev = add_count;
1702 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001703 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001704 add_count = 0;
1705 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001706 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001707
1708 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001709 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001710
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001711 if( add_count != add_c_prev ||
1712 dbl_count != dbl_c_prev ||
1713 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001714 {
1715 if( verbose != 0 )
1716 printf( "failed (%zu)\n", i );
1717
1718 ret = 1;
1719 goto cleanup;
1720 }
1721 }
1722
1723 if( verbose != 0 )
1724 printf( "passed\n" );
1725
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001726 if( verbose != 0 )
1727 printf( " ECP test #2 (constant op_count, other point): " );
1728 /* We computed P = 2G last time, use it */
1729
1730 add_count = 0;
1731 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001732 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001733 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1734 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1735
1736 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1737 {
1738 add_c_prev = add_count;
1739 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001740 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001741 add_count = 0;
1742 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001743 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001744
1745 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1746 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1747
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001748 if( add_count != add_c_prev ||
1749 dbl_count != dbl_c_prev ||
1750 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001751 {
1752 if( verbose != 0 )
1753 printf( "failed (%zu)\n", i );
1754
1755 ret = 1;
1756 goto cleanup;
1757 }
1758 }
1759
1760 if( verbose != 0 )
1761 printf( "passed\n" );
1762
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001763cleanup:
1764
1765 if( ret < 0 && verbose != 0 )
1766 printf( "Unexpected error, return code = %08X\n", ret );
1767
1768 ecp_group_free( &grp );
1769 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001770 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001771 mpi_free( &m );
1772
1773 if( verbose != 0 )
1774 printf( "\n" );
1775
1776 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001777}
1778
1779#endif
1780
1781#endif