blob: 544b5a6276b9c9c34a36015a5fb9d066aea34435 [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, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200474 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200475int ecp_group_read_string( ecp_group *grp, int radix,
476 const char *p, const char *b,
477 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100478{
479 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100480
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100481 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200482 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100483 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 ) );
486
487 grp->pbits = mpi_msb( &grp->P );
488 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100489
490cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200491 if( ret != 0 )
492 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200493
494 return( ret );
495}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200496
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100497/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100498 * Set a group from an ECParameters record (RFC 4492)
499 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100500int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100501{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200502 uint16_t tls_id;
503 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100504
505 /*
506 * We expect at least three bytes (see below)
507 */
508 if( len < 3 )
509 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
510
511 /*
512 * First byte is curve_type; only named_curve is handled
513 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100514 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100515 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
516
517 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100518 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100519 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200520 tls_id = *(*buf)++;
521 tls_id <<= 8;
522 tls_id |= *(*buf)++;
523
524 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
525 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
526
527 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100528}
529
530/*
531 * Write the ECParameters record corresponding to a group (RFC 4492)
532 */
533int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
534 unsigned char *buf, size_t blen )
535{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200536 const ecp_curve_info *curve_info;
537
538 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
539 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200540
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100541 /*
542 * We are going to write 3 bytes (see below)
543 */
544 *olen = 3;
545 if( blen < *olen )
546 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
547
548 /*
549 * First byte is curve_type, always named_curve
550 */
551 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
552
553 /*
554 * Next two bytes are the namedcurve value
555 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200556 buf[0] = curve_info->tls_id >> 8;
557 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100558
559 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100560}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100561
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200562/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200563 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
564 * See the documentation of struct ecp_group.
565 *
566 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200567 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200568static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200569{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200570 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200571
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200572 if( grp->modp == NULL )
573 return( mpi_mod_mpi( N, N, &grp->P ) );
574
575 /* N->s < 0 is a much faster test, which fails only if N is 0 */
576 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
577 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200578 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200579 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200580 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200581
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200582 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200583
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200584 /* N->s < 0 is a much faster test, which fails only if N is 0 */
585 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
586 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200587
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200588 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
589 /* we known P, N and the result are positive */
590 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200591
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200592cleanup:
593 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200594}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200595
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100596/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100597 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100598 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100599 * In order to guarantee that, we need to ensure that operands of
600 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100601 * bring the result back to this range.
602 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100603 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100604 */
605
606/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100607 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
608 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100609#if defined(POLARSSL_SELF_TEST)
610#define INC_MUL_COUNT mul_count++;
611#else
612#define INC_MUL_COUNT
613#endif
614
615#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
616 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100617
618/*
619 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200620 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100621 */
622#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200623 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100624 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
625
626/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200627 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
628 * We known P, N and the result are positive, so sub_abs is correct, and
629 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100630 */
631#define MOD_ADD( N ) \
632 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200633 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100634
635/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100636 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100637 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100638 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100639static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100640{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100641 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100642 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100643
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100644 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100645 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100646
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100647 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100648
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100649 /*
650 * X = X / Z^2 mod p
651 */
652 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
653 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
654 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100655
656 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100657 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100658 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100659 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
660 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100661
662 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100663 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100664 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100665 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100666
667cleanup:
668
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100669 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100670
671 return( ret );
672}
673
674/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100675 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100676 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100677 * (See for example Cohen's "A Course in Computational Algebraic Number
678 * Theory", Algorithm 10.3.4.)
679 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200680 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100681 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100682 *
683 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100684 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100685static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100686 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100687{
688 int ret;
689 size_t i;
690 mpi *c, u, Zi, ZZi;
691
692 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100693 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100694
Paul Bakker6e339b52013-07-03 13:37:05 +0200695 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200696 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100697
698 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
699 for( i = 0; i < t_len; i++ )
700 mpi_init( &c[i] );
701
702 /*
703 * c[i] = Z_0 * ... * Z_i
704 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100705 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100706 for( i = 1; i < t_len; i++ )
707 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100708 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100709 MOD_MUL( c[i] );
710 }
711
712 /*
713 * u = 1 / (Z_0 * ... * Z_n) mod P
714 */
715 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
716
717 for( i = t_len - 1; ; i-- )
718 {
719 /*
720 * Zi = 1 / Z_i mod p
721 * u = 1 / (Z_0 * ... * Z_i) mod P
722 */
723 if( i == 0 ) {
724 MPI_CHK( mpi_copy( &Zi, &u ) );
725 }
726 else
727 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100728 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
729 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100730 }
731
732 /*
733 * proceed as in normalize()
734 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100735 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
736 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
737 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
738 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
739 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100740
741 if( i == 0 )
742 break;
743 }
744
745cleanup:
746
747 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
748 for( i = 0; i < t_len; i++ )
749 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200750 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100751
752 return( ret );
753}
754
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100755/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100756 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
757 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
758 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100759static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100760 ecp_point *Q,
761 unsigned char inv )
762{
763 int ret;
764 unsigned char nonzero;
765 mpi mQY;
766
767 mpi_init( &mQY );
768
769 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
770 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
771 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
772 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
773
774cleanup:
775 mpi_free( &mQY );
776
777 return( ret );
778}
779
780/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200781 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200782 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200783 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200784 * with heavy variable renaming, some reordering and one minor modification
785 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
786 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100787 *
788 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200789 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200790static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
791 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200792{
793 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200794 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200795
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200796#if defined(POLARSSL_SELF_TEST)
797 dbl_count++;
798#endif
799
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200800 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
801 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200802
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200803 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
804 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
805 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
806 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
807 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
808 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
809 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
810 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
811 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
812 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
813 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
814 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
815 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
816 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
817 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
818 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
819 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
820 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
821 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
822 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
823 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
824 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
825 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
826 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200827
828 MPI_CHK( mpi_copy( &R->X, &X3 ) );
829 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
830 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
831
832cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200833 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
834 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200835
836 return( ret );
837}
838
839/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100840 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100841 *
842 * The coordinates of Q must be normalized (= affine),
843 * but those of P don't need to. R is not normalized.
844 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100845 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
846 * None of these cases can happen as intermediate step in ecp_mul():
847 * - at each step, P, Q and R are multiples of the base point, the factor
848 * being less than its order, so none of them is zero;
849 * - Q is an odd multiple of the base point, P an even multiple,
850 * due to the choice of precomputed points in the modified comb method.
851 * So branches for these cases do not leak secret information.
852 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100853 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100854 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100855static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100856 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100857{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100858 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100859 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100860
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100861#if defined(POLARSSL_SELF_TEST)
862 add_count++;
863#endif
864
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100865 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100866 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100867 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100868 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
869 return( ecp_copy( R, Q ) );
870
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100871 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
872 return( ecp_copy( R, P ) );
873
874 /*
875 * Make sure Q coordinates are normalized
876 */
877 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200878 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100879
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100880 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
881 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100882
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100883 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
884 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
885 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
886 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
887 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
888 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100889
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100890 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100891 if( mpi_cmp_int( &T1, 0 ) == 0 )
892 {
893 if( mpi_cmp_int( &T2, 0 ) == 0 )
894 {
895 ret = ecp_double_jac( grp, R, P );
896 goto cleanup;
897 }
898 else
899 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100900 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100901 goto cleanup;
902 }
903 }
904
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100905 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
906 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
907 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
908 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
909 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
910 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
911 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
912 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
913 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
914 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
915 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
916 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100917
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100918 MPI_CHK( mpi_copy( &R->X, &X ) );
919 MPI_CHK( mpi_copy( &R->Y, &Y ) );
920 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100921
922cleanup:
923
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100924 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
925 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100926
927 return( ret );
928}
929
930/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100931 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100932 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100933 */
934int ecp_add( const ecp_group *grp, ecp_point *R,
935 const ecp_point *P, const ecp_point *Q )
936{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100937 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100938
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100939 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100940 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100941
942cleanup:
943 return( ret );
944}
945
946/*
947 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100948 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100949 */
950int ecp_sub( const ecp_group *grp, ecp_point *R,
951 const ecp_point *P, const ecp_point *Q )
952{
953 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100954 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100955
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100956 ecp_point_init( &mQ );
957
958 /* mQ = - Q */
959 ecp_copy( &mQ, Q );
960 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
961 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
962
963 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100964 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100965
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100966cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100967 ecp_point_free( &mQ );
968
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100969 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100970}
971
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +0100972/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200973 * Randomize jacobian coordinates:
974 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100975 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +0100976 *
977 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200978 */
979static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
980 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
981{
982 int ret;
983 mpi l, ll;
984 size_t p_size = (grp->pbits + 7) / 8;
985 int count = 0;
986
987 mpi_init( &l ); mpi_init( &ll );
988
989 /* Generate l such that 1 < l < p */
990 do
991 {
992 mpi_fill_random( &l, p_size, f_rng, p_rng );
993
994 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
995 mpi_shift_r( &l, 1 );
996
997 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200998 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200999 }
1000 while( mpi_cmp_int( &l, 1 ) <= 0 );
1001
1002 /* Z = l * Z */
1003 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1004
1005 /* X = l^2 * X */
1006 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1007 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1008
1009 /* Y = l^3 * Y */
1010 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1011 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1012
1013cleanup:
1014 mpi_free( &l ); mpi_free( &ll );
1015
1016 return( ret );
1017}
1018
1019/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001020 * Check and define parameters used by the comb method (see below for details)
1021 */
1022#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1023#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1024#endif
1025
1026/* d = ceil( n / w ) */
1027#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1028
1029/* number of precomputed points */
1030#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1031
1032/*
1033 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001034 *
1035 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001036 * modified version that provides resistance to SPA by avoiding zero
1037 * digits in the representation as in [3]. We modify the method further by
1038 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001039 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001040 *
1041 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1042 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1043 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001044 *
1045 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001046 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001047 * - w is the size, ie number of teeth, of the comb, and must be between
1048 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001049 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1050 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001051 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001052static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001053 unsigned char w, const mpi *m )
1054{
1055 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001056 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001057
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001058 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001059
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001060 /* First get the classical comb values (except for x_d = 0) */
1061 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001062 for( j = 0; j < w; j++ )
1063 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1064
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001065 /* Now make sure x_1 .. x_d are odd */
1066 c = 0;
1067 for( i = 1; i <= d; i++ )
1068 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001069 /* Add carry and update it */
1070 cc = x[i] & c;
1071 x[i] = x[i] ^ c;
1072 c = cc;
1073
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001074 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001075 adjust = 1 - ( x[i] & 0x01 );
1076 c |= x[i] & ( x[i-1] * adjust );
1077 x[i] = x[i] ^ ( x[i-1] * adjust );
1078 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001079 }
1080}
1081
1082/*
1083 * Precompute points for the comb method
1084 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001085 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1086 * 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 +01001087 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001088 * T must be able to hold 2^{w - 1} elements
1089 *
1090 * 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 +01001091 */
1092static int ecp_precompute_comb( const ecp_group *grp,
1093 ecp_point T[], const ecp_point *P,
1094 unsigned char w, size_t d )
1095{
1096 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001097 unsigned char i, k;
1098 size_t j;
1099 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001100
1101 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001102 * Set T[0] = P and
1103 * 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 +01001104 */
1105 MPI_CHK( ecp_copy( &T[0], P ) );
1106
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001107 k = 0;
1108 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001109 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001110 cur = T + i;
1111 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001112 for( j = 0; j < d; j++ )
1113 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001114
1115 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001116 }
1117
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001118 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001119
1120 /*
1121 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001122 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001123 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001124 k = 0;
1125 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001126 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001127 j = i;
1128 while( j-- )
1129 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001130 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001131 TT[k++] = &T[i + j];
1132 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001133 }
1134
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001135 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001136
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001137 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001138 * Post-precessing: reclaim some memory by
1139 * - not storing Z (always 1)
1140 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001141 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001142 */
1143 for( i = 0; i < ( 1U << (w-1) ); i++ )
1144 {
1145 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001146 mpi_shrink( &T[i].X, grp->P.n );
1147 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001148 }
1149
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001150cleanup:
1151 return( ret );
1152}
1153
1154/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001155 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001156 */
1157static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001158 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001159 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001160{
1161 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001162 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001163
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001164 /* Ignore the "sign" bit and scale down */
1165 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001166
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001167 /* Read the whole table to thwart cache-based timing attacks */
1168 for( j = 0; j < t_len; j++ )
1169 {
1170 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1171 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1172 }
1173
1174 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001175 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1176
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001177 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001178 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001179
1180cleanup:
1181 return( ret );
1182}
1183
1184/*
1185 * Core multiplication algorithm for the (modified) comb method.
1186 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001187 *
1188 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001189 */
1190static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001191 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001192 const unsigned char x[], size_t d,
1193 int (*f_rng)(void *, unsigned char *, size_t),
1194 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001195{
1196 int ret;
1197 ecp_point Txi;
1198 size_t i;
1199
1200 ecp_point_init( &Txi );
1201
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001202 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001203 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001204 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001205 if( f_rng != 0 )
1206 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001207
1208 while( i-- != 0 )
1209 {
1210 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001211 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001212 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001213 }
1214
1215cleanup:
1216 ecp_point_free( &Txi );
1217
1218 return( ret );
1219}
1220
1221/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001222 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001223 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001224int ecp_mul( ecp_group *grp, ecp_point *R,
1225 const mpi *m, const ecp_point *P,
1226 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001227{
1228 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001229 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1230 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001231 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001232 ecp_point *T;
1233 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001234
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001235 /*
1236 * Sanity checks (before we even initialize anything)
1237 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001238 if( mpi_cmp_int( &P->Z, 1 ) != 0 ||
1239 mpi_get_bit( &grp->N, 0 ) != 1 )
1240 {
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001241 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001242 }
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001243
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001244 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1245 return( ret );
1246
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001247 /* We'll need this later, but do it now to possibly avoid checking P */
1248 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001249 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001250
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001251 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1252 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001253
1254 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001255 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001256
1257 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001258 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001259 * 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 +01001260 * (see costs of the various parts, with 1S = 1M)
1261 */
1262 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001263
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001264 /*
1265 * If P == G, pre-compute a bit more, since this may be re-used later.
1266 * Just adding one ups the cost of the first mul by at most 3%.
1267 */
1268 if( p_eq_g )
1269 w++;
1270
1271 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001272 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001273 * (The last test is useful only for very small curves in the test suite.)
1274 */
1275 if( w > POLARSSL_ECP_WINDOW_SIZE )
1276 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001277 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001278 w = 2;
1279
1280 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001281 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001282 d = ( grp->nbits + w - 1 ) / w;
1283
1284 /*
1285 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001286 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001287 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001288 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001289
1290 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001291 {
1292 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1293 if( T == NULL )
1294 {
1295 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1296 goto cleanup;
1297 }
1298
1299 for( i = 0; i < pre_len; i++ )
1300 ecp_point_init( &T[i] );
1301
1302 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1303
1304 if( p_eq_g )
1305 {
1306 grp->T = T;
1307 grp->T_size = pre_len;
1308 }
1309 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001310
1311 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001312 * Make sure M is odd (M = m or M = N - m, since N is odd)
1313 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001314 */
1315 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001316 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001317 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1318 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001319
1320 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001321 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001322 */
1323 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001324 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 +01001325
1326 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001327 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001328 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001329 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1330 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001331
1332cleanup:
1333
1334 if( T != NULL && ! p_eq_g )
1335 {
1336 for( i = 0; i < pre_len; i++ )
1337 ecp_point_free( &T[i] );
1338 polarssl_free( T );
1339 }
1340
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001341 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001342 mpi_free( &mm );
1343
1344 if( ret != 0 )
1345 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001346
1347 return( ret );
1348}
1349
1350/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001351 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1352 */
1353int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1354{
1355 int ret;
1356 mpi YY, RHS;
1357
1358 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001359 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001360
1361 /*
1362 * pt coordinates must be normalized for our checks
1363 */
1364 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001365 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001366
1367 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1368 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1369 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1370 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001371 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001372
1373 mpi_init( &YY ); mpi_init( &RHS );
1374
1375 /*
1376 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001377 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001378 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001379 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1380 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001381 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001382 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1383 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001384
1385 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001386 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001387
1388cleanup:
1389
1390 mpi_free( &YY ); mpi_free( &RHS );
1391
1392 return( ret );
1393}
1394
1395/*
1396 * Check that an mpi is valid as a private key (SEC1 3.2)
1397 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001398int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001399{
1400 /* We want 1 <= d <= N-1 */
1401 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001402 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001403
1404 return( 0 );
1405}
1406
1407/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001408 * Generate a keypair (SEC1 3.2.1)
1409 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001410int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001411 int (*f_rng)(void *, unsigned char *, size_t),
1412 void *p_rng )
1413{
1414 int count = 0;
1415 size_t n_size = (grp->nbits + 7) / 8;
1416
1417 /*
1418 * Generate d such that 1 <= n < N
1419 */
1420 do
1421 {
1422 mpi_fill_random( d, n_size, f_rng, p_rng );
1423
1424 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1425 mpi_shift_r( d, 1 );
1426
1427 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001428 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001429 }
1430 while( mpi_cmp_int( d, 1 ) < 0 );
1431
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001432 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001433}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001434
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001435/*
1436 * Generate a keypair, prettier wrapper
1437 */
1438int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1439 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1440{
1441 int ret;
1442
1443 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1444 return( ret );
1445
1446 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1447}
1448
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001449#if defined(POLARSSL_SELF_TEST)
1450
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001451/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001452 * Checkup routine
1453 */
1454int ecp_self_test( int verbose )
1455{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001456 int ret;
1457 size_t i;
1458 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001459 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001460 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001461 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001462 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001463 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001464 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001465 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001466 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001467 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001468 "400000000000000000000000000000000000000000000000", /* one and zeros */
1469 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1470 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001471 };
1472
1473 ecp_group_init( &grp );
1474 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001475 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001476 mpi_init( &m );
1477
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001478 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001479#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001480 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001481#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001482 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1483#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001484
1485 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001486 printf( " ECP test #1 (constant op_count, base point G): " );
1487
1488 /* Do a dummy multiplication first to trigger precomputation */
1489 MPI_CHK( mpi_lset( &m, 2 ) );
1490 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001491
1492 add_count = 0;
1493 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001494 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001495 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001496 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001497
1498 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1499 {
1500 add_c_prev = add_count;
1501 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001502 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001503 add_count = 0;
1504 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001505 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001506
1507 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001508 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001509
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001510 if( add_count != add_c_prev ||
1511 dbl_count != dbl_c_prev ||
1512 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001513 {
1514 if( verbose != 0 )
1515 printf( "failed (%zu)\n", i );
1516
1517 ret = 1;
1518 goto cleanup;
1519 }
1520 }
1521
1522 if( verbose != 0 )
1523 printf( "passed\n" );
1524
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001525 if( verbose != 0 )
1526 printf( " ECP test #2 (constant op_count, other point): " );
1527 /* We computed P = 2G last time, use it */
1528
1529 add_count = 0;
1530 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001531 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001532 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1533 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1534
1535 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1536 {
1537 add_c_prev = add_count;
1538 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001539 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001540 add_count = 0;
1541 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001542 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001543
1544 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1545 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1546
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001547 if( add_count != add_c_prev ||
1548 dbl_count != dbl_c_prev ||
1549 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001550 {
1551 if( verbose != 0 )
1552 printf( "failed (%zu)\n", i );
1553
1554 ret = 1;
1555 goto cleanup;
1556 }
1557 }
1558
1559 if( verbose != 0 )
1560 printf( "passed\n" );
1561
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001562cleanup:
1563
1564 if( ret < 0 && verbose != 0 )
1565 printf( "Unexpected error, return code = %08X\n", ret );
1566
1567 ecp_group_free( &grp );
1568 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001569 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001570 mpi_free( &m );
1571
1572 if( verbose != 0 )
1573 printf( "\n" );
1574
1575 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001576}
1577
1578#endif
1579
1580#endif