blob: 21e28c84512a2ce43df0ea3af5d8ec1f563f564e [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é-Gonnardb505c272012-11-05 17:27:54 +0100180 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100181 */
182void ecp_point_init( ecp_point *pt )
183{
184 if( pt == NULL )
185 return;
186
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100187 mpi_init( &pt->X );
188 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100189 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100190}
191
192/*
193 * Initialize (the components of) a group
194 */
195void ecp_group_init( ecp_group *grp )
196{
197 if( grp == NULL )
198 return;
199
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200200 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100201}
202
203/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200204 * Initialize (the components of) a key pair
205 */
206void ecp_keypair_init( ecp_keypair *key )
207{
208 if ( key == NULL )
209 return;
210
211 ecp_group_init( &key->grp );
212 mpi_init( &key->d );
213 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200214}
215
216/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100217 * Unallocate (the components of) a point
218 */
219void ecp_point_free( ecp_point *pt )
220{
221 if( pt == NULL )
222 return;
223
224 mpi_free( &( pt->X ) );
225 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100226 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100227}
228
229/*
230 * Unallocate (the components of) a group
231 */
232void ecp_group_free( ecp_group *grp )
233{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200234 size_t i;
235
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100236 if( grp == NULL )
237 return;
238
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100239 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200240 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100241 mpi_free( &grp->B );
242 ecp_point_free( &grp->G );
243 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200244
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200245 if( grp->T != NULL )
246 {
247 for( i = 0; i < grp->T_size; i++ )
248 ecp_point_free( &grp->T[i] );
249 polarssl_free( grp->T );
250 }
251
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200252 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100253}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100254
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100255/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200256 * Unallocate (the components of) a key pair
257 */
258void ecp_keypair_free( ecp_keypair *key )
259{
260 if ( key == NULL )
261 return;
262
263 ecp_group_free( &key->grp );
264 mpi_free( &key->d );
265 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200266}
267
268/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200269 * Copy the contents of a point
270 */
271int ecp_copy( ecp_point *P, const ecp_point *Q )
272{
273 int ret;
274
275 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
276 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
277 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
278
279cleanup:
280 return( ret );
281}
282
283/*
284 * Copy the contents of a group object
285 */
286int ecp_group_copy( ecp_group *dst, const ecp_group *src )
287{
288 return ecp_use_known_dp( dst, src->id );
289}
290
291/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100292 * Set point to zero
293 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100294int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100295{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100296 int ret;
297
298 MPI_CHK( mpi_lset( &pt->X , 1 ) );
299 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
300 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
301
302cleanup:
303 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100304}
305
306/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100307 * Tell if a point is zero
308 */
309int ecp_is_zero( ecp_point *pt )
310{
311 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
312}
313
314/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100315 * Import a non-zero point from ASCII strings
316 */
317int ecp_point_read_string( ecp_point *P, int radix,
318 const char *x, const char *y )
319{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100320 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100321
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100322 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
323 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100324 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100325
326cleanup:
327 return( ret );
328}
329
330/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100331 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100332 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100333int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100334 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100335 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100336{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200337 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100338 size_t plen;
339
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100340 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
341 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100342 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100343
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100344 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100345 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100346 */
347 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
348 {
349 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100350 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100351
352 buf[0] = 0x00;
353 *olen = 1;
354
355 return( 0 );
356 }
357
358 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100359
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100360 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
361 {
362 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100363
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100364 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100365 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100366
367 buf[0] = 0x04;
368 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
369 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
370 }
371 else if( format == POLARSSL_ECP_PF_COMPRESSED )
372 {
373 *olen = plen + 1;
374
375 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100376 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100377
378 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
379 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
380 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100381
382cleanup:
383 return( ret );
384}
385
386/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100387 * Import a point from unsigned binary data (SEC1 2.3.4)
388 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100389int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
390 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100391 int ret;
392 size_t plen;
393
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100394 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100395 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100396
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100397 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100398
399 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100400 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100401
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100402 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
403 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
404 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100405
406cleanup:
407 return( ret );
408}
409
410/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100411 * Import a point from a TLS ECPoint record (RFC 4492)
412 * struct {
413 * opaque point <1..2^8-1>;
414 * } ECPoint;
415 */
416int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100417 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100418{
419 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100420 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100421
422 /*
423 * We must have at least two bytes (1 for length, at least of for data)
424 */
425 if( buf_len < 2 )
426 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
427
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100428 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100429 if( data_len < 1 || data_len > buf_len - 1 )
430 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
431
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100432 /*
433 * Save buffer start for read_binary and update buf
434 */
435 buf_start = *buf;
436 *buf += data_len;
437
438 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100439}
440
441/*
442 * Export a point as a TLS ECPoint record (RFC 4492)
443 * struct {
444 * opaque point <1..2^8-1>;
445 * } ECPoint;
446 */
447int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100448 int format, size_t *olen,
449 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100450{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100451 int ret;
452
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100453 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100454 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100455 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100456 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100457 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
458
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100459 if( ( ret = ecp_point_write_binary( grp, pt, format,
460 olen, buf + 1, blen - 1) ) != 0 )
461 return( ret );
462
463 /*
464 * write length to the first byte and update total length
465 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200466 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100467 ++*olen;
468
469 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100470}
471
472/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200473 * Import an ECP group from ASCII strings, general case (A used)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100474 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200475static int ecp_group_read_string_gen( ecp_group *grp, int radix,
476 const char *p, const char *a, const char *b,
477 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100478{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100479 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100480
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200481 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
482 MPI_CHK( mpi_read_string( &grp->A, radix, a ) );
483 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
484 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
485 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100486
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200487 grp->pbits = mpi_msb( &grp->P );
488 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100489
490cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200491 if( ret != 0 )
492 ecp_group_free( grp );
493
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100494 return( ret );
495}
496
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200497/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200498 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200499 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200500int ecp_group_read_string( ecp_group *grp, int radix,
501 const char *p, const char *b,
502 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100503{
504 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100505
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200506 MPI_CHK( ecp_group_read_string_gen( grp, radix, p, "00", b, gx, gy, n ) );
507 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100508
509cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200510 if( ret != 0 )
511 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200512
513 return( ret );
514}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200515
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100516/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100517 * Set a group from an ECParameters record (RFC 4492)
518 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100519int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100520{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200521 uint16_t tls_id;
522 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100523
524 /*
525 * We expect at least three bytes (see below)
526 */
527 if( len < 3 )
528 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
529
530 /*
531 * First byte is curve_type; only named_curve is handled
532 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100533 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100534 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
535
536 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100537 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100538 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200539 tls_id = *(*buf)++;
540 tls_id <<= 8;
541 tls_id |= *(*buf)++;
542
543 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
544 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
545
546 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100547}
548
549/*
550 * Write the ECParameters record corresponding to a group (RFC 4492)
551 */
552int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
553 unsigned char *buf, size_t blen )
554{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200555 const ecp_curve_info *curve_info;
556
557 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
558 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200559
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100560 /*
561 * We are going to write 3 bytes (see below)
562 */
563 *olen = 3;
564 if( blen < *olen )
565 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
566
567 /*
568 * First byte is curve_type, always named_curve
569 */
570 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
571
572 /*
573 * Next two bytes are the namedcurve value
574 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200575 buf[0] = curve_info->tls_id >> 8;
576 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100577
578 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100579}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100580
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200581/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200582 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
583 * See the documentation of struct ecp_group.
584 *
585 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200586 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200587static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200588{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200589 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200590
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200591 if( grp->modp == NULL )
592 return( mpi_mod_mpi( N, N, &grp->P ) );
593
594 /* N->s < 0 is a much faster test, which fails only if N is 0 */
595 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
596 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200597 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200598 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200599 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200600
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200601 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200602
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200603 /* N->s < 0 is a much faster test, which fails only if N is 0 */
604 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
605 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200606
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200607 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
608 /* we known P, N and the result are positive */
609 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200610
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200611cleanup:
612 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200613}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200614
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100615/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100616 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100617 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100618 * In order to guarantee that, we need to ensure that operands of
619 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100620 * bring the result back to this range.
621 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100622 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100623 */
624
625/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100626 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
627 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100628#if defined(POLARSSL_SELF_TEST)
629#define INC_MUL_COUNT mul_count++;
630#else
631#define INC_MUL_COUNT
632#endif
633
634#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
635 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100636
637/*
638 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200639 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100640 */
641#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200642 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100643 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
644
645/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200646 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
647 * We known P, N and the result are positive, so sub_abs is correct, and
648 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100649 */
650#define MOD_ADD( N ) \
651 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200652 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100653
654/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100655 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100656 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100657 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100658static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100659{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100660 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100661 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100662
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100663 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100664 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100665
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100666 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100667
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100668 /*
669 * X = X / Z^2 mod p
670 */
671 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
672 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
673 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100674
675 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100676 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100677 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100678 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
679 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100680
681 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100682 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100683 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100684 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100685
686cleanup:
687
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100688 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100689
690 return( ret );
691}
692
693/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100694 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100695 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100696 * (See for example Cohen's "A Course in Computational Algebraic Number
697 * Theory", Algorithm 10.3.4.)
698 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200699 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100700 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100701 *
702 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100703 */
704static int ecp_normalize_many( const ecp_group *grp,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100705 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100706{
707 int ret;
708 size_t i;
709 mpi *c, u, Zi, ZZi;
710
711 if( t_len < 2 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100712 return( ecp_normalize( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100713
Paul Bakker6e339b52013-07-03 13:37:05 +0200714 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200715 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100716
717 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
718 for( i = 0; i < t_len; i++ )
719 mpi_init( &c[i] );
720
721 /*
722 * c[i] = Z_0 * ... * Z_i
723 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100724 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100725 for( i = 1; i < t_len; i++ )
726 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100727 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100728 MOD_MUL( c[i] );
729 }
730
731 /*
732 * u = 1 / (Z_0 * ... * Z_n) mod P
733 */
734 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
735
736 for( i = t_len - 1; ; i-- )
737 {
738 /*
739 * Zi = 1 / Z_i mod p
740 * u = 1 / (Z_0 * ... * Z_i) mod P
741 */
742 if( i == 0 ) {
743 MPI_CHK( mpi_copy( &Zi, &u ) );
744 }
745 else
746 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100747 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
748 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100749 }
750
751 /*
752 * proceed as in normalize()
753 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100754 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
755 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
756 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
757 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
758 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100759
760 if( i == 0 )
761 break;
762 }
763
764cleanup:
765
766 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
767 for( i = 0; i < t_len; i++ )
768 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200769 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100770
771 return( ret );
772}
773
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100774/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100775 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
776 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
777 */
778static int ecp_safe_invert( const ecp_group *grp,
779 ecp_point *Q,
780 unsigned char inv )
781{
782 int ret;
783 unsigned char nonzero;
784 mpi mQY;
785
786 mpi_init( &mQY );
787
788 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
789 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
790 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
791 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
792
793cleanup:
794 mpi_free( &mQY );
795
796 return( ret );
797}
798
799/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200800 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200801 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200802 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200803 * with heavy variable renaming, some reordering and one minor modification
804 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
805 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100806 *
807 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200808 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200809static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
810 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200811{
812 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200813 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200814
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200815#if defined(POLARSSL_SELF_TEST)
816 dbl_count++;
817#endif
818
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200819 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
820 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200821
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200822 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
823 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
824 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
825 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
826 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
827 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
828 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
829 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
830 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
831 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
832 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
833 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
834 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
835 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
836 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
837 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
838 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
839 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
840 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
841 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
842 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
843 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
844 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
845 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200846
847 MPI_CHK( mpi_copy( &R->X, &X3 ) );
848 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
849 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
850
851cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200852 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
853 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200854
855 return( ret );
856}
857
858/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100859 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100860 *
861 * The coordinates of Q must be normalized (= affine),
862 * but those of P don't need to. R is not normalized.
863 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100864 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
865 * None of these cases can happen as intermediate step in ecp_mul():
866 * - at each step, P, Q and R are multiples of the base point, the factor
867 * being less than its order, so none of them is zero;
868 * - Q is an odd multiple of the base point, P an even multiple,
869 * due to the choice of precomputed points in the modified comb method.
870 * So branches for these cases do not leak secret information.
871 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100872 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100873 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100874static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100875 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100876{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100877 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100878 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100879
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100880#if defined(POLARSSL_SELF_TEST)
881 add_count++;
882#endif
883
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100884 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100885 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100886 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100887 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
888 return( ecp_copy( R, Q ) );
889
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100890 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
891 return( ecp_copy( R, P ) );
892
893 /*
894 * Make sure Q coordinates are normalized
895 */
896 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200897 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100898
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100899 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
900 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100901
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100902 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
903 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
904 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
905 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
906 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
907 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100908
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100909 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100910 if( mpi_cmp_int( &T1, 0 ) == 0 )
911 {
912 if( mpi_cmp_int( &T2, 0 ) == 0 )
913 {
914 ret = ecp_double_jac( grp, R, P );
915 goto cleanup;
916 }
917 else
918 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100919 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100920 goto cleanup;
921 }
922 }
923
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100924 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
925 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
926 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
927 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
928 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
929 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
930 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
931 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
932 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
933 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
934 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
935 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100936
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100937 MPI_CHK( mpi_copy( &R->X, &X ) );
938 MPI_CHK( mpi_copy( &R->Y, &Y ) );
939 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100940
941cleanup:
942
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100943 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
944 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100945
946 return( ret );
947}
948
949/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100950 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100951 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100952 */
953int ecp_add( const ecp_group *grp, ecp_point *R,
954 const ecp_point *P, const ecp_point *Q )
955{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100956 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100957
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100958 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100959 MPI_CHK( ecp_normalize( grp, R ) );
960
961cleanup:
962 return( ret );
963}
964
965/*
966 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100967 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100968 */
969int ecp_sub( const ecp_group *grp, ecp_point *R,
970 const ecp_point *P, const ecp_point *Q )
971{
972 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100973 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100974
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100975 ecp_point_init( &mQ );
976
977 /* mQ = - Q */
978 ecp_copy( &mQ, Q );
979 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
980 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
981
982 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100983 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100984
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100985cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100986 ecp_point_free( &mQ );
987
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100988 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100989}
990
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100991/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200992 * Randomize jacobian coordinates:
993 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
994 * This is sort of the reverse operation of ecp_normalize().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +0100995 *
996 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200997 */
998static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
999 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1000{
1001 int ret;
1002 mpi l, ll;
1003 size_t p_size = (grp->pbits + 7) / 8;
1004 int count = 0;
1005
1006 mpi_init( &l ); mpi_init( &ll );
1007
1008 /* Generate l such that 1 < l < p */
1009 do
1010 {
1011 mpi_fill_random( &l, p_size, f_rng, p_rng );
1012
1013 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1014 mpi_shift_r( &l, 1 );
1015
1016 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001017 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001018 }
1019 while( mpi_cmp_int( &l, 1 ) <= 0 );
1020
1021 /* Z = l * Z */
1022 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1023
1024 /* X = l^2 * X */
1025 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1026 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1027
1028 /* Y = l^3 * Y */
1029 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1030 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1031
1032cleanup:
1033 mpi_free( &l ); mpi_free( &ll );
1034
1035 return( ret );
1036}
1037
1038/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001039 * Check and define parameters used by the comb method (see below for details)
1040 */
1041#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1042#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1043#endif
1044
1045/* d = ceil( n / w ) */
1046#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1047
1048/* number of precomputed points */
1049#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1050
1051/*
1052 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001053 *
1054 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001055 * modified version that provides resistance to SPA by avoiding zero
1056 * digits in the representation as in [3]. We modify the method further by
1057 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001058 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001059 *
1060 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1061 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1062 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001063 *
1064 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001065 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001066 * - w is the size, ie number of teeth, of the comb, and must be between
1067 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001068 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1069 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001070 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001071static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001072 unsigned char w, const mpi *m )
1073{
1074 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001075 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001076
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001077 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001078
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001079 /* First get the classical comb values (except for x_d = 0) */
1080 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001081 for( j = 0; j < w; j++ )
1082 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1083
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001084 /* Now make sure x_1 .. x_d are odd */
1085 c = 0;
1086 for( i = 1; i <= d; i++ )
1087 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001088 /* Add carry and update it */
1089 cc = x[i] & c;
1090 x[i] = x[i] ^ c;
1091 c = cc;
1092
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001093 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001094 adjust = 1 - ( x[i] & 0x01 );
1095 c |= x[i] & ( x[i-1] * adjust );
1096 x[i] = x[i] ^ ( x[i-1] * adjust );
1097 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001098 }
1099}
1100
1101/*
1102 * Precompute points for the comb method
1103 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001104 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1105 * 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 +01001106 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001107 * T must be able to hold 2^{w - 1} elements
1108 *
1109 * 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 +01001110 */
1111static int ecp_precompute_comb( const ecp_group *grp,
1112 ecp_point T[], const ecp_point *P,
1113 unsigned char w, size_t d )
1114{
1115 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001116 unsigned char i, k;
1117 size_t j;
1118 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001119
1120 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001121 * Set T[0] = P and
1122 * 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 +01001123 */
1124 MPI_CHK( ecp_copy( &T[0], P ) );
1125
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001126 k = 0;
1127 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001128 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001129 cur = T + i;
1130 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001131 for( j = 0; j < d; j++ )
1132 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001133
1134 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001135 }
1136
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001137 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001138
1139 /*
1140 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001141 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001142 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001143 k = 0;
1144 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001145 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001146 j = i;
1147 while( j-- )
1148 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001149 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001150 TT[k++] = &T[i + j];
1151 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001152 }
1153
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001154 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001155
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001156 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001157 * Post-precessing: reclaim some memory by
1158 * - not storing Z (always 1)
1159 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001160 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001161 */
1162 for( i = 0; i < ( 1U << (w-1) ); i++ )
1163 {
1164 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001165 mpi_shrink( &T[i].X, grp->P.n );
1166 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001167 }
1168
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001169cleanup:
1170 return( ret );
1171}
1172
1173/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001174 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001175 */
1176static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001177 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001178 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001179{
1180 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001181 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001182
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001183 /* Ignore the "sign" bit and scale down */
1184 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001185
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001186 /* Read the whole table to thwart cache-based timing attacks */
1187 for( j = 0; j < t_len; j++ )
1188 {
1189 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1190 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1191 }
1192
1193 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001194 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1195
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001196 /* Safely invert result if i is "negative" */
1197 MPI_CHK( ecp_safe_invert( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001198
1199cleanup:
1200 return( ret );
1201}
1202
1203/*
1204 * Core multiplication algorithm for the (modified) comb method.
1205 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001206 *
1207 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001208 */
1209static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001210 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001211 const unsigned char x[], size_t d,
1212 int (*f_rng)(void *, unsigned char *, size_t),
1213 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001214{
1215 int ret;
1216 ecp_point Txi;
1217 size_t i;
1218
1219 ecp_point_init( &Txi );
1220
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001221 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001222 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001223 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001224 if( f_rng != 0 )
1225 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001226
1227 while( i-- != 0 )
1228 {
1229 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001230 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001231 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001232 }
1233
1234cleanup:
1235 ecp_point_free( &Txi );
1236
1237 return( ret );
1238}
1239
1240/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001241 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001242 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001243int ecp_mul( ecp_group *grp, ecp_point *R,
1244 const mpi *m, const ecp_point *P,
1245 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001246{
1247 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001248 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1249 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001250 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001251 ecp_point *T;
1252 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001253
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001254 /*
1255 * Sanity checks (before we even initialize anything)
1256 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001257 if( mpi_cmp_int( &P->Z, 1 ) != 0 ||
1258 mpi_get_bit( &grp->N, 0 ) != 1 )
1259 {
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001260 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001261 }
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001262
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001263 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1264 return( ret );
1265
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001266 /* We'll need this later, but do it now to possibly avoid checking P */
1267 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001268 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001269
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001270 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1271 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001272
1273 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001274 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001275
1276 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001277 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001278 * 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 +01001279 * (see costs of the various parts, with 1S = 1M)
1280 */
1281 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001282
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001283 /*
1284 * If P == G, pre-compute a bit more, since this may be re-used later.
1285 * Just adding one ups the cost of the first mul by at most 3%.
1286 */
1287 if( p_eq_g )
1288 w++;
1289
1290 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001291 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001292 * (The last test is useful only for very small curves in the test suite.)
1293 */
1294 if( w > POLARSSL_ECP_WINDOW_SIZE )
1295 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001296 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001297 w = 2;
1298
1299 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001300 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001301 d = ( grp->nbits + w - 1 ) / w;
1302
1303 /*
1304 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001305 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001306 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001307 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001308
1309 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001310 {
1311 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1312 if( T == NULL )
1313 {
1314 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1315 goto cleanup;
1316 }
1317
1318 for( i = 0; i < pre_len; i++ )
1319 ecp_point_init( &T[i] );
1320
1321 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1322
1323 if( p_eq_g )
1324 {
1325 grp->T = T;
1326 grp->T_size = pre_len;
1327 }
1328 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001329
1330 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001331 * Make sure M is odd (M = m or M = N - m, since N is odd)
1332 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001333 */
1334 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001335 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001336 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1337 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001338
1339 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001340 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001341 */
1342 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001343 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 +01001344
1345 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001346 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001347 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001348 MPI_CHK( ecp_safe_invert( grp, R, ! m_is_odd ) );
1349 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001350
1351cleanup:
1352
1353 if( T != NULL && ! p_eq_g )
1354 {
1355 for( i = 0; i < pre_len; i++ )
1356 ecp_point_free( &T[i] );
1357 polarssl_free( T );
1358 }
1359
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001360 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001361 mpi_free( &mm );
1362
1363 if( ret != 0 )
1364 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001365
1366 return( ret );
1367}
1368
1369/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001370 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1371 */
1372int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1373{
1374 int ret;
1375 mpi YY, RHS;
1376
1377 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001378 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001379
1380 /*
1381 * pt coordinates must be normalized for our checks
1382 */
1383 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001384 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001385
1386 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1387 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1388 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1389 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001390 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001391
1392 mpi_init( &YY ); mpi_init( &RHS );
1393
1394 /*
1395 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001396 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001397 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001398 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1399 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001400 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001401 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1402 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001403
1404 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001405 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001406
1407cleanup:
1408
1409 mpi_free( &YY ); mpi_free( &RHS );
1410
1411 return( ret );
1412}
1413
1414/*
1415 * Check that an mpi is valid as a private key (SEC1 3.2)
1416 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001417int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001418{
1419 /* We want 1 <= d <= N-1 */
1420 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001421 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001422
1423 return( 0 );
1424}
1425
1426/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001427 * Generate a keypair (SEC1 3.2.1)
1428 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001429int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001430 int (*f_rng)(void *, unsigned char *, size_t),
1431 void *p_rng )
1432{
1433 int count = 0;
1434 size_t n_size = (grp->nbits + 7) / 8;
1435
1436 /*
1437 * Generate d such that 1 <= n < N
1438 */
1439 do
1440 {
1441 mpi_fill_random( d, n_size, f_rng, p_rng );
1442
1443 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1444 mpi_shift_r( d, 1 );
1445
1446 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001447 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001448 }
1449 while( mpi_cmp_int( d, 1 ) < 0 );
1450
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001451 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001452}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001453
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001454/*
1455 * Generate a keypair, prettier wrapper
1456 */
1457int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1458 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1459{
1460 int ret;
1461
1462 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1463 return( ret );
1464
1465 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1466}
1467
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001468#if defined(POLARSSL_SELF_TEST)
1469
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001470/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001471 * Checkup routine
1472 */
1473int ecp_self_test( int verbose )
1474{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001475 int ret;
1476 size_t i;
1477 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001478 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001479 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001480 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001481 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001482 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001483 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001484 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001485 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001486 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001487 "400000000000000000000000000000000000000000000000", /* one and zeros */
1488 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1489 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001490 };
1491
1492 ecp_group_init( &grp );
1493 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001494 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001495 mpi_init( &m );
1496
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001497 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001498#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001499 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001500#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001501 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1502#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001503
1504 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001505 printf( " ECP test #1 (constant op_count, base point G): " );
1506
1507 /* Do a dummy multiplication first to trigger precomputation */
1508 MPI_CHK( mpi_lset( &m, 2 ) );
1509 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001510
1511 add_count = 0;
1512 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001513 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001514 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001515 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001516
1517 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1518 {
1519 add_c_prev = add_count;
1520 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001521 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001522 add_count = 0;
1523 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001524 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001525
1526 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001527 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001528
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001529 if( add_count != add_c_prev ||
1530 dbl_count != dbl_c_prev ||
1531 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001532 {
1533 if( verbose != 0 )
1534 printf( "failed (%zu)\n", i );
1535
1536 ret = 1;
1537 goto cleanup;
1538 }
1539 }
1540
1541 if( verbose != 0 )
1542 printf( "passed\n" );
1543
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001544 if( verbose != 0 )
1545 printf( " ECP test #2 (constant op_count, other point): " );
1546 /* We computed P = 2G last time, use it */
1547
1548 add_count = 0;
1549 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001550 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001551 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1552 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1553
1554 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1555 {
1556 add_c_prev = add_count;
1557 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001558 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001559 add_count = 0;
1560 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001561 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001562
1563 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1564 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1565
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001566 if( add_count != add_c_prev ||
1567 dbl_count != dbl_c_prev ||
1568 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001569 {
1570 if( verbose != 0 )
1571 printf( "failed (%zu)\n", i );
1572
1573 ret = 1;
1574 goto cleanup;
1575 }
1576 }
1577
1578 if( verbose != 0 )
1579 printf( "passed\n" );
1580
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001581cleanup:
1582
1583 if( ret < 0 && verbose != 0 )
1584 printf( "Unexpected error, return code = %08X\n", ret );
1585
1586 ecp_group_free( &grp );
1587 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001588 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001589 mpi_free( &m );
1590
1591 if( verbose != 0 )
1592 printf( "\n" );
1593
1594 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001595}
1596
1597#endif
1598
1599#endif