blob: d661c2eaa3634a8b04e8dd00a92d3b2558e65dc8 [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é-Gonnardfe0af402013-12-04 18:14:55 +010034 * [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf
35 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020036 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
37 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
38 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
39 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010040 *
41 * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
42 * render ECC resistant against Side Channel Attacks. IACR Cryptology
43 * ePrint Archive, 2004, vol. 2004, p. 342.
44 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045 */
46
47#include "polarssl/config.h"
48
49#if defined(POLARSSL_ECP_C)
50
51#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052
53#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010060#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010062#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
63 !defined(EFI32)
64#define strcasecmp _stricmp
65#endif
66
Paul Bakker6a6087e2013-10-28 18:53:08 +010067#if defined(_MSC_VER) && !defined(inline)
68#define inline _inline
69#else
70#if defined(__ARMCC_VERSION) && !defined(inline)
71#define inline __inline
72#endif /* __ARMCC_VERSION */
73#endif /*_MSC_VER */
74
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010075#if defined(POLARSSL_SELF_TEST)
76/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010077 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020078 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010079 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010080static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010081#endif
82
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010083/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020084 * List of supported curves:
85 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020086 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020087 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020088 * - readable name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020089 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010090static const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020091{
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020092#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010093 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020094#endif
95#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010096 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020097#endif
98#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010099 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200100#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200101#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200102 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200103#endif
104#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200105 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200106#endif
107#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200108 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200109#endif
110#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200111 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200112#endif
113#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200114 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200115#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200116 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200117};
118
119/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200120 * List of supported curves and associated info
121 */
122const ecp_curve_info *ecp_curve_list( void )
123{
124 return ecp_supported_curves;
125}
126
127/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200128 * Get the curve info for the internal identifer
129 */
130const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
131{
132 const ecp_curve_info *curve_info;
133
134 for( curve_info = ecp_curve_list();
135 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
136 curve_info++ )
137 {
138 if( curve_info->grp_id == grp_id )
139 return( curve_info );
140 }
141
142 return( NULL );
143}
144
145/*
146 * Get the curve info from the TLS identifier
147 */
148const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
149{
150 const ecp_curve_info *curve_info;
151
152 for( curve_info = ecp_curve_list();
153 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
154 curve_info++ )
155 {
156 if( curve_info->tls_id == tls_id )
157 return( curve_info );
158 }
159
160 return( NULL );
161}
162
163/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100164 * Get the curve info from the name
165 */
166const ecp_curve_info *ecp_curve_info_from_name( const char *name )
167{
168 const ecp_curve_info *curve_info;
169
170 for( curve_info = ecp_curve_list();
171 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
172 curve_info++ )
173 {
174 if( strcasecmp( curve_info->name, name ) == 0 )
175 return( curve_info );
176 }
177
178 return( NULL );
179}
180
181/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100182 * Tell if a group structure is associated to a Montgomery curve
183 */
184static inline int ecp_is_montgomery( const ecp_group *grp )
185{
186 return( grp->G.X.p != NULL && grp->G.Y.p == NULL );
187}
188
189/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100190 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100191 */
192void ecp_point_init( ecp_point *pt )
193{
194 if( pt == NULL )
195 return;
196
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100197 mpi_init( &pt->X );
198 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100199 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100200}
201
202/*
203 * Initialize (the components of) a group
204 */
205void ecp_group_init( ecp_group *grp )
206{
207 if( grp == NULL )
208 return;
209
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200210 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100211}
212
213/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200214 * Initialize (the components of) a key pair
215 */
216void ecp_keypair_init( ecp_keypair *key )
217{
218 if ( key == NULL )
219 return;
220
221 ecp_group_init( &key->grp );
222 mpi_init( &key->d );
223 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200224}
225
226/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100227 * Unallocate (the components of) a point
228 */
229void ecp_point_free( ecp_point *pt )
230{
231 if( pt == NULL )
232 return;
233
234 mpi_free( &( pt->X ) );
235 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100236 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100237}
238
239/*
240 * Unallocate (the components of) a group
241 */
242void ecp_group_free( ecp_group *grp )
243{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200244 size_t i;
245
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100246 if( grp == NULL )
247 return;
248
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100249 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200250 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100251 mpi_free( &grp->B );
252 ecp_point_free( &grp->G );
253 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200254
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200255 if( grp->T != NULL )
256 {
257 for( i = 0; i < grp->T_size; i++ )
258 ecp_point_free( &grp->T[i] );
259 polarssl_free( grp->T );
260 }
261
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200262 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100263}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100264
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100265/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200266 * Unallocate (the components of) a key pair
267 */
268void ecp_keypair_free( ecp_keypair *key )
269{
270 if ( key == NULL )
271 return;
272
273 ecp_group_free( &key->grp );
274 mpi_free( &key->d );
275 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200276}
277
278/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200279 * Copy the contents of a point
280 */
281int ecp_copy( ecp_point *P, const ecp_point *Q )
282{
283 int ret;
284
285 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
286 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
287 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
288
289cleanup:
290 return( ret );
291}
292
293/*
294 * Copy the contents of a group object
295 */
296int ecp_group_copy( ecp_group *dst, const ecp_group *src )
297{
298 return ecp_use_known_dp( dst, src->id );
299}
300
301/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100302 * Set point to zero
303 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100304int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100305{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100306 int ret;
307
308 MPI_CHK( mpi_lset( &pt->X , 1 ) );
309 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
310 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
311
312cleanup:
313 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100314}
315
316/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100317 * Tell if a point is zero
318 */
319int ecp_is_zero( ecp_point *pt )
320{
321 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
322}
323
324/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100325 * Import a non-zero point from ASCII strings
326 */
327int ecp_point_read_string( ecp_point *P, int radix,
328 const char *x, const char *y )
329{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100330 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100331
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100332 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
333 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100334 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100335
336cleanup:
337 return( ret );
338}
339
340/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100341 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100342 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100343int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100344 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100345 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100346{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200347 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100348 size_t plen;
349
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100350 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
351 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100352 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100353
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100354 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100355 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100356 */
357 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
358 {
359 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100360 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100361
362 buf[0] = 0x00;
363 *olen = 1;
364
365 return( 0 );
366 }
367
368 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100369
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100370 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
371 {
372 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100373
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100374 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100375 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100376
377 buf[0] = 0x04;
378 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
379 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
380 }
381 else if( format == POLARSSL_ECP_PF_COMPRESSED )
382 {
383 *olen = plen + 1;
384
385 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100386 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100387
388 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
389 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
390 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100391
392cleanup:
393 return( ret );
394}
395
396/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100397 * Import a point from unsigned binary data (SEC1 2.3.4)
398 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100399int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
400 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100401 int ret;
402 size_t plen;
403
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100404 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100405 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100406
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100407 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100408
409 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100410 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100411
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100412 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
413 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
414 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100415
416cleanup:
417 return( ret );
418}
419
420/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100421 * Import a point from a TLS ECPoint record (RFC 4492)
422 * struct {
423 * opaque point <1..2^8-1>;
424 * } ECPoint;
425 */
426int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100427 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100428{
429 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100430 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100431
432 /*
433 * We must have at least two bytes (1 for length, at least of for data)
434 */
435 if( buf_len < 2 )
436 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
437
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100438 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100439 if( data_len < 1 || data_len > buf_len - 1 )
440 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
441
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100442 /*
443 * Save buffer start for read_binary and update buf
444 */
445 buf_start = *buf;
446 *buf += data_len;
447
448 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100449}
450
451/*
452 * Export a point as a TLS ECPoint record (RFC 4492)
453 * struct {
454 * opaque point <1..2^8-1>;
455 * } ECPoint;
456 */
457int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100458 int format, size_t *olen,
459 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100460{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100461 int ret;
462
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100463 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100464 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100465 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100466 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100467 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
468
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100469 if( ( ret = ecp_point_write_binary( grp, pt, format,
470 olen, buf + 1, blen - 1) ) != 0 )
471 return( ret );
472
473 /*
474 * write length to the first byte and update total length
475 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200476 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100477 ++*olen;
478
479 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100480}
481
482/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200483 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200484 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200485int ecp_group_read_string( ecp_group *grp, int radix,
486 const char *p, const char *b,
487 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100488{
489 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100490
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100491 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200492 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100493 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
494 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
495 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
496
497 grp->pbits = mpi_msb( &grp->P );
498 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100499
500cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200501 if( ret != 0 )
502 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200503
504 return( ret );
505}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200506
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100507/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100508 * Set a group from an ECParameters record (RFC 4492)
509 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100510int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100511{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200512 uint16_t tls_id;
513 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100514
515 /*
516 * We expect at least three bytes (see below)
517 */
518 if( len < 3 )
519 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
520
521 /*
522 * First byte is curve_type; only named_curve is handled
523 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100524 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100525 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
526
527 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100528 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100529 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200530 tls_id = *(*buf)++;
531 tls_id <<= 8;
532 tls_id |= *(*buf)++;
533
534 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
535 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
536
537 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100538}
539
540/*
541 * Write the ECParameters record corresponding to a group (RFC 4492)
542 */
543int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
544 unsigned char *buf, size_t blen )
545{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200546 const ecp_curve_info *curve_info;
547
548 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
549 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200550
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100551 /*
552 * We are going to write 3 bytes (see below)
553 */
554 *olen = 3;
555 if( blen < *olen )
556 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
557
558 /*
559 * First byte is curve_type, always named_curve
560 */
561 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
562
563 /*
564 * Next two bytes are the namedcurve value
565 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200566 buf[0] = curve_info->tls_id >> 8;
567 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100568
569 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100570}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100571
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200572/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200573 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
574 * See the documentation of struct ecp_group.
575 *
576 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200577 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200578static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200579{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200580 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200581
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200582 if( grp->modp == NULL )
583 return( mpi_mod_mpi( N, N, &grp->P ) );
584
585 /* N->s < 0 is a much faster test, which fails only if N is 0 */
586 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
587 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200588 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200589 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200590 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200591
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200592 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200593
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200594 /* N->s < 0 is a much faster test, which fails only if N is 0 */
595 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
596 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200597
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200598 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
599 /* we known P, N and the result are positive */
600 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200601
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200602cleanup:
603 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200604}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200605
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100606/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100607 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100608 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100609 * In order to guarantee that, we need to ensure that operands of
610 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100611 * bring the result back to this range.
612 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100613 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100614 */
615
616/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100617 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
618 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100619#if defined(POLARSSL_SELF_TEST)
620#define INC_MUL_COUNT mul_count++;
621#else
622#define INC_MUL_COUNT
623#endif
624
625#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
626 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100627
628/*
629 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200630 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100631 */
632#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200633 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100634 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
635
636/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200637 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
638 * We known P, N and the result are positive, so sub_abs is correct, and
639 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100640 */
641#define MOD_ADD( N ) \
642 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200643 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100644
645/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100646 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100647 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100648 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100649static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100650{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100651 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100652 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100653
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100654 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100655 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100656
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100657 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100658
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100659 /*
660 * X = X / Z^2 mod p
661 */
662 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
663 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
664 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100665
666 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100667 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100668 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100669 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
670 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100671
672 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100673 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100674 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100675 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100676
677cleanup:
678
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100679 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100680
681 return( ret );
682}
683
684/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100685 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100686 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100687 * (See for example Cohen's "A Course in Computational Algebraic Number
688 * Theory", Algorithm 10.3.4.)
689 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200690 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100691 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100692 *
693 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100694 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100695static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100696 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100697{
698 int ret;
699 size_t i;
700 mpi *c, u, Zi, ZZi;
701
702 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100703 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100704
Paul Bakker6e339b52013-07-03 13:37:05 +0200705 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200706 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100707
708 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
709 for( i = 0; i < t_len; i++ )
710 mpi_init( &c[i] );
711
712 /*
713 * c[i] = Z_0 * ... * Z_i
714 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100715 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100716 for( i = 1; i < t_len; i++ )
717 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100718 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100719 MOD_MUL( c[i] );
720 }
721
722 /*
723 * u = 1 / (Z_0 * ... * Z_n) mod P
724 */
725 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
726
727 for( i = t_len - 1; ; i-- )
728 {
729 /*
730 * Zi = 1 / Z_i mod p
731 * u = 1 / (Z_0 * ... * Z_i) mod P
732 */
733 if( i == 0 ) {
734 MPI_CHK( mpi_copy( &Zi, &u ) );
735 }
736 else
737 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100738 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
739 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100740 }
741
742 /*
743 * proceed as in normalize()
744 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100745 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
746 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
747 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
748 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
749 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100750
751 if( i == 0 )
752 break;
753 }
754
755cleanup:
756
757 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
758 for( i = 0; i < t_len; i++ )
759 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200760 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100761
762 return( ret );
763}
764
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100765/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100766 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
767 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
768 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100769static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100770 ecp_point *Q,
771 unsigned char inv )
772{
773 int ret;
774 unsigned char nonzero;
775 mpi mQY;
776
777 mpi_init( &mQY );
778
779 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
780 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
781 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
782 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
783
784cleanup:
785 mpi_free( &mQY );
786
787 return( ret );
788}
789
790/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200791 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200792 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200793 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200794 * with heavy variable renaming, some reordering and one minor modification
795 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
796 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100797 *
798 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200799 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200800static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
801 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200802{
803 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200804 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200805
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200806#if defined(POLARSSL_SELF_TEST)
807 dbl_count++;
808#endif
809
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200810 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
811 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200812
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200813 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
814 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
815 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
816 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
817 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
818 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
819 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
820 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
821 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
822 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
823 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
824 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
825 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
826 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
827 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
828 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
829 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
830 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
831 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
832 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
833 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
834 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
835 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
836 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200837
838 MPI_CHK( mpi_copy( &R->X, &X3 ) );
839 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
840 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
841
842cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200843 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
844 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200845
846 return( ret );
847}
848
849/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100850 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100851 *
852 * The coordinates of Q must be normalized (= affine),
853 * but those of P don't need to. R is not normalized.
854 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100855 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
856 * None of these cases can happen as intermediate step in ecp_mul():
857 * - at each step, P, Q and R are multiples of the base point, the factor
858 * being less than its order, so none of them is zero;
859 * - Q is an odd multiple of the base point, P an even multiple,
860 * due to the choice of precomputed points in the modified comb method.
861 * So branches for these cases do not leak secret information.
862 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100863 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100864 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100865static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100866 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100867{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100868 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100869 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100870
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100871#if defined(POLARSSL_SELF_TEST)
872 add_count++;
873#endif
874
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100875 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100876 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100877 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100878 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
879 return( ecp_copy( R, Q ) );
880
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100881 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
882 return( ecp_copy( R, P ) );
883
884 /*
885 * Make sure Q coordinates are normalized
886 */
887 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200888 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100889
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100890 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
891 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100892
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100893 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
894 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
895 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
896 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
897 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
898 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100899
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100900 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100901 if( mpi_cmp_int( &T1, 0 ) == 0 )
902 {
903 if( mpi_cmp_int( &T2, 0 ) == 0 )
904 {
905 ret = ecp_double_jac( grp, R, P );
906 goto cleanup;
907 }
908 else
909 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100910 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100911 goto cleanup;
912 }
913 }
914
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100915 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
916 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
917 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
918 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
919 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
920 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
921 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
922 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
923 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
924 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
925 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
926 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100927
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100928 MPI_CHK( mpi_copy( &R->X, &X ) );
929 MPI_CHK( mpi_copy( &R->Y, &Y ) );
930 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100931
932cleanup:
933
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100934 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
935 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100936
937 return( ret );
938}
939
940/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100941 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100942 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100943 */
944int ecp_add( const ecp_group *grp, ecp_point *R,
945 const ecp_point *P, const ecp_point *Q )
946{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100947 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100948
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100949 if( ecp_is_montgomery( grp ) )
950 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
951
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100952 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100953 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100954
955cleanup:
956 return( ret );
957}
958
959/*
960 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100961 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100962 */
963int ecp_sub( const ecp_group *grp, ecp_point *R,
964 const ecp_point *P, const ecp_point *Q )
965{
966 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100967 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100968
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100969 ecp_point_init( &mQ );
970
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100971 if( ecp_is_montgomery( grp ) )
972 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
973
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100974 /* mQ = - Q */
975 ecp_copy( &mQ, Q );
976 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
977 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
978
979 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100980 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100981
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100982cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100983 ecp_point_free( &mQ );
984
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100985 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100986}
987
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100988/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200989 * Randomize jacobian coordinates:
990 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100991 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +0100992 *
993 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200994 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +0100995static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200996 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
997{
998 int ret;
999 mpi l, ll;
1000 size_t p_size = (grp->pbits + 7) / 8;
1001 int count = 0;
1002
1003 mpi_init( &l ); mpi_init( &ll );
1004
1005 /* Generate l such that 1 < l < p */
1006 do
1007 {
1008 mpi_fill_random( &l, p_size, f_rng, p_rng );
1009
1010 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1011 mpi_shift_r( &l, 1 );
1012
1013 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001014 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001015 }
1016 while( mpi_cmp_int( &l, 1 ) <= 0 );
1017
1018 /* Z = l * Z */
1019 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1020
1021 /* X = l^2 * X */
1022 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1023 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1024
1025 /* Y = l^3 * Y */
1026 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1027 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1028
1029cleanup:
1030 mpi_free( &l ); mpi_free( &ll );
1031
1032 return( ret );
1033}
1034
1035/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001036 * Check and define parameters used by the comb method (see below for details)
1037 */
1038#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1039#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1040#endif
1041
1042/* d = ceil( n / w ) */
1043#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1044
1045/* number of precomputed points */
1046#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1047
1048/*
1049 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001050 *
1051 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001052 * modified version that provides resistance to SPA by avoiding zero
1053 * digits in the representation as in [3]. We modify the method further by
1054 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001055 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001056 *
1057 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1058 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1059 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001060 *
1061 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001062 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001063 * - w is the size, ie number of teeth, of the comb, and must be between
1064 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001065 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1066 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001067 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001068static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001069 unsigned char w, const mpi *m )
1070{
1071 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001072 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001073
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001074 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001075
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001076 /* First get the classical comb values (except for x_d = 0) */
1077 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001078 for( j = 0; j < w; j++ )
1079 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1080
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001081 /* Now make sure x_1 .. x_d are odd */
1082 c = 0;
1083 for( i = 1; i <= d; i++ )
1084 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001085 /* Add carry and update it */
1086 cc = x[i] & c;
1087 x[i] = x[i] ^ c;
1088 c = cc;
1089
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001090 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001091 adjust = 1 - ( x[i] & 0x01 );
1092 c |= x[i] & ( x[i-1] * adjust );
1093 x[i] = x[i] ^ ( x[i-1] * adjust );
1094 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001095 }
1096}
1097
1098/*
1099 * Precompute points for the comb method
1100 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001101 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1102 * 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 +01001103 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001104 * T must be able to hold 2^{w - 1} elements
1105 *
1106 * 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 +01001107 */
1108static int ecp_precompute_comb( const ecp_group *grp,
1109 ecp_point T[], const ecp_point *P,
1110 unsigned char w, size_t d )
1111{
1112 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001113 unsigned char i, k;
1114 size_t j;
1115 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001116
1117 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001118 * Set T[0] = P and
1119 * 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 +01001120 */
1121 MPI_CHK( ecp_copy( &T[0], P ) );
1122
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001123 k = 0;
1124 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001125 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001126 cur = T + i;
1127 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001128 for( j = 0; j < d; j++ )
1129 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001130
1131 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001132 }
1133
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001134 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001135
1136 /*
1137 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001138 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001139 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001140 k = 0;
1141 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001142 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001143 j = i;
1144 while( j-- )
1145 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001146 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001147 TT[k++] = &T[i + j];
1148 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001149 }
1150
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001151 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001152
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001153 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001154 * Post-precessing: reclaim some memory by
1155 * - not storing Z (always 1)
1156 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001157 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001158 */
1159 for( i = 0; i < ( 1U << (w-1) ); i++ )
1160 {
1161 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001162 mpi_shrink( &T[i].X, grp->P.n );
1163 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001164 }
1165
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001166cleanup:
1167 return( ret );
1168}
1169
1170/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001171 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001172 */
1173static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001174 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001175 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001176{
1177 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001178 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001179
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001180 /* Ignore the "sign" bit and scale down */
1181 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001182
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001183 /* Read the whole table to thwart cache-based timing attacks */
1184 for( j = 0; j < t_len; j++ )
1185 {
1186 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1187 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1188 }
1189
1190 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001191 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1192
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001193 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001194 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001195
1196cleanup:
1197 return( ret );
1198}
1199
1200/*
1201 * Core multiplication algorithm for the (modified) comb method.
1202 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001203 *
1204 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001205 */
1206static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001207 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001208 const unsigned char x[], size_t d,
1209 int (*f_rng)(void *, unsigned char *, size_t),
1210 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001211{
1212 int ret;
1213 ecp_point Txi;
1214 size_t i;
1215
1216 ecp_point_init( &Txi );
1217
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001218 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001219 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001220 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001221 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001222 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001223
1224 while( i-- != 0 )
1225 {
1226 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001227 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001228 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001229 }
1230
1231cleanup:
1232 ecp_point_free( &Txi );
1233
1234 return( ret );
1235}
1236
1237/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001238 * Multiplication using the comb method,
1239 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001240 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001241static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1242 const mpi *m, const ecp_point *P,
1243 int (*f_rng)(void *, unsigned char *, size_t),
1244 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001245{
1246 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001247 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1248 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001249 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001250 ecp_point *T;
1251 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001252
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001253 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001254 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001255
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001256 /* we need N to be odd to trnaform m in an odd number, check now */
1257 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1258 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1259
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001260 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001261 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001262 * 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 +01001263 * (see costs of the various parts, with 1S = 1M)
1264 */
1265 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001266
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001267 /*
1268 * If P == G, pre-compute a bit more, since this may be re-used later.
1269 * Just adding one ups the cost of the first mul by at most 3%.
1270 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001271 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1272 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001273 if( p_eq_g )
1274 w++;
1275
1276 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001277 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001278 * (The last test is useful only for very small curves in the test suite.)
1279 */
1280 if( w > POLARSSL_ECP_WINDOW_SIZE )
1281 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001282 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001283 w = 2;
1284
1285 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001286 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001287 d = ( grp->nbits + w - 1 ) / w;
1288
1289 /*
1290 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001291 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001292 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001293 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001294
1295 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001296 {
1297 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1298 if( T == NULL )
1299 {
1300 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1301 goto cleanup;
1302 }
1303
1304 for( i = 0; i < pre_len; i++ )
1305 ecp_point_init( &T[i] );
1306
1307 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1308
1309 if( p_eq_g )
1310 {
1311 grp->T = T;
1312 grp->T_size = pre_len;
1313 }
1314 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001315
1316 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001317 * Make sure M is odd (M = m or M = N - m, since N is odd)
1318 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001319 */
1320 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001321 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001322 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1323 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001324
1325 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001326 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001327 */
1328 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001329 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 +01001330
1331 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001332 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001333 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001334 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1335 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001336
1337cleanup:
1338
1339 if( T != NULL && ! p_eq_g )
1340 {
1341 for( i = 0; i < pre_len; i++ )
1342 ecp_point_free( &T[i] );
1343 polarssl_free( T );
1344 }
1345
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001346 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001347 mpi_free( &mm );
1348
1349 if( ret != 0 )
1350 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001351
1352 return( ret );
1353}
1354
1355/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001356 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1357 * Cost: 1M + 1I
1358 */
1359static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1360{
1361 int ret;
1362
1363 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1364 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1365 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1366
1367cleanup:
1368 return( ret );
1369}
1370
1371/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001372 * Randomize projective x/z coordinates:
1373 * (X, Z) -> (l X, l Z) for random l
1374 * This is sort of the reverse operation of ecp_normalize_mxz().
1375 *
1376 * This countermeasure was first suggested in [2].
1377 * Cost: 2M
1378 */
1379static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1380 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1381{
1382 int ret;
1383 mpi l;
1384 size_t p_size = (grp->pbits + 7) / 8;
1385 int count = 0;
1386
1387 mpi_init( &l );
1388
1389 /* Generate l such that 1 < l < p */
1390 do
1391 {
1392 mpi_fill_random( &l, p_size, f_rng, p_rng );
1393
1394 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1395 mpi_shift_r( &l, 1 );
1396
1397 if( count++ > 10 )
1398 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1399 }
1400 while( mpi_cmp_int( &l, 1 ) <= 0 );
1401
1402 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1403 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1404
1405cleanup:
1406 mpi_free( &l );
1407
1408 return( ret );
1409}
1410
1411/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001412 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1413 * for Montgomery curves in x/z coordinates.
1414 *
1415 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1416 * with
1417 * d = X1
1418 * P = (X2, Z2)
1419 * Q = (X3, Z3)
1420 * R = (X4, Z4)
1421 * S = (X5, Z5)
1422 * and eliminating temporary variables tO, ..., t4.
1423 *
1424 * Cost: 5M + 4S
1425 */
1426static int ecp_double_add_mxz( const ecp_group *grp,
1427 ecp_point *R, ecp_point *S,
1428 const ecp_point *P, const ecp_point *Q,
1429 const mpi *d )
1430{
1431 int ret;
1432 mpi A, AA, B, BB, E, C, D, DA, CB;
1433
1434 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1435 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1436 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1437
1438 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1439 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1440 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1441 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1442 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1443 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1444 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1445 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1446 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1447 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1448 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1449 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1450 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1451 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1452 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1453 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1454 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1455 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1456
1457cleanup:
1458 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1459 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1460 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1461
1462 return( ret );
1463}
1464
1465/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001466 * Multiplication with Montgomery ladder in x/z coordinates,
1467 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001468 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001469static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1470 const mpi *m, const ecp_point *P,
1471 int (*f_rng)(void *, unsigned char *, size_t),
1472 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001473{
1474 int ret;
1475 size_t i;
1476 ecp_point RP;
1477 mpi PX;
1478
1479 ecp_point_init( &RP ); mpi_init( &PX );
1480
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001481 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001482 mpi_copy( &PX, &P->X );
1483 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001484
1485 /* Set R to zero in modified x/z coordinates */
1486 MPI_CHK( mpi_lset( &R->X, 1 ) );
1487 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1488 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001489
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001490 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001491 if( f_rng != NULL )
1492 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001493
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001494 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001495 while( i-- > 0 )
1496 {
1497 // TODO: no branch, and constant memory-access pattern
1498 if( mpi_get_bit( m, i ) )
1499 MPI_CHK( ecp_double_add_mxz( grp, &RP, R, &RP, R, &PX ) );
1500 else
1501 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1502 }
1503
1504 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1505
1506cleanup:
1507 ecp_point_free( &RP ); mpi_free( &PX );
1508
1509 return( ret );
1510}
1511
1512/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001513 * Multiplication R = m * P
1514 */
1515int ecp_mul( ecp_group *grp, ecp_point *R,
1516 const mpi *m, const ecp_point *P,
1517 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1518{
1519 int ret;
1520
1521 /* Common sanity checks */
1522 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1523 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1524
1525 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1526 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1527 return( ret );
1528
1529 /* Actual multiplication aglorithm depending of curve type */
1530 if( ecp_is_montgomery( grp ) )
1531 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
1532 else
1533 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
1534}
1535
1536/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001537 * Check that a point is valid as a public key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001538 */
1539int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1540{
1541 int ret;
1542 mpi YY, RHS;
1543
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001544 /* Must use affine coordinates */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001545 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001546 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001547
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001548 if( ecp_is_montgomery( grp ) )
1549 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001550 /* [M255 p. 5] Just check X is the correct number of bytes */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001551 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1552 return( POLARSSL_ERR_ECP_INVALID_KEY );
1553
1554 return( 0 );
1555 }
1556
1557 /*
1558 * Ok, so we have a short Weierstrass curve as in SEC1 3.2.3.1
1559 */
1560
1561 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001562 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1563 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1564 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1565 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001566 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001567
1568 mpi_init( &YY ); mpi_init( &RHS );
1569
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001570 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1571 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1572 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1573 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
1574 return( POLARSSL_ERR_ECP_INVALID_KEY );
1575
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001576 /*
1577 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001578 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001579 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001580 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1581 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001582 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001583 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1584 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001585
1586 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001587 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001588
1589cleanup:
1590
1591 mpi_free( &YY ); mpi_free( &RHS );
1592
1593 return( ret );
1594}
1595
1596/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001597 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001598 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001599int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001600{
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001601 if( ecp_is_montgomery( grp ) )
1602 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001603 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001604 if( mpi_get_bit( d, 0 ) != 0 ||
1605 mpi_get_bit( d, 1 ) != 0 ||
1606 mpi_get_bit( d, 2 ) != 0 ||
1607 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1608 return( POLARSSL_ERR_ECP_INVALID_KEY );
1609 }
1610 else
1611 {
1612 /* see SEC1 3.2 */
1613 if( mpi_cmp_int( d, 1 ) < 0 ||
1614 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1615 return( POLARSSL_ERR_ECP_INVALID_KEY );
1616 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001617
1618 return( 0 );
1619}
1620
1621/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001622 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001623 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001624int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001625 int (*f_rng)(void *, unsigned char *, size_t),
1626 void *p_rng )
1627{
1628 int count = 0;
1629 size_t n_size = (grp->nbits + 7) / 8;
1630
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001631 if( ecp_is_montgomery( grp ) )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001632 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001633 /* [M225] page 5 */
1634 size_t b;
1635
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001636 mpi_fill_random( d, n_size, f_rng, p_rng );
1637
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001638 /* Make sure the most significant bit is nbits */
1639 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1640 if( b > grp->nbits )
1641 mpi_shift_r( d, b - grp->nbits );
1642 else
1643 mpi_set_bit( d, grp->nbits, 1 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001644
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001645 /* Make sure the last three bits are unset */
1646 mpi_set_bit( d, 0, 0 );
1647 mpi_set_bit( d, 1, 0 );
1648 mpi_set_bit( d, 2, 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001649 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001650 else
1651 {
1652 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
1653 do
1654 {
1655 mpi_fill_random( d, n_size, f_rng, p_rng );
1656
1657 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1658 mpi_shift_r( d, 1 );
1659
1660 if( count++ > 10 )
1661 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1662 }
1663 while( mpi_cmp_int( d, 1 ) < 0 );
1664 }
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001665
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001666 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001667}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001668
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001669/*
1670 * Generate a keypair, prettier wrapper
1671 */
1672int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1673 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1674{
1675 int ret;
1676
1677 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1678 return( ret );
1679
1680 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1681}
1682
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001683#if defined(POLARSSL_SELF_TEST)
1684
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001685/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001686 * Checkup routine
1687 */
1688int ecp_self_test( int verbose )
1689{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001690 int ret;
1691 size_t i;
1692 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001693 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001694 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001695 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001696 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001697 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001698 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001699 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001700 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001701 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001702 "400000000000000000000000000000000000000000000000", /* one and zeros */
1703 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1704 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001705 };
1706
1707 ecp_group_init( &grp );
1708 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001709 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001710 mpi_init( &m );
1711
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001712 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001713#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001714 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001715#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001716 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1717#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001718
1719 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001720 printf( " ECP test #1 (constant op_count, base point G): " );
1721
1722 /* Do a dummy multiplication first to trigger precomputation */
1723 MPI_CHK( mpi_lset( &m, 2 ) );
1724 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001725
1726 add_count = 0;
1727 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001728 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001729 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001730 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001731
1732 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1733 {
1734 add_c_prev = add_count;
1735 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001736 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001737 add_count = 0;
1738 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001739 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001740
1741 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001742 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001743
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001744 if( add_count != add_c_prev ||
1745 dbl_count != dbl_c_prev ||
1746 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001747 {
1748 if( verbose != 0 )
1749 printf( "failed (%zu)\n", i );
1750
1751 ret = 1;
1752 goto cleanup;
1753 }
1754 }
1755
1756 if( verbose != 0 )
1757 printf( "passed\n" );
1758
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001759 if( verbose != 0 )
1760 printf( " ECP test #2 (constant op_count, other point): " );
1761 /* We computed P = 2G last time, use it */
1762
1763 add_count = 0;
1764 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001765 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001766 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1767 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1768
1769 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1770 {
1771 add_c_prev = add_count;
1772 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001773 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001774 add_count = 0;
1775 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001776 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001777
1778 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1779 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1780
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001781 if( add_count != add_c_prev ||
1782 dbl_count != dbl_c_prev ||
1783 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001784 {
1785 if( verbose != 0 )
1786 printf( "failed (%zu)\n", i );
1787
1788 ret = 1;
1789 goto cleanup;
1790 }
1791 }
1792
1793 if( verbose != 0 )
1794 printf( "passed\n" );
1795
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001796cleanup:
1797
1798 if( ret < 0 && verbose != 0 )
1799 printf( "Unexpected error, return code = %08X\n", ret );
1800
1801 ecp_group_free( &grp );
1802 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001803 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001804 mpi_free( &m );
1805
1806 if( verbose != 0 )
1807 printf( "\n" );
1808
1809 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001810}
1811
1812#endif
1813
1814#endif