blob: 969e11dc3734be883a875b114c8b286057013e10 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
Paul Bakkercf4365f2013-01-16 17:00:43 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-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é-Gonnarddada4da2012-11-10 14:23:17 +0100517 * Domain parameters for secp192r1
518 */
519#define SECP192R1_P \
520 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
521#define SECP192R1_B \
522 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
523#define SECP192R1_GX \
524 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
525#define SECP192R1_GY \
526 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
527#define SECP192R1_N \
528 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
529
530/*
531 * Domain parameters for secp224r1
532 */
533#define SECP224R1_P \
534 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
535#define SECP224R1_B \
536 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
537#define SECP224R1_GX \
538 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
539#define SECP224R1_GY \
540 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
541#define SECP224R1_N \
542 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
543
544/*
545 * Domain parameters for secp256r1
546 */
547#define SECP256R1_P \
548 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
549#define SECP256R1_B \
550 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
551#define SECP256R1_GX \
552 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
553#define SECP256R1_GY \
554 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
555#define SECP256R1_N \
556 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
557
558/*
559 * Domain parameters for secp384r1
560 */
561#define SECP384R1_P \
562 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
563 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
564#define SECP384R1_B \
565 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
566 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
567#define SECP384R1_GX \
568 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
569 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
570#define SECP384R1_GY \
571 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
572 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
573#define SECP384R1_N \
574 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
575 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
576
577/*
578 * Domain parameters for secp521r1
579 */
580#define SECP521R1_P \
581 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
582 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
583 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
584#define SECP521R1_B \
585 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
586 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
587 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
588#define SECP521R1_GX \
589 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
590 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
591 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
592#define SECP521R1_GY \
593 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
594 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
595 "3FAD0761353C7086A272C24088BE94769FD16650"
596#define SECP521R1_N \
597 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
598 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
599 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
600
601/*
Manuel Pégourié-Gonnardcec4a532013-10-07 19:52:27 +0200602 * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
603 */
604#define BP256R1_P \
605 "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377"
606#define BP256R1_A \
607 "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9"
608#define BP256R1_B \
609 "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6"
610#define BP256R1_GX \
611 "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262"
612#define BP256R1_GY \
613 "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"
614#define BP256R1_N \
615 "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7"
616
617/*
618 * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
619 */
620#define BP384R1_P \
621 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB711" \
622 "23ACD3A729901D1A71874700133107EC53"
623#define BP384R1_A \
624 "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F9" \
625 "0F8AA5814A503AD4EB04A8C7DD22CE2826"
626#define BP384R1_B \
627 "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62" \
628 "D57CB4390295DBC9943AB78696FA504C11"
629#define BP384R1_GX \
630 "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10" \
631 "E8E826E03436D646AAEF87B2E247D4AF1E"
632#define BP384R1_GY \
633 "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129" \
634 "280E4646217791811142820341263C5315"
635#define BP384R1_N \
636 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425" \
637 "A7CF3AB6AF6B7FC3103B883202E9046565"
638
639/*
640 * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
641 */
642#define BP512R1_P \
643 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
644 "717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"
645#define BP512R1_A \
646 "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863" \
647 "BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA"
648#define BP512R1_B \
649 "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117" \
650 "A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723"
651#define BP512R1_GX \
652 "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D009" \
653 "8EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822"
654#define BP512R1_GY \
655 "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F81" \
656 "11B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"
657#define BP512R1_N \
658 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
659 "70553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"
660
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200661#if defined(POLARSSL_ECP_NIST_OPTIM)
662/* Forward declarations */
663static int ecp_mod_p192( mpi * );
664static int ecp_mod_p224( mpi * );
665static int ecp_mod_p256( mpi * );
666static int ecp_mod_p384( mpi * );
667static int ecp_mod_p521( mpi * );
668#endif
669
Manuel Pégourié-Gonnardcec4a532013-10-07 19:52:27 +0200670/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100671 * Set a group using well-known domain parameters
672 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100673int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100674{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100675 grp->id = id;
676
677 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100678 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200679#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100680 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200681#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100682 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200683#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100684 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100685 SECP192R1_P, SECP192R1_B,
686 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200687#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100688
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200689#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100690 case POLARSSL_ECP_DP_SECP224R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200691#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200692 grp->modp = ecp_mod_p224;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200693#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100694 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100695 SECP224R1_P, SECP224R1_B,
696 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200697#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100698
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200699#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100700 case POLARSSL_ECP_DP_SECP256R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200701#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnardec655c92013-10-23 14:50:39 +0200702 grp->modp = ecp_mod_p256;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200703#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100704 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100705 SECP256R1_P, SECP256R1_B,
706 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200707#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100708
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200709#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100710 case POLARSSL_ECP_DP_SECP384R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200711#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard0f9149c2013-10-23 15:06:37 +0200712 grp->modp = ecp_mod_p384;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200713#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100714 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100715 SECP384R1_P, SECP384R1_B,
716 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200717#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100718
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200719#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100720 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200721#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100722 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200723#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100724 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100725 SECP521R1_P, SECP521R1_B,
726 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200727#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100728
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200729#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
730 case POLARSSL_ECP_DP_BP256R1:
731 return( ecp_group_read_string_gen( grp, 16,
732 BP256R1_P, BP256R1_A, BP256R1_B,
733 BP256R1_GX, BP256R1_GY, BP256R1_N ) );
734#endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
735
736#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
737 case POLARSSL_ECP_DP_BP384R1:
738 return( ecp_group_read_string_gen( grp, 16,
739 BP384R1_P, BP384R1_A, BP384R1_B,
740 BP384R1_GX, BP384R1_GY, BP384R1_N ) );
741#endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
742
743#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
744 case POLARSSL_ECP_DP_BP512R1:
745 return( ecp_group_read_string_gen( grp, 16,
746 BP512R1_P, BP512R1_A, BP512R1_B,
747 BP512R1_GX, BP512R1_GY, BP512R1_N ) );
748#endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
749
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200750 default:
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200751 ecp_group_free( grp );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200752 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
753 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100754}
755
756/*
757 * Set a group from an ECParameters record (RFC 4492)
758 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100759int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100760{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200761 uint16_t tls_id;
762 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100763
764 /*
765 * We expect at least three bytes (see below)
766 */
767 if( len < 3 )
768 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
769
770 /*
771 * First byte is curve_type; only named_curve is handled
772 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100773 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100774 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
775
776 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100777 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100778 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200779 tls_id = *(*buf)++;
780 tls_id <<= 8;
781 tls_id |= *(*buf)++;
782
783 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
784 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
785
786 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100787}
788
789/*
790 * Write the ECParameters record corresponding to a group (RFC 4492)
791 */
792int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
793 unsigned char *buf, size_t blen )
794{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200795 const ecp_curve_info *curve_info;
796
797 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
798 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200799
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100800 /*
801 * We are going to write 3 bytes (see below)
802 */
803 *olen = 3;
804 if( blen < *olen )
805 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
806
807 /*
808 * First byte is curve_type, always named_curve
809 */
810 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
811
812 /*
813 * Next two bytes are the namedcurve value
814 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200815 buf[0] = curve_info->tls_id >> 8;
816 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100817
818 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100819}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100820
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200821/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200822 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
823 * See the documentation of struct ecp_group.
824 *
825 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200826 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200827static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200828{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200829 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200830
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200831 if( grp->modp == NULL )
832 return( mpi_mod_mpi( N, N, &grp->P ) );
833
834 /* N->s < 0 is a much faster test, which fails only if N is 0 */
835 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
836 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200837 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200838 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200839 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200840
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200841 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200842
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200843 /* N->s < 0 is a much faster test, which fails only if N is 0 */
844 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
845 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200846
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200847 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
848 /* we known P, N and the result are positive */
849 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200850
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200851cleanup:
852 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200853}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200854
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100855/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100856 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100857 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100858 * In order to guarantee that, we need to ensure that operands of
859 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100860 * bring the result back to this range.
861 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100862 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100863 */
864
865/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100866 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
867 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100868#if defined(POLARSSL_SELF_TEST)
869#define INC_MUL_COUNT mul_count++;
870#else
871#define INC_MUL_COUNT
872#endif
873
874#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
875 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100876
877/*
878 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200879 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100880 */
881#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200882 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100883 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
884
885/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200886 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
887 * We known P, N and the result are positive, so sub_abs is correct, and
888 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100889 */
890#define MOD_ADD( N ) \
891 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200892 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100893
894/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100895 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100896 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100897 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100898static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100899{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100900 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100901 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100902
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100903 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100904 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100905
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100906 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100907
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100908 /*
909 * X = X / Z^2 mod p
910 */
911 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
912 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
913 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100914
915 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100916 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100917 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100918 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
919 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100920
921 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100922 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100923 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100924 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100925
926cleanup:
927
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100928 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100929
930 return( ret );
931}
932
933/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100934 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100935 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100936 * (See for example Cohen's "A Course in Computational Algebraic Number
937 * Theory", Algorithm 10.3.4.)
938 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200939 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100940 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100941 *
942 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100943 */
944static int ecp_normalize_many( const ecp_group *grp,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100945 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100946{
947 int ret;
948 size_t i;
949 mpi *c, u, Zi, ZZi;
950
951 if( t_len < 2 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100952 return( ecp_normalize( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100953
Paul Bakker6e339b52013-07-03 13:37:05 +0200954 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200955 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100956
957 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
958 for( i = 0; i < t_len; i++ )
959 mpi_init( &c[i] );
960
961 /*
962 * c[i] = Z_0 * ... * Z_i
963 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100964 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100965 for( i = 1; i < t_len; i++ )
966 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100967 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100968 MOD_MUL( c[i] );
969 }
970
971 /*
972 * u = 1 / (Z_0 * ... * Z_n) mod P
973 */
974 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
975
976 for( i = t_len - 1; ; i-- )
977 {
978 /*
979 * Zi = 1 / Z_i mod p
980 * u = 1 / (Z_0 * ... * Z_i) mod P
981 */
982 if( i == 0 ) {
983 MPI_CHK( mpi_copy( &Zi, &u ) );
984 }
985 else
986 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100987 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
988 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100989 }
990
991 /*
992 * proceed as in normalize()
993 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100994 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
995 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
996 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
997 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
998 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100999
1000 if( i == 0 )
1001 break;
1002 }
1003
1004cleanup:
1005
1006 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
1007 for( i = 0; i < t_len; i++ )
1008 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +02001009 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001010
1011 return( ret );
1012}
1013
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001014/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001015 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1016 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1017 */
1018static int ecp_safe_invert( const ecp_group *grp,
1019 ecp_point *Q,
1020 unsigned char inv )
1021{
1022 int ret;
1023 unsigned char nonzero;
1024 mpi mQY;
1025
1026 mpi_init( &mQY );
1027
1028 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
1029 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1030 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
1031 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
1032
1033cleanup:
1034 mpi_free( &mQY );
1035
1036 return( ret );
1037}
1038
1039/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001040 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001041 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001042 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001043 * with heavy variable renaming, some reordering and one minor modification
1044 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
1045 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001046 *
1047 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001048 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001049static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
1050 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001051{
1052 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001053 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001054
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001055#if defined(POLARSSL_SELF_TEST)
1056 dbl_count++;
1057#endif
1058
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001059 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
1060 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001061
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001062 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
1063 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
1064 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
1065 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
1066 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
1067 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
1068 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
1069 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
1070 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
1071 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
1072 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
1073 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
1074 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
1075 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
1076 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1077 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1078 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
1079 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
1080 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
1081 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
1082 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
1083 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
1084 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
1085 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001086
1087 MPI_CHK( mpi_copy( &R->X, &X3 ) );
1088 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
1089 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
1090
1091cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001092 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
1093 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001094
1095 return( ret );
1096}
1097
1098/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001099 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001100 *
1101 * The coordinates of Q must be normalized (= affine),
1102 * but those of P don't need to. R is not normalized.
1103 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001104 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
1105 * None of these cases can happen as intermediate step in ecp_mul():
1106 * - at each step, P, Q and R are multiples of the base point, the factor
1107 * being less than its order, so none of them is zero;
1108 * - Q is an odd multiple of the base point, P an even multiple,
1109 * due to the choice of precomputed points in the modified comb method.
1110 * So branches for these cases do not leak secret information.
1111 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001112 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001113 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001114static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001115 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001116{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001117 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001118 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001119
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001120#if defined(POLARSSL_SELF_TEST)
1121 add_count++;
1122#endif
1123
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001124 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001125 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001126 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001127 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1128 return( ecp_copy( R, Q ) );
1129
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001130 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1131 return( ecp_copy( R, P ) );
1132
1133 /*
1134 * Make sure Q coordinates are normalized
1135 */
1136 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001137 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001138
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001139 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1140 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001141
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001142 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1143 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1144 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1145 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1146 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1147 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001148
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001149 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001150 if( mpi_cmp_int( &T1, 0 ) == 0 )
1151 {
1152 if( mpi_cmp_int( &T2, 0 ) == 0 )
1153 {
1154 ret = ecp_double_jac( grp, R, P );
1155 goto cleanup;
1156 }
1157 else
1158 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001159 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001160 goto cleanup;
1161 }
1162 }
1163
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001164 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1165 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1166 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1167 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1168 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1169 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1170 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1171 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1172 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1173 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1174 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1175 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001176
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001177 MPI_CHK( mpi_copy( &R->X, &X ) );
1178 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1179 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001180
1181cleanup:
1182
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001183 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1184 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001185
1186 return( ret );
1187}
1188
1189/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001190 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001191 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001192 */
1193int ecp_add( const ecp_group *grp, ecp_point *R,
1194 const ecp_point *P, const ecp_point *Q )
1195{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001196 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001197
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001198 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001199 MPI_CHK( ecp_normalize( grp, R ) );
1200
1201cleanup:
1202 return( ret );
1203}
1204
1205/*
1206 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001207 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001208 */
1209int ecp_sub( const ecp_group *grp, ecp_point *R,
1210 const ecp_point *P, const ecp_point *Q )
1211{
1212 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001213 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001214
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001215 ecp_point_init( &mQ );
1216
1217 /* mQ = - Q */
1218 ecp_copy( &mQ, Q );
1219 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1220 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1221
1222 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001223 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001224
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001225cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001226 ecp_point_free( &mQ );
1227
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001228 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001229}
1230
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001231/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001232 * Randomize jacobian coordinates:
1233 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1234 * This is sort of the reverse operation of ecp_normalize().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001235 *
1236 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001237 */
1238static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1239 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1240{
1241 int ret;
1242 mpi l, ll;
1243 size_t p_size = (grp->pbits + 7) / 8;
1244 int count = 0;
1245
1246 mpi_init( &l ); mpi_init( &ll );
1247
1248 /* Generate l such that 1 < l < p */
1249 do
1250 {
1251 mpi_fill_random( &l, p_size, f_rng, p_rng );
1252
1253 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1254 mpi_shift_r( &l, 1 );
1255
1256 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001257 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001258 }
1259 while( mpi_cmp_int( &l, 1 ) <= 0 );
1260
1261 /* Z = l * Z */
1262 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1263
1264 /* X = l^2 * X */
1265 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1266 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1267
1268 /* Y = l^3 * Y */
1269 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1270 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1271
1272cleanup:
1273 mpi_free( &l ); mpi_free( &ll );
1274
1275 return( ret );
1276}
1277
1278/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001279 * Check and define parameters used by the comb method (see below for details)
1280 */
1281#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1282#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1283#endif
1284
1285/* d = ceil( n / w ) */
1286#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1287
1288/* number of precomputed points */
1289#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1290
1291/*
1292 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001293 *
1294 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001295 * modified version that provides resistance to SPA by avoiding zero
1296 * digits in the representation as in [3]. We modify the method further by
1297 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001298 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001299 *
1300 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1301 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1302 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001303 *
1304 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001305 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001306 * - w is the size, ie number of teeth, of the comb, and must be between
1307 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001308 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1309 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001310 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001311static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001312 unsigned char w, const mpi *m )
1313{
1314 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001315 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001316
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001317 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001318
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001319 /* First get the classical comb values (except for x_d = 0) */
1320 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001321 for( j = 0; j < w; j++ )
1322 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1323
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001324 /* Now make sure x_1 .. x_d are odd */
1325 c = 0;
1326 for( i = 1; i <= d; i++ )
1327 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001328 /* Add carry and update it */
1329 cc = x[i] & c;
1330 x[i] = x[i] ^ c;
1331 c = cc;
1332
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001333 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001334 adjust = 1 - ( x[i] & 0x01 );
1335 c |= x[i] & ( x[i-1] * adjust );
1336 x[i] = x[i] ^ ( x[i-1] * adjust );
1337 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001338 }
1339}
1340
1341/*
1342 * Precompute points for the comb method
1343 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001344 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1345 * 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 +01001346 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001347 * T must be able to hold 2^{w - 1} elements
1348 *
1349 * 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 +01001350 */
1351static int ecp_precompute_comb( const ecp_group *grp,
1352 ecp_point T[], const ecp_point *P,
1353 unsigned char w, size_t d )
1354{
1355 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001356 unsigned char i, k;
1357 size_t j;
1358 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001359
1360 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001361 * Set T[0] = P and
1362 * 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 +01001363 */
1364 MPI_CHK( ecp_copy( &T[0], P ) );
1365
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001366 k = 0;
1367 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001368 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001369 cur = T + i;
1370 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001371 for( j = 0; j < d; j++ )
1372 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001373
1374 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001375 }
1376
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001377 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001378
1379 /*
1380 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001381 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001382 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001383 k = 0;
1384 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001385 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001386 j = i;
1387 while( j-- )
1388 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001389 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001390 TT[k++] = &T[i + j];
1391 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001392 }
1393
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001394 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001395
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001396 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001397 * Post-precessing: reclaim some memory by
1398 * - not storing Z (always 1)
1399 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001400 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001401 */
1402 for( i = 0; i < ( 1U << (w-1) ); i++ )
1403 {
1404 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001405 mpi_shrink( &T[i].X, grp->P.n );
1406 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001407 }
1408
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001409cleanup:
1410 return( ret );
1411}
1412
1413/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001414 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001415 */
1416static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001417 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001418 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001419{
1420 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001421 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001422
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001423 /* Ignore the "sign" bit and scale down */
1424 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001425
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001426 /* Read the whole table to thwart cache-based timing attacks */
1427 for( j = 0; j < t_len; j++ )
1428 {
1429 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1430 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1431 }
1432
1433 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001434 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1435
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001436 /* Safely invert result if i is "negative" */
1437 MPI_CHK( ecp_safe_invert( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001438
1439cleanup:
1440 return( ret );
1441}
1442
1443/*
1444 * Core multiplication algorithm for the (modified) comb method.
1445 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001446 *
1447 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001448 */
1449static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001450 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001451 const unsigned char x[], size_t d,
1452 int (*f_rng)(void *, unsigned char *, size_t),
1453 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001454{
1455 int ret;
1456 ecp_point Txi;
1457 size_t i;
1458
1459 ecp_point_init( &Txi );
1460
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001461 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001462 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001463 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001464 if( f_rng != 0 )
1465 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001466
1467 while( i-- != 0 )
1468 {
1469 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001470 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001471 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001472 }
1473
1474cleanup:
1475 ecp_point_free( &Txi );
1476
1477 return( ret );
1478}
1479
1480/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001481 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001482 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001483int ecp_mul( ecp_group *grp, ecp_point *R,
1484 const mpi *m, const ecp_point *P,
1485 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001486{
1487 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001488 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1489 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001490 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001491 ecp_point *T;
1492 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001493
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001494 /*
1495 * Sanity checks (before we even initialize anything)
1496 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001497 if( mpi_cmp_int( &P->Z, 1 ) != 0 ||
1498 mpi_get_bit( &grp->N, 0 ) != 1 )
1499 {
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001500 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001501 }
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001502
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001503 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1504 return( ret );
1505
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001506 /* We'll need this later, but do it now to possibly avoid checking P */
1507 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001508 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001509
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001510 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1511 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001512
1513 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001514 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001515
1516 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001517 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001518 * 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 +01001519 * (see costs of the various parts, with 1S = 1M)
1520 */
1521 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001522
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001523 /*
1524 * If P == G, pre-compute a bit more, since this may be re-used later.
1525 * Just adding one ups the cost of the first mul by at most 3%.
1526 */
1527 if( p_eq_g )
1528 w++;
1529
1530 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001531 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001532 * (The last test is useful only for very small curves in the test suite.)
1533 */
1534 if( w > POLARSSL_ECP_WINDOW_SIZE )
1535 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001536 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001537 w = 2;
1538
1539 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001540 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001541 d = ( grp->nbits + w - 1 ) / w;
1542
1543 /*
1544 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001545 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001546 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001547 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001548
1549 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001550 {
1551 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1552 if( T == NULL )
1553 {
1554 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1555 goto cleanup;
1556 }
1557
1558 for( i = 0; i < pre_len; i++ )
1559 ecp_point_init( &T[i] );
1560
1561 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1562
1563 if( p_eq_g )
1564 {
1565 grp->T = T;
1566 grp->T_size = pre_len;
1567 }
1568 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001569
1570 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001571 * Make sure M is odd (M = m or M = N - m, since N is odd)
1572 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001573 */
1574 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001575 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001576 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1577 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001578
1579 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001580 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001581 */
1582 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001583 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 +01001584
1585 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001586 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001587 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001588 MPI_CHK( ecp_safe_invert( grp, R, ! m_is_odd ) );
1589 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001590
1591cleanup:
1592
1593 if( T != NULL && ! p_eq_g )
1594 {
1595 for( i = 0; i < pre_len; i++ )
1596 ecp_point_free( &T[i] );
1597 polarssl_free( T );
1598 }
1599
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001600 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001601 mpi_free( &mm );
1602
1603 if( ret != 0 )
1604 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001605
1606 return( ret );
1607}
1608
1609/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001610 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1611 */
1612int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1613{
1614 int ret;
1615 mpi YY, RHS;
1616
1617 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001618 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001619
1620 /*
1621 * pt coordinates must be normalized for our checks
1622 */
1623 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001624 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001625
1626 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1627 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1628 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1629 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001630 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001631
1632 mpi_init( &YY ); mpi_init( &RHS );
1633
1634 /*
1635 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001636 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001637 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001638 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1639 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001640 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001641 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1642 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001643
1644 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001645 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001646
1647cleanup:
1648
1649 mpi_free( &YY ); mpi_free( &RHS );
1650
1651 return( ret );
1652}
1653
1654/*
1655 * Check that an mpi is valid as a private key (SEC1 3.2)
1656 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001657int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001658{
1659 /* We want 1 <= d <= N-1 */
1660 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001661 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001662
1663 return( 0 );
1664}
1665
1666/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001667 * Generate a keypair (SEC1 3.2.1)
1668 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001669int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001670 int (*f_rng)(void *, unsigned char *, size_t),
1671 void *p_rng )
1672{
1673 int count = 0;
1674 size_t n_size = (grp->nbits + 7) / 8;
1675
1676 /*
1677 * Generate d such that 1 <= n < N
1678 */
1679 do
1680 {
1681 mpi_fill_random( d, n_size, f_rng, p_rng );
1682
1683 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1684 mpi_shift_r( d, 1 );
1685
1686 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001687 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001688 }
1689 while( mpi_cmp_int( d, 1 ) < 0 );
1690
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001691 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001692}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001693
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001694/*
1695 * Generate a keypair, prettier wrapper
1696 */
1697int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1698 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1699{
1700 int ret;
1701
1702 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1703 return( ret );
1704
1705 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1706}
1707
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001708#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard9fcceac2013-10-23 20:56:12 +02001709/*
1710 * Fast reduction modulo the primes used by the NIST curves.
1711 *
1712 * These functions are: critical for speed, but not need for correct
1713 * operations. So, we make the choice to heavily rely on the internals of our
1714 * bignum library, which creates a tight coupling between these functions and
1715 * our MPI implementation. However, the coupling between the ECP module and
1716 * MPI remains loose, since these functions can be deactivated at will.
1717 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001718
1719#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1720/*
1721 * Compared to the way things are presented in FIPS 186-3 D.2,
1722 * we proceed in columns, from right (least significant chunk) to left,
1723 * adding chunks to N in place, and keeping a carry for the next chunk.
1724 * This avoids moving things around in memory, and uselessly adding zeros,
1725 * compared to the more straightforward, line-oriented approach.
1726 *
1727 * For this prime we need to handle data in chunks of 64 bits.
1728 * Since this is always a multiple of our basic t_uint, we can
1729 * use a t_uint * to designate such a chunk, and small loops to handle it.
1730 */
1731
1732/* Add 64-bit chunks (dst += src) and update carry */
1733static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
1734{
1735 unsigned char i;
1736 t_uint c = 0;
1737 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
1738 {
1739 *dst += c; c = ( *dst < c );
1740 *dst += *src; c += ( *dst < *src );
1741 }
1742 *carry += c;
1743}
1744
1745/* Add carry to a 64-bit chunk and update carry */
1746static inline void carry64( t_uint *dst, t_uint *carry )
1747{
1748 unsigned char i;
1749 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
1750 {
1751 *dst += *carry;
1752 *carry = ( *dst < *carry );
1753 }
1754}
1755
1756#define WIDTH 8 / sizeof( t_uint )
1757#define A( i ) N->p + i * WIDTH
1758#define ADD( i ) add64( p, A( i ), &c )
1759#define NEXT p += WIDTH; carry64( p, &c )
1760#define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
1761
1762/*
1763 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
1764 */
1765static int ecp_mod_p192( mpi *N )
1766{
1767 int ret;
1768 t_uint c = 0;
1769 t_uint *p, *end;
1770
1771 /* Make sure we have enough blocks so that A(5) is legal */
1772 MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
1773
1774 p = N->p;
1775 end = p + N->n;
1776
1777 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
1778 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
1779 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
1780
1781cleanup:
1782 return( ret );
1783}
1784
1785#undef WIDTH
1786#undef A
1787#undef ADD
1788#undef NEXT
1789#undef LAST
1790#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
1791
1792#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
1793 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
1794 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1795/*
1796 * The reader is advised to first understand ecp_mod_p192() since the same
1797 * general structure is used here, but with additional complications:
1798 * (1) chunks of 32 bits, and (2) subtractions.
1799 */
1800
1801/*
1802 * For these primes, we need to handle data in chunks of 32 bits.
1803 * This makes it more complicated if we use 64 bits limbs in MPI,
1804 * which prevents us from using a uniform access method as for p192.
1805 *
1806 * So, we define a mini abstraction layer to access 32 bit chunks,
1807 * load them in 'cur' for work, and store them back from 'cur' when done.
1808 *
1809 * While at it, also define the size of N in terms of 32-bit chunks.
1810 */
1811#define LOAD32 cur = A( i );
1812
1813#if defined(POLARSSL_HAVE_INT8) /* 8 bit */
1814
1815#define MAX32 N->n / 4
1816#define A( j ) (uint32_t)( N->p[4*j+0] ) | \
1817 ( N->p[4*j+1] << 8 ) | \
1818 ( N->p[4*j+2] << 16 ) | \
1819 ( N->p[4*j+3] << 24 )
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001820#define STORE32 N->p[4*i+0] = (t_uint)( cur ); \
1821 N->p[4*i+1] = (t_uint)( cur >> 8 ); \
1822 N->p[4*i+2] = (t_uint)( cur >> 16 ); \
1823 N->p[4*i+3] = (t_uint)( cur >> 24 );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001824
1825#elif defined(POLARSSL_HAVE_INT16) /* 16 bit */
1826
1827#define MAX32 N->n / 2
1828#define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001829#define STORE32 N->p[2*i+0] = (t_uint)( cur ); \
1830 N->p[2*i+1] = (t_uint)( cur >> 16 );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001831
1832#elif defined(POLARSSL_HAVE_INT32) /* 32 bit */
1833
1834#define MAX32 N->n
1835#define A( j ) N->p[j]
1836#define STORE32 N->p[i] = cur;
1837
1838#else /* 64-bit */
1839
1840#define MAX32 N->n * 2
1841#define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
1842#define STORE32 \
1843 if( i % 2 ) { \
1844 N->p[i/2] &= 0x00000000FFFFFFFF; \
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001845 N->p[i/2] |= ((t_uint) cur) << 32; \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001846 } else { \
1847 N->p[i/2] &= 0xFFFFFFFF00000000; \
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001848 N->p[i/2] |= (t_uint) cur; \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001849 }
1850
1851#endif /* sizeof( t_uint ) */
1852
1853/*
1854 * Helpers for addition and subtraction of chunks, with signed carry.
1855 */
1856static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
1857{
1858 *dst += src;
1859 *carry += ( *dst < src );
1860}
1861
1862static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
1863{
1864 *carry -= ( *dst < src );
1865 *dst -= src;
1866}
1867
1868#define ADD( j ) add32( &cur, A( j ), &c );
1869#define SUB( j ) sub32( &cur, A( j ), &c );
1870
1871/*
1872 * Helpers for the main 'loop'
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001873 * (see fix_negative for the motivation of C)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001874 */
1875#define INIT( b ) \
1876 int ret; \
1877 signed char c = 0, cc; \
1878 uint32_t cur; \
1879 size_t i = 0, bits = b; \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001880 mpi C; \
1881 t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \
1882 \
1883 C.s = 1; \
1884 C.n = b / 8 / sizeof( t_uint) + 1; \
1885 C.p = Cp; \
1886 memset( Cp, 0, C.n * sizeof( t_uint ) ); \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001887 \
1888 MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
1889 LOAD32;
1890
1891#define NEXT \
1892 STORE32; i++; LOAD32; \
1893 cc = c; c = 0; \
1894 if( cc < 0 ) \
1895 sub32( &cur, -cc, &c ); \
1896 else \
1897 add32( &cur, cc, &c ); \
1898
1899#define LAST \
1900 STORE32; i++; \
1901 cur = c > 0 ? c : 0; STORE32; \
1902 cur = 0; while( ++i < MAX32 ) { STORE32; } \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001903 if( c < 0 ) fix_negative( N, c, &C, bits );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001904
1905/*
1906 * If the result is negative, we get it in the form
1907 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
1908 */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001909static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001910{
1911 int ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001912
1913 /* C = - c * 2^(bits + 32) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001914#if !defined(POLARSSL_HAVE_INT64)
1915 ((void) bits);
1916#else
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001917 if( bits == 224 )
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001918 C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001919 else
1920#endif
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001921 C->p[ C->n - 1 ] = (t_uint) -c;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001922
1923 /* N = - ( C - N ) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001924 MPI_CHK( mpi_sub_abs( N, C, N ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001925 N->s = -1;
1926
1927cleanup:
1928
1929 return( ret );
1930}
1931
1932#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1933/*
1934 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
1935 */
1936static int ecp_mod_p224( mpi *N )
1937{
1938 INIT( 224 );
1939
1940 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
1941 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
1942 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
1943 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
1944 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
1945 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
1946 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
1947
1948cleanup:
1949 return( ret );
1950}
1951#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1952
1953#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1954/*
1955 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
1956 */
1957static int ecp_mod_p256( mpi *N )
1958{
1959 INIT( 256 );
1960
1961 ADD( 8 ); ADD( 9 );
1962 SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0
1963
1964 ADD( 9 ); ADD( 10 );
1965 SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1
1966
1967 ADD( 10 ); ADD( 11 );
1968 SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2
1969
1970 ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
1971 SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3
1972
1973 ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
1974 SUB( 9 ); SUB( 10 ); NEXT; // A4
1975
1976 ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
1977 SUB( 10 ); SUB( 11 ); NEXT; // A5
1978
1979 ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
1980 SUB( 8 ); SUB( 9 ); NEXT; // A6
1981
1982 ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
1983 SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7
1984
1985cleanup:
1986 return( ret );
1987}
1988#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1989
1990#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1991/*
1992 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
1993 */
1994static int ecp_mod_p384( mpi *N )
1995{
1996 INIT( 384 );
1997
1998 ADD( 12 ); ADD( 21 ); ADD( 20 );
1999 SUB( 23 ); NEXT; // A0
2000
2001 ADD( 13 ); ADD( 22 ); ADD( 23 );
2002 SUB( 12 ); SUB( 20 ); NEXT; // A2
2003
2004 ADD( 14 ); ADD( 23 );
2005 SUB( 13 ); SUB( 21 ); NEXT; // A2
2006
2007 ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
2008 SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3
2009
2010 ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
2011 SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4
2012
2013 ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
2014 SUB( 16 ); NEXT; // A5
2015
2016 ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
2017 SUB( 17 ); NEXT; // A6
2018
2019 ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
2020 SUB( 18 ); NEXT; // A7
2021
2022 ADD( 20 ); ADD( 17 ); ADD( 16 );
2023 SUB( 19 ); NEXT; // A8
2024
2025 ADD( 21 ); ADD( 18 ); ADD( 17 );
2026 SUB( 20 ); NEXT; // A9
2027
2028 ADD( 22 ); ADD( 19 ); ADD( 18 );
2029 SUB( 21 ); NEXT; // A10
2030
2031 ADD( 23 ); ADD( 20 ); ADD( 19 );
2032 SUB( 22 ); LAST; // A11
2033
2034cleanup:
2035 return( ret );
2036}
2037#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
2038
2039#undef A
2040#undef LOAD32
2041#undef STORE32
2042#undef MAX32
2043#undef INIT
2044#undef NEXT
2045#undef LAST
2046
2047#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
2048 POLARSSL_ECP_DP_SECP256R1_ENABLED ||
2049 POLARSSL_ECP_DP_SECP384R1_ENABLED */
2050
2051#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
2052/*
2053 * Here we have an actual Mersenne prime, so things are more straightforward.
2054 * However, chunks are aligned on a 'weird' boundary (521 bits).
2055 */
2056
2057/* Size of p521 in terms of t_uint */
2058#define P521_WIDTH ( 521 / 8 / sizeof( t_uint ) + 1 )
2059
2060/* Bits to keep in the most significant t_uint */
2061#if defined(POLARSSL_HAVE_INT8)
2062#define P521_MASK 0x01
2063#else
2064#define P521_MASK 0x01FF
2065#endif
2066
2067/*
2068 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
2069 * Write N as A1 + 2^521 A0, return A0 + A1
2070 */
2071static int ecp_mod_p521( mpi *N )
2072{
2073 int ret;
2074 size_t i;
2075 mpi M;
2076 t_uint Mp[P521_WIDTH + 1];
2077 /* Worst case for the size of M is when t_uint is 16 bits:
2078 * we need to hold bits 513 to 1056, which is 34 limbs, that is
2079 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
2080
2081 if( N->n < P521_WIDTH )
2082 return( 0 );
2083
2084 /* M = A1 */
2085 M.s = 1;
2086 M.n = N->n - ( P521_WIDTH - 1 );
2087 if( M.n > P521_WIDTH + 1 )
2088 M.n = P521_WIDTH + 1;
2089 M.p = Mp;
2090 memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) );
2091 MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
2092
2093 /* N = A0 */
2094 N->p[P521_WIDTH - 1] &= P521_MASK;
2095 for( i = P521_WIDTH; i < N->n; i++ )
2096 N->p[i] = 0;
2097
2098 /* N = A0 + A1 */
2099 MPI_CHK( mpi_add_abs( N, N, &M ) );
2100
2101cleanup:
2102 return( ret );
2103}
2104
2105#undef P521_WIDTH
2106#undef P521_MASK
2107#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
2108
2109#endif /* POLARSSL_ECP_NIST_OPTIM */
2110
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002111#if defined(POLARSSL_SELF_TEST)
2112
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002113/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002114 * Checkup routine
2115 */
2116int ecp_self_test( int verbose )
2117{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002118 int ret;
2119 size_t i;
2120 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002121 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002122 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002123 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002124 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002125 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002126 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002127 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002128 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002129 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002130 "400000000000000000000000000000000000000000000000", /* one and zeros */
2131 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2132 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002133 };
2134
2135 ecp_group_init( &grp );
2136 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002137 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002138 mpi_init( &m );
2139
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002140 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002141#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002142 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002143#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002144 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
2145#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002146
2147 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002148 printf( " ECP test #1 (constant op_count, base point G): " );
2149
2150 /* Do a dummy multiplication first to trigger precomputation */
2151 MPI_CHK( mpi_lset( &m, 2 ) );
2152 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002153
2154 add_count = 0;
2155 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002156 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002157 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002158 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002159
2160 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2161 {
2162 add_c_prev = add_count;
2163 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002164 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002165 add_count = 0;
2166 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002167 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002168
2169 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002170 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002171
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002172 if( add_count != add_c_prev ||
2173 dbl_count != dbl_c_prev ||
2174 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002175 {
2176 if( verbose != 0 )
2177 printf( "failed (%zu)\n", i );
2178
2179 ret = 1;
2180 goto cleanup;
2181 }
2182 }
2183
2184 if( verbose != 0 )
2185 printf( "passed\n" );
2186
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002187 if( verbose != 0 )
2188 printf( " ECP test #2 (constant op_count, other point): " );
2189 /* We computed P = 2G last time, use it */
2190
2191 add_count = 0;
2192 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002193 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002194 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2195 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2196
2197 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2198 {
2199 add_c_prev = add_count;
2200 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002201 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002202 add_count = 0;
2203 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002204 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002205
2206 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2207 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2208
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002209 if( add_count != add_c_prev ||
2210 dbl_count != dbl_c_prev ||
2211 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002212 {
2213 if( verbose != 0 )
2214 printf( "failed (%zu)\n", i );
2215
2216 ret = 1;
2217 goto cleanup;
2218 }
2219 }
2220
2221 if( verbose != 0 )
2222 printf( "passed\n" );
2223
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002224cleanup:
2225
2226 if( ret < 0 && verbose != 0 )
2227 printf( "Unexpected error, return code = %08X\n", ret );
2228
2229 ecp_group_free( &grp );
2230 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002231 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002232 mpi_free( &m );
2233
2234 if( verbose != 0 )
2235 printf( "\n" );
2236
2237 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002238}
2239
2240#endif
2241
2242#endif