blob: ef38d19d7d2127f2d0f717a856e2ae7fa38648db [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é-Gonnard84338242012-11-11 20:45:18 +010058#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010059#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010060
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010061#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
62 !defined(EFI32)
63#define strcasecmp _stricmp
64#endif
65
Paul Bakker6a6087e2013-10-28 18:53:08 +010066#if defined(_MSC_VER) && !defined(inline)
67#define inline _inline
68#else
69#if defined(__ARMCC_VERSION) && !defined(inline)
70#define inline __inline
71#endif /* __ARMCC_VERSION */
72#endif /*_MSC_VER */
73
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010074#if defined(POLARSSL_SELF_TEST)
75/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010076 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020077 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010078 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010079unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010080#endif
81
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010082/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020083 * List of supported curves:
84 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020085 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020086 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020087 * - readable name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020088 */
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +020089const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020090{
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020091#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010092 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020093#endif
94#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010095 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020096#endif
97#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010098 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020099#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200100#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200101 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200102#endif
103#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200104 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200105#endif
106#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200107 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200108#endif
109#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200110 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200111#endif
112#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200113 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200114#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200115 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200116};
117
118/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200119 * List of supported curves and associated info
120 */
121const ecp_curve_info *ecp_curve_list( void )
122{
123 return ecp_supported_curves;
124}
125
126/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200127 * Get the curve info for the internal identifer
128 */
129const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
130{
131 const ecp_curve_info *curve_info;
132
133 for( curve_info = ecp_curve_list();
134 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
135 curve_info++ )
136 {
137 if( curve_info->grp_id == grp_id )
138 return( curve_info );
139 }
140
141 return( NULL );
142}
143
144/*
145 * Get the curve info from the TLS identifier
146 */
147const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
148{
149 const ecp_curve_info *curve_info;
150
151 for( curve_info = ecp_curve_list();
152 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
153 curve_info++ )
154 {
155 if( curve_info->tls_id == tls_id )
156 return( curve_info );
157 }
158
159 return( NULL );
160}
161
162/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100163 * Get the curve info from the name
164 */
165const ecp_curve_info *ecp_curve_info_from_name( const char *name )
166{
167 const ecp_curve_info *curve_info;
168
169 for( curve_info = ecp_curve_list();
170 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
171 curve_info++ )
172 {
173 if( strcasecmp( curve_info->name, name ) == 0 )
174 return( curve_info );
175 }
176
177 return( NULL );
178}
179
180/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100181 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100182 */
183void ecp_point_init( ecp_point *pt )
184{
185 if( pt == NULL )
186 return;
187
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100188 mpi_init( &pt->X );
189 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100190 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100191}
192
193/*
194 * Initialize (the components of) a group
195 */
196void ecp_group_init( ecp_group *grp )
197{
198 if( grp == NULL )
199 return;
200
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200201 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100202}
203
204/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200205 * Initialize (the components of) a key pair
206 */
207void ecp_keypair_init( ecp_keypair *key )
208{
209 if ( key == NULL )
210 return;
211
212 ecp_group_init( &key->grp );
213 mpi_init( &key->d );
214 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200215}
216
217/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100218 * Unallocate (the components of) a point
219 */
220void ecp_point_free( ecp_point *pt )
221{
222 if( pt == NULL )
223 return;
224
225 mpi_free( &( pt->X ) );
226 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100227 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100228}
229
230/*
231 * Unallocate (the components of) a group
232 */
233void ecp_group_free( ecp_group *grp )
234{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200235 size_t i;
236
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100237 if( grp == NULL )
238 return;
239
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100240 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200241 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100242 mpi_free( &grp->B );
243 ecp_point_free( &grp->G );
244 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200245
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200246 if( grp->T != NULL )
247 {
248 for( i = 0; i < grp->T_size; i++ )
249 ecp_point_free( &grp->T[i] );
250 polarssl_free( grp->T );
251 }
252
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200253 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100254}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100255
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100256/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200257 * Unallocate (the components of) a key pair
258 */
259void ecp_keypair_free( ecp_keypair *key )
260{
261 if ( key == NULL )
262 return;
263
264 ecp_group_free( &key->grp );
265 mpi_free( &key->d );
266 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200267}
268
269/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200270 * Copy the contents of a point
271 */
272int ecp_copy( ecp_point *P, const ecp_point *Q )
273{
274 int ret;
275
276 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
277 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
278 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
279
280cleanup:
281 return( ret );
282}
283
284/*
285 * Copy the contents of a group object
286 */
287int ecp_group_copy( ecp_group *dst, const ecp_group *src )
288{
289 return ecp_use_known_dp( dst, src->id );
290}
291
292/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100293 * Set point to zero
294 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100295int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100296{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100297 int ret;
298
299 MPI_CHK( mpi_lset( &pt->X , 1 ) );
300 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
301 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
302
303cleanup:
304 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100305}
306
307/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100308 * Tell if a point is zero
309 */
310int ecp_is_zero( ecp_point *pt )
311{
312 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
313}
314
315/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100316 * Import a non-zero point from ASCII strings
317 */
318int ecp_point_read_string( ecp_point *P, int radix,
319 const char *x, const char *y )
320{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100321 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100322
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100323 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
324 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100325 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100326
327cleanup:
328 return( ret );
329}
330
331/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100332 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100333 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100334int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100335 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100336 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100337{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200338 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100339 size_t plen;
340
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100341 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
342 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100343 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100344
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100345 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100346 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100347 */
348 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
349 {
350 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100351 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100352
353 buf[0] = 0x00;
354 *olen = 1;
355
356 return( 0 );
357 }
358
359 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100360
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100361 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
362 {
363 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100364
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100365 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100366 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100367
368 buf[0] = 0x04;
369 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
370 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
371 }
372 else if( format == POLARSSL_ECP_PF_COMPRESSED )
373 {
374 *olen = plen + 1;
375
376 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100377 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100378
379 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
380 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
381 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100382
383cleanup:
384 return( ret );
385}
386
387/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100388 * Import a point from unsigned binary data (SEC1 2.3.4)
389 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100390int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
391 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100392 int ret;
393 size_t plen;
394
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100395 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100396 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100397
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100398 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100399
400 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100401 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100402
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100403 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
404 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
405 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100406
407cleanup:
408 return( ret );
409}
410
411/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100412 * Import a point from a TLS ECPoint record (RFC 4492)
413 * struct {
414 * opaque point <1..2^8-1>;
415 * } ECPoint;
416 */
417int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100418 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100419{
420 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100421 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100422
423 /*
424 * We must have at least two bytes (1 for length, at least of for data)
425 */
426 if( buf_len < 2 )
427 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
428
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100429 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100430 if( data_len < 1 || data_len > buf_len - 1 )
431 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
432
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100433 /*
434 * Save buffer start for read_binary and update buf
435 */
436 buf_start = *buf;
437 *buf += data_len;
438
439 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100440}
441
442/*
443 * Export a point as a TLS ECPoint record (RFC 4492)
444 * struct {
445 * opaque point <1..2^8-1>;
446 * } ECPoint;
447 */
448int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100449 int format, size_t *olen,
450 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100451{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100452 int ret;
453
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100454 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100455 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100456 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100457 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100458 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
459
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100460 if( ( ret = ecp_point_write_binary( grp, pt, format,
461 olen, buf + 1, blen - 1) ) != 0 )
462 return( ret );
463
464 /*
465 * write length to the first byte and update total length
466 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200467 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100468 ++*olen;
469
470 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100471}
472
473/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200474 * Import an ECP group from ASCII strings, general case (A used)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100475 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200476static int ecp_group_read_string_gen( ecp_group *grp, int radix,
477 const char *p, const char *a, const char *b,
478 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100479{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100480 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100481
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200482 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
483 MPI_CHK( mpi_read_string( &grp->A, radix, a ) );
484 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
485 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
486 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100487
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200488 grp->pbits = mpi_msb( &grp->P );
489 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100490
491cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200492 if( ret != 0 )
493 ecp_group_free( grp );
494
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100495 return( ret );
496}
497
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200498/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200499 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200500 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200501int ecp_group_read_string( ecp_group *grp, int radix,
502 const char *p, const char *b,
503 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100504{
505 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100506
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200507 MPI_CHK( ecp_group_read_string_gen( grp, radix, p, "00", b, gx, gy, n ) );
508 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100509
510cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200511 if( ret != 0 )
512 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200513
514 return( ret );
515}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200516
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100517/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100518 * Domain parameters for secp192r1
519 */
520#define SECP192R1_P \
521 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
522#define SECP192R1_B \
523 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
524#define SECP192R1_GX \
525 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
526#define SECP192R1_GY \
527 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
528#define SECP192R1_N \
529 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
530
531/*
532 * Domain parameters for secp224r1
533 */
534#define SECP224R1_P \
535 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
536#define SECP224R1_B \
537 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
538#define SECP224R1_GX \
539 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
540#define SECP224R1_GY \
541 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
542#define SECP224R1_N \
543 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
544
545/*
546 * Domain parameters for secp256r1
547 */
548#define SECP256R1_P \
549 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
550#define SECP256R1_B \
551 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
552#define SECP256R1_GX \
553 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
554#define SECP256R1_GY \
555 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
556#define SECP256R1_N \
557 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
558
559/*
560 * Domain parameters for secp384r1
561 */
562#define SECP384R1_P \
563 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
564 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
565#define SECP384R1_B \
566 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
567 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
568#define SECP384R1_GX \
569 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
570 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
571#define SECP384R1_GY \
572 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
573 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
574#define SECP384R1_N \
575 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
576 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
577
578/*
579 * Domain parameters for secp521r1
580 */
581#define SECP521R1_P \
582 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
583 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
584 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
585#define SECP521R1_B \
586 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
587 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
588 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
589#define SECP521R1_GX \
590 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
591 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
592 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
593#define SECP521R1_GY \
594 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
595 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
596 "3FAD0761353C7086A272C24088BE94769FD16650"
597#define SECP521R1_N \
598 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
599 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
600 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
601
602/*
Manuel Pégourié-Gonnardcec4a532013-10-07 19:52:27 +0200603 * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
604 */
605#define BP256R1_P \
606 "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377"
607#define BP256R1_A \
608 "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9"
609#define BP256R1_B \
610 "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6"
611#define BP256R1_GX \
612 "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262"
613#define BP256R1_GY \
614 "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"
615#define BP256R1_N \
616 "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7"
617
618/*
619 * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
620 */
621#define BP384R1_P \
622 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB711" \
623 "23ACD3A729901D1A71874700133107EC53"
624#define BP384R1_A \
625 "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F9" \
626 "0F8AA5814A503AD4EB04A8C7DD22CE2826"
627#define BP384R1_B \
628 "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62" \
629 "D57CB4390295DBC9943AB78696FA504C11"
630#define BP384R1_GX \
631 "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10" \
632 "E8E826E03436D646AAEF87B2E247D4AF1E"
633#define BP384R1_GY \
634 "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129" \
635 "280E4646217791811142820341263C5315"
636#define BP384R1_N \
637 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425" \
638 "A7CF3AB6AF6B7FC3103B883202E9046565"
639
640/*
641 * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
642 */
643#define BP512R1_P \
644 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
645 "717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"
646#define BP512R1_A \
647 "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863" \
648 "BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA"
649#define BP512R1_B \
650 "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117" \
651 "A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723"
652#define BP512R1_GX \
653 "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D009" \
654 "8EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822"
655#define BP512R1_GY \
656 "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F81" \
657 "11B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"
658#define BP512R1_N \
659 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
660 "70553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"
661
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200662#if defined(POLARSSL_ECP_NIST_OPTIM)
663/* Forward declarations */
664static int ecp_mod_p192( mpi * );
665static int ecp_mod_p224( mpi * );
666static int ecp_mod_p256( mpi * );
667static int ecp_mod_p384( mpi * );
668static int ecp_mod_p521( mpi * );
669#endif
670
Manuel Pégourié-Gonnardcec4a532013-10-07 19:52:27 +0200671/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100672 * Set a group using well-known domain parameters
673 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100674int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100675{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100676 grp->id = id;
677
678 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100679 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200680#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100681 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200682#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100683 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200684#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100685 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100686 SECP192R1_P, SECP192R1_B,
687 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200688#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100689
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200690#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100691 case POLARSSL_ECP_DP_SECP224R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200692#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200693 grp->modp = ecp_mod_p224;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200694#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100695 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100696 SECP224R1_P, SECP224R1_B,
697 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200698#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100699
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200700#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100701 case POLARSSL_ECP_DP_SECP256R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200702#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnardec655c92013-10-23 14:50:39 +0200703 grp->modp = ecp_mod_p256;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200704#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100705 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100706 SECP256R1_P, SECP256R1_B,
707 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200708#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100709
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200710#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100711 case POLARSSL_ECP_DP_SECP384R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200712#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard0f9149c2013-10-23 15:06:37 +0200713 grp->modp = ecp_mod_p384;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200714#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100715 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100716 SECP384R1_P, SECP384R1_B,
717 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200718#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100719
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200720#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100721 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200722#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100723 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200724#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100725 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100726 SECP521R1_P, SECP521R1_B,
727 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200728#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100729
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200730#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
731 case POLARSSL_ECP_DP_BP256R1:
732 return( ecp_group_read_string_gen( grp, 16,
733 BP256R1_P, BP256R1_A, BP256R1_B,
734 BP256R1_GX, BP256R1_GY, BP256R1_N ) );
735#endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
736
737#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
738 case POLARSSL_ECP_DP_BP384R1:
739 return( ecp_group_read_string_gen( grp, 16,
740 BP384R1_P, BP384R1_A, BP384R1_B,
741 BP384R1_GX, BP384R1_GY, BP384R1_N ) );
742#endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
743
744#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
745 case POLARSSL_ECP_DP_BP512R1:
746 return( ecp_group_read_string_gen( grp, 16,
747 BP512R1_P, BP512R1_A, BP512R1_B,
748 BP512R1_GX, BP512R1_GY, BP512R1_N ) );
749#endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
750
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200751 default:
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200752 ecp_group_free( grp );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200753 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
754 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100755}
756
757/*
758 * Set a group from an ECParameters record (RFC 4492)
759 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100760int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100761{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200762 uint16_t tls_id;
763 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100764
765 /*
766 * We expect at least three bytes (see below)
767 */
768 if( len < 3 )
769 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
770
771 /*
772 * First byte is curve_type; only named_curve is handled
773 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100774 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100775 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
776
777 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100778 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100779 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200780 tls_id = *(*buf)++;
781 tls_id <<= 8;
782 tls_id |= *(*buf)++;
783
784 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
785 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
786
787 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100788}
789
790/*
791 * Write the ECParameters record corresponding to a group (RFC 4492)
792 */
793int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
794 unsigned char *buf, size_t blen )
795{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200796 const ecp_curve_info *curve_info;
797
798 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
799 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200800
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100801 /*
802 * We are going to write 3 bytes (see below)
803 */
804 *olen = 3;
805 if( blen < *olen )
806 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
807
808 /*
809 * First byte is curve_type, always named_curve
810 */
811 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
812
813 /*
814 * Next two bytes are the namedcurve value
815 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200816 buf[0] = curve_info->tls_id >> 8;
817 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100818
819 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100820}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100821
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200822/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200823 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
824 * See the documentation of struct ecp_group.
825 *
826 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200827 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200828static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200829{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200830 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200831
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200832 if( grp->modp == NULL )
833 return( mpi_mod_mpi( N, N, &grp->P ) );
834
835 /* N->s < 0 is a much faster test, which fails only if N is 0 */
836 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
837 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200838 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200839 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200840 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200841
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200842 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200843
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200844 /* N->s < 0 is a much faster test, which fails only if N is 0 */
845 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
846 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200847
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200848 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
849 /* we known P, N and the result are positive */
850 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200851
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200852cleanup:
853 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200854}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200855
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100856/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100857 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100858 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100859 * In order to guarantee that, we need to ensure that operands of
860 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100861 * bring the result back to this range.
862 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100863 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100864 */
865
866/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100867 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
868 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100869#if defined(POLARSSL_SELF_TEST)
870#define INC_MUL_COUNT mul_count++;
871#else
872#define INC_MUL_COUNT
873#endif
874
875#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
876 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100877
878/*
879 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200880 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100881 */
882#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200883 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100884 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
885
886/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200887 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
888 * We known P, N and the result are positive, so sub_abs is correct, and
889 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100890 */
891#define MOD_ADD( N ) \
892 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200893 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100894
895/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100896 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100897 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100898 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100899static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100900{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100901 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100902 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100903
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100904 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100905 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100906
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100907 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100908
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100909 /*
910 * X = X / Z^2 mod p
911 */
912 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
913 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
914 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100915
916 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100917 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100918 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100919 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
920 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100921
922 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100923 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100924 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100925 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100926
927cleanup:
928
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100929 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100930
931 return( ret );
932}
933
934/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100935 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100936 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100937 * (See for example Cohen's "A Course in Computational Algebraic Number
938 * Theory", Algorithm 10.3.4.)
939 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200940 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100941 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100942 *
943 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100944 */
945static int ecp_normalize_many( const ecp_group *grp,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100946 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100947{
948 int ret;
949 size_t i;
950 mpi *c, u, Zi, ZZi;
951
952 if( t_len < 2 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100953 return( ecp_normalize( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100954
Paul Bakker6e339b52013-07-03 13:37:05 +0200955 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200956 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100957
958 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
959 for( i = 0; i < t_len; i++ )
960 mpi_init( &c[i] );
961
962 /*
963 * c[i] = Z_0 * ... * Z_i
964 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100965 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100966 for( i = 1; i < t_len; i++ )
967 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100968 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100969 MOD_MUL( c[i] );
970 }
971
972 /*
973 * u = 1 / (Z_0 * ... * Z_n) mod P
974 */
975 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
976
977 for( i = t_len - 1; ; i-- )
978 {
979 /*
980 * Zi = 1 / Z_i mod p
981 * u = 1 / (Z_0 * ... * Z_i) mod P
982 */
983 if( i == 0 ) {
984 MPI_CHK( mpi_copy( &Zi, &u ) );
985 }
986 else
987 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100988 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
989 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100990 }
991
992 /*
993 * proceed as in normalize()
994 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100995 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
996 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
997 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
998 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
999 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001000
1001 if( i == 0 )
1002 break;
1003 }
1004
1005cleanup:
1006
1007 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
1008 for( i = 0; i < t_len; i++ )
1009 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +02001010 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001011
1012 return( ret );
1013}
1014
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001015/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001016 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1017 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1018 */
1019static int ecp_safe_invert( const ecp_group *grp,
1020 ecp_point *Q,
1021 unsigned char inv )
1022{
1023 int ret;
1024 unsigned char nonzero;
1025 mpi mQY;
1026
1027 mpi_init( &mQY );
1028
1029 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
1030 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1031 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
1032 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
1033
1034cleanup:
1035 mpi_free( &mQY );
1036
1037 return( ret );
1038}
1039
1040/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001041 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001042 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001043 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001044 * with heavy variable renaming, some reordering and one minor modification
1045 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
1046 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001047 *
1048 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001049 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001050static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
1051 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001052{
1053 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001054 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001055
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001056#if defined(POLARSSL_SELF_TEST)
1057 dbl_count++;
1058#endif
1059
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001060 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
1061 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001062
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001063 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
1064 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
1065 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
1066 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
1067 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
1068 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
1069 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
1070 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
1071 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
1072 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
1073 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
1074 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
1075 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
1076 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
1077 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1078 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1079 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
1080 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
1081 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
1082 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
1083 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
1084 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
1085 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
1086 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001087
1088 MPI_CHK( mpi_copy( &R->X, &X3 ) );
1089 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
1090 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
1091
1092cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001093 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
1094 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001095
1096 return( ret );
1097}
1098
1099/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001100 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001101 *
1102 * The coordinates of Q must be normalized (= affine),
1103 * but those of P don't need to. R is not normalized.
1104 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001105 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
1106 * None of these cases can happen as intermediate step in ecp_mul():
1107 * - at each step, P, Q and R are multiples of the base point, the factor
1108 * being less than its order, so none of them is zero;
1109 * - Q is an odd multiple of the base point, P an even multiple,
1110 * due to the choice of precomputed points in the modified comb method.
1111 * So branches for these cases do not leak secret information.
1112 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001113 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001114 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001115static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001116 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001117{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001118 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001119 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001120
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001121#if defined(POLARSSL_SELF_TEST)
1122 add_count++;
1123#endif
1124
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001125 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001126 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001127 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001128 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1129 return( ecp_copy( R, Q ) );
1130
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001131 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1132 return( ecp_copy( R, P ) );
1133
1134 /*
1135 * Make sure Q coordinates are normalized
1136 */
1137 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001138 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001139
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001140 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1141 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001142
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001143 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1144 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1145 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1146 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1147 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1148 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001149
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001150 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001151 if( mpi_cmp_int( &T1, 0 ) == 0 )
1152 {
1153 if( mpi_cmp_int( &T2, 0 ) == 0 )
1154 {
1155 ret = ecp_double_jac( grp, R, P );
1156 goto cleanup;
1157 }
1158 else
1159 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001160 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001161 goto cleanup;
1162 }
1163 }
1164
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001165 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1166 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1167 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1168 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1169 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1170 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1171 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1172 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1173 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1174 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1175 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1176 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001177
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001178 MPI_CHK( mpi_copy( &R->X, &X ) );
1179 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1180 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001181
1182cleanup:
1183
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001184 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1185 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001186
1187 return( ret );
1188}
1189
1190/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001191 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001192 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001193 */
1194int ecp_add( const ecp_group *grp, ecp_point *R,
1195 const ecp_point *P, const ecp_point *Q )
1196{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001197 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001198
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001199 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001200 MPI_CHK( ecp_normalize( grp, R ) );
1201
1202cleanup:
1203 return( ret );
1204}
1205
1206/*
1207 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001208 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001209 */
1210int ecp_sub( const ecp_group *grp, ecp_point *R,
1211 const ecp_point *P, const ecp_point *Q )
1212{
1213 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001214 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001215
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001216 ecp_point_init( &mQ );
1217
1218 /* mQ = - Q */
1219 ecp_copy( &mQ, Q );
1220 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1221 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1222
1223 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001224 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001225
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001226cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001227 ecp_point_free( &mQ );
1228
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001229 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001230}
1231
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001232/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001233 * Randomize jacobian coordinates:
1234 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1235 * This is sort of the reverse operation of ecp_normalize().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001236 *
1237 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001238 */
1239static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1240 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1241{
1242 int ret;
1243 mpi l, ll;
1244 size_t p_size = (grp->pbits + 7) / 8;
1245 int count = 0;
1246
1247 mpi_init( &l ); mpi_init( &ll );
1248
1249 /* Generate l such that 1 < l < p */
1250 do
1251 {
1252 mpi_fill_random( &l, p_size, f_rng, p_rng );
1253
1254 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1255 mpi_shift_r( &l, 1 );
1256
1257 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001258 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001259 }
1260 while( mpi_cmp_int( &l, 1 ) <= 0 );
1261
1262 /* Z = l * Z */
1263 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1264
1265 /* X = l^2 * X */
1266 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1267 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1268
1269 /* Y = l^3 * Y */
1270 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1271 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1272
1273cleanup:
1274 mpi_free( &l ); mpi_free( &ll );
1275
1276 return( ret );
1277}
1278
1279/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001280 * Check and define parameters used by the comb method (see below for details)
1281 */
1282#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1283#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1284#endif
1285
1286/* d = ceil( n / w ) */
1287#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1288
1289/* number of precomputed points */
1290#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1291
1292/*
1293 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001294 *
1295 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001296 * modified version that provides resistance to SPA by avoiding zero
1297 * digits in the representation as in [3]. We modify the method further by
1298 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001299 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001300 *
1301 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1302 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1303 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001304 *
1305 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001306 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001307 * - w is the size, ie number of teeth, of the comb, and must be between
1308 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001309 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1310 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001311 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001312static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001313 unsigned char w, const mpi *m )
1314{
1315 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001316 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001317
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001318 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001319
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001320 /* First get the classical comb values (except for x_d = 0) */
1321 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001322 for( j = 0; j < w; j++ )
1323 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1324
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001325 /* Now make sure x_1 .. x_d are odd */
1326 c = 0;
1327 for( i = 1; i <= d; i++ )
1328 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001329 /* Add carry and update it */
1330 cc = x[i] & c;
1331 x[i] = x[i] ^ c;
1332 c = cc;
1333
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001334 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001335 adjust = 1 - ( x[i] & 0x01 );
1336 c |= x[i] & ( x[i-1] * adjust );
1337 x[i] = x[i] ^ ( x[i-1] * adjust );
1338 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001339 }
1340}
1341
1342/*
1343 * Precompute points for the comb method
1344 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001345 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1346 * 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 +01001347 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001348 * T must be able to hold 2^{w - 1} elements
1349 *
1350 * 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 +01001351 */
1352static int ecp_precompute_comb( const ecp_group *grp,
1353 ecp_point T[], const ecp_point *P,
1354 unsigned char w, size_t d )
1355{
1356 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001357 unsigned char i, k;
1358 size_t j;
1359 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001360
1361 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001362 * Set T[0] = P and
1363 * 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 +01001364 */
1365 MPI_CHK( ecp_copy( &T[0], P ) );
1366
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001367 k = 0;
1368 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001369 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001370 cur = T + i;
1371 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001372 for( j = 0; j < d; j++ )
1373 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001374
1375 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001376 }
1377
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001378 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001379
1380 /*
1381 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001382 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001383 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001384 k = 0;
1385 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001386 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001387 j = i;
1388 while( j-- )
1389 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001390 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001391 TT[k++] = &T[i + j];
1392 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001393 }
1394
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001395 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001396
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001397 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001398 * Post-precessing: reclaim some memory by
1399 * - not storing Z (always 1)
1400 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001401 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001402 */
1403 for( i = 0; i < ( 1U << (w-1) ); i++ )
1404 {
1405 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001406 mpi_shrink( &T[i].X, grp->P.n );
1407 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001408 }
1409
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001410cleanup:
1411 return( ret );
1412}
1413
1414/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001415 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001416 */
1417static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001418 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001419 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001420{
1421 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001422 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001423
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001424 /* Ignore the "sign" bit and scale down */
1425 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001426
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001427 /* Read the whole table to thwart cache-based timing attacks */
1428 for( j = 0; j < t_len; j++ )
1429 {
1430 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1431 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1432 }
1433
1434 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001435 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1436
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001437 /* Safely invert result if i is "negative" */
1438 MPI_CHK( ecp_safe_invert( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001439
1440cleanup:
1441 return( ret );
1442}
1443
1444/*
1445 * Core multiplication algorithm for the (modified) comb method.
1446 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001447 *
1448 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001449 */
1450static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001451 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001452 const unsigned char x[], size_t d,
1453 int (*f_rng)(void *, unsigned char *, size_t),
1454 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001455{
1456 int ret;
1457 ecp_point Txi;
1458 size_t i;
1459
1460 ecp_point_init( &Txi );
1461
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001462 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001463 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001464 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001465 if( f_rng != 0 )
1466 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001467
1468 while( i-- != 0 )
1469 {
1470 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001471 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001472 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001473 }
1474
1475cleanup:
1476 ecp_point_free( &Txi );
1477
1478 return( ret );
1479}
1480
1481/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001482 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001483 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001484int ecp_mul( ecp_group *grp, ecp_point *R,
1485 const mpi *m, const ecp_point *P,
1486 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001487{
1488 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001489 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1490 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001491 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001492 ecp_point *T;
1493 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001494
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001495 /*
1496 * Sanity checks (before we even initialize anything)
1497 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001498 if( mpi_cmp_int( &P->Z, 1 ) != 0 ||
1499 mpi_get_bit( &grp->N, 0 ) != 1 )
1500 {
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001501 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001502 }
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001503
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001504 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1505 return( ret );
1506
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001507 /* We'll need this later, but do it now to possibly avoid checking P */
1508 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001509 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001510
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001511 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1512 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001513
1514 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001515 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001516
1517 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001518 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001519 * 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 +01001520 * (see costs of the various parts, with 1S = 1M)
1521 */
1522 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001523
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001524 /*
1525 * If P == G, pre-compute a bit more, since this may be re-used later.
1526 * Just adding one ups the cost of the first mul by at most 3%.
1527 */
1528 if( p_eq_g )
1529 w++;
1530
1531 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001532 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001533 * (The last test is useful only for very small curves in the test suite.)
1534 */
1535 if( w > POLARSSL_ECP_WINDOW_SIZE )
1536 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001537 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001538 w = 2;
1539
1540 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001541 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001542 d = ( grp->nbits + w - 1 ) / w;
1543
1544 /*
1545 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001546 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001547 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001548 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001549
1550 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001551 {
1552 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1553 if( T == NULL )
1554 {
1555 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1556 goto cleanup;
1557 }
1558
1559 for( i = 0; i < pre_len; i++ )
1560 ecp_point_init( &T[i] );
1561
1562 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1563
1564 if( p_eq_g )
1565 {
1566 grp->T = T;
1567 grp->T_size = pre_len;
1568 }
1569 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001570
1571 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001572 * Make sure M is odd (M = m or M = N - m, since N is odd)
1573 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001574 */
1575 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001576 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001577 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1578 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001579
1580 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001581 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001582 */
1583 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001584 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 +01001585
1586 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001587 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001588 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001589 MPI_CHK( ecp_safe_invert( grp, R, ! m_is_odd ) );
1590 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001591
1592cleanup:
1593
1594 if( T != NULL && ! p_eq_g )
1595 {
1596 for( i = 0; i < pre_len; i++ )
1597 ecp_point_free( &T[i] );
1598 polarssl_free( T );
1599 }
1600
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001601 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001602 mpi_free( &mm );
1603
1604 if( ret != 0 )
1605 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001606
1607 return( ret );
1608}
1609
1610/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001611 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1612 */
1613int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1614{
1615 int ret;
1616 mpi YY, RHS;
1617
1618 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001619 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001620
1621 /*
1622 * pt coordinates must be normalized for our checks
1623 */
1624 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001625 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001626
1627 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1628 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1629 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1630 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001631 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001632
1633 mpi_init( &YY ); mpi_init( &RHS );
1634
1635 /*
1636 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001637 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001638 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001639 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1640 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001641 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001642 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1643 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001644
1645 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001646 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001647
1648cleanup:
1649
1650 mpi_free( &YY ); mpi_free( &RHS );
1651
1652 return( ret );
1653}
1654
1655/*
1656 * Check that an mpi is valid as a private key (SEC1 3.2)
1657 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001658int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001659{
1660 /* We want 1 <= d <= N-1 */
1661 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001662 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001663
1664 return( 0 );
1665}
1666
1667/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001668 * Generate a keypair (SEC1 3.2.1)
1669 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001670int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001671 int (*f_rng)(void *, unsigned char *, size_t),
1672 void *p_rng )
1673{
1674 int count = 0;
1675 size_t n_size = (grp->nbits + 7) / 8;
1676
1677 /*
1678 * Generate d such that 1 <= n < N
1679 */
1680 do
1681 {
1682 mpi_fill_random( d, n_size, f_rng, p_rng );
1683
1684 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1685 mpi_shift_r( d, 1 );
1686
1687 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001688 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001689 }
1690 while( mpi_cmp_int( d, 1 ) < 0 );
1691
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001692 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001693}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001694
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001695/*
1696 * Generate a keypair, prettier wrapper
1697 */
1698int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1699 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1700{
1701 int ret;
1702
1703 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1704 return( ret );
1705
1706 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1707}
1708
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001709#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard9fcceac2013-10-23 20:56:12 +02001710/*
1711 * Fast reduction modulo the primes used by the NIST curves.
1712 *
1713 * These functions are: critical for speed, but not need for correct
1714 * operations. So, we make the choice to heavily rely on the internals of our
1715 * bignum library, which creates a tight coupling between these functions and
1716 * our MPI implementation. However, the coupling between the ECP module and
1717 * MPI remains loose, since these functions can be deactivated at will.
1718 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001719
1720#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1721/*
1722 * Compared to the way things are presented in FIPS 186-3 D.2,
1723 * we proceed in columns, from right (least significant chunk) to left,
1724 * adding chunks to N in place, and keeping a carry for the next chunk.
1725 * This avoids moving things around in memory, and uselessly adding zeros,
1726 * compared to the more straightforward, line-oriented approach.
1727 *
1728 * For this prime we need to handle data in chunks of 64 bits.
1729 * Since this is always a multiple of our basic t_uint, we can
1730 * use a t_uint * to designate such a chunk, and small loops to handle it.
1731 */
1732
1733/* Add 64-bit chunks (dst += src) and update carry */
1734static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
1735{
1736 unsigned char i;
1737 t_uint c = 0;
1738 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
1739 {
1740 *dst += c; c = ( *dst < c );
1741 *dst += *src; c += ( *dst < *src );
1742 }
1743 *carry += c;
1744}
1745
1746/* Add carry to a 64-bit chunk and update carry */
1747static inline void carry64( t_uint *dst, t_uint *carry )
1748{
1749 unsigned char i;
1750 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
1751 {
1752 *dst += *carry;
1753 *carry = ( *dst < *carry );
1754 }
1755}
1756
1757#define WIDTH 8 / sizeof( t_uint )
1758#define A( i ) N->p + i * WIDTH
1759#define ADD( i ) add64( p, A( i ), &c )
1760#define NEXT p += WIDTH; carry64( p, &c )
1761#define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
1762
1763/*
1764 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
1765 */
1766static int ecp_mod_p192( mpi *N )
1767{
1768 int ret;
1769 t_uint c = 0;
1770 t_uint *p, *end;
1771
1772 /* Make sure we have enough blocks so that A(5) is legal */
1773 MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
1774
1775 p = N->p;
1776 end = p + N->n;
1777
1778 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
1779 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
1780 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
1781
1782cleanup:
1783 return( ret );
1784}
1785
1786#undef WIDTH
1787#undef A
1788#undef ADD
1789#undef NEXT
1790#undef LAST
1791#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
1792
1793#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
1794 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
1795 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1796/*
1797 * The reader is advised to first understand ecp_mod_p192() since the same
1798 * general structure is used here, but with additional complications:
1799 * (1) chunks of 32 bits, and (2) subtractions.
1800 */
1801
1802/*
1803 * For these primes, we need to handle data in chunks of 32 bits.
1804 * This makes it more complicated if we use 64 bits limbs in MPI,
1805 * which prevents us from using a uniform access method as for p192.
1806 *
1807 * So, we define a mini abstraction layer to access 32 bit chunks,
1808 * load them in 'cur' for work, and store them back from 'cur' when done.
1809 *
1810 * While at it, also define the size of N in terms of 32-bit chunks.
1811 */
1812#define LOAD32 cur = A( i );
1813
1814#if defined(POLARSSL_HAVE_INT8) /* 8 bit */
1815
1816#define MAX32 N->n / 4
1817#define A( j ) (uint32_t)( N->p[4*j+0] ) | \
1818 ( N->p[4*j+1] << 8 ) | \
1819 ( N->p[4*j+2] << 16 ) | \
1820 ( N->p[4*j+3] << 24 )
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001821#define STORE32 N->p[4*i+0] = (t_uint)( cur ); \
1822 N->p[4*i+1] = (t_uint)( cur >> 8 ); \
1823 N->p[4*i+2] = (t_uint)( cur >> 16 ); \
1824 N->p[4*i+3] = (t_uint)( cur >> 24 );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001825
1826#elif defined(POLARSSL_HAVE_INT16) /* 16 bit */
1827
1828#define MAX32 N->n / 2
1829#define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001830#define STORE32 N->p[2*i+0] = (t_uint)( cur ); \
1831 N->p[2*i+1] = (t_uint)( cur >> 16 );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001832
1833#elif defined(POLARSSL_HAVE_INT32) /* 32 bit */
1834
1835#define MAX32 N->n
1836#define A( j ) N->p[j]
1837#define STORE32 N->p[i] = cur;
1838
1839#else /* 64-bit */
1840
1841#define MAX32 N->n * 2
1842#define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
1843#define STORE32 \
1844 if( i % 2 ) { \
1845 N->p[i/2] &= 0x00000000FFFFFFFF; \
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001846 N->p[i/2] |= ((t_uint) cur) << 32; \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001847 } else { \
1848 N->p[i/2] &= 0xFFFFFFFF00000000; \
Manuel Pégourié-Gonnardc57b6542013-11-25 16:02:53 +01001849 N->p[i/2] |= (t_uint) cur; \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001850 }
1851
1852#endif /* sizeof( t_uint ) */
1853
1854/*
1855 * Helpers for addition and subtraction of chunks, with signed carry.
1856 */
1857static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
1858{
1859 *dst += src;
1860 *carry += ( *dst < src );
1861}
1862
1863static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
1864{
1865 *carry -= ( *dst < src );
1866 *dst -= src;
1867}
1868
1869#define ADD( j ) add32( &cur, A( j ), &c );
1870#define SUB( j ) sub32( &cur, A( j ), &c );
1871
1872/*
1873 * Helpers for the main 'loop'
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001874 * (see fix_negative for the motivation of C)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001875 */
1876#define INIT( b ) \
1877 int ret; \
1878 signed char c = 0, cc; \
1879 uint32_t cur; \
1880 size_t i = 0, bits = b; \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001881 mpi C; \
1882 t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \
1883 \
1884 C.s = 1; \
1885 C.n = b / 8 / sizeof( t_uint) + 1; \
1886 C.p = Cp; \
1887 memset( Cp, 0, C.n * sizeof( t_uint ) ); \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001888 \
1889 MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
1890 LOAD32;
1891
1892#define NEXT \
1893 STORE32; i++; LOAD32; \
1894 cc = c; c = 0; \
1895 if( cc < 0 ) \
1896 sub32( &cur, -cc, &c ); \
1897 else \
1898 add32( &cur, cc, &c ); \
1899
1900#define LAST \
1901 STORE32; i++; \
1902 cur = c > 0 ? c : 0; STORE32; \
1903 cur = 0; while( ++i < MAX32 ) { STORE32; } \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001904 if( c < 0 ) fix_negative( N, c, &C, bits );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001905
1906/*
1907 * If the result is negative, we get it in the form
1908 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
1909 */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001910static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001911{
1912 int ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001913
1914 /* C = - c * 2^(bits + 32) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001915#if !defined(POLARSSL_HAVE_INT64)
1916 ((void) bits);
1917#else
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001918 if( bits == 224 )
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001919 C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001920 else
1921#endif
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001922 C->p[ C->n - 1 ] = (t_uint) -c;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001923
1924 /* N = - ( C - N ) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001925 MPI_CHK( mpi_sub_abs( N, C, N ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001926 N->s = -1;
1927
1928cleanup:
1929
1930 return( ret );
1931}
1932
1933#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1934/*
1935 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
1936 */
1937static int ecp_mod_p224( mpi *N )
1938{
1939 INIT( 224 );
1940
1941 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
1942 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
1943 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
1944 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
1945 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
1946 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
1947 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
1948
1949cleanup:
1950 return( ret );
1951}
1952#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1953
1954#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1955/*
1956 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
1957 */
1958static int ecp_mod_p256( mpi *N )
1959{
1960 INIT( 256 );
1961
1962 ADD( 8 ); ADD( 9 );
1963 SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0
1964
1965 ADD( 9 ); ADD( 10 );
1966 SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1
1967
1968 ADD( 10 ); ADD( 11 );
1969 SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2
1970
1971 ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
1972 SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3
1973
1974 ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
1975 SUB( 9 ); SUB( 10 ); NEXT; // A4
1976
1977 ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
1978 SUB( 10 ); SUB( 11 ); NEXT; // A5
1979
1980 ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
1981 SUB( 8 ); SUB( 9 ); NEXT; // A6
1982
1983 ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
1984 SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7
1985
1986cleanup:
1987 return( ret );
1988}
1989#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1990
1991#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1992/*
1993 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
1994 */
1995static int ecp_mod_p384( mpi *N )
1996{
1997 INIT( 384 );
1998
1999 ADD( 12 ); ADD( 21 ); ADD( 20 );
2000 SUB( 23 ); NEXT; // A0
2001
2002 ADD( 13 ); ADD( 22 ); ADD( 23 );
2003 SUB( 12 ); SUB( 20 ); NEXT; // A2
2004
2005 ADD( 14 ); ADD( 23 );
2006 SUB( 13 ); SUB( 21 ); NEXT; // A2
2007
2008 ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
2009 SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3
2010
2011 ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
2012 SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4
2013
2014 ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
2015 SUB( 16 ); NEXT; // A5
2016
2017 ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
2018 SUB( 17 ); NEXT; // A6
2019
2020 ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
2021 SUB( 18 ); NEXT; // A7
2022
2023 ADD( 20 ); ADD( 17 ); ADD( 16 );
2024 SUB( 19 ); NEXT; // A8
2025
2026 ADD( 21 ); ADD( 18 ); ADD( 17 );
2027 SUB( 20 ); NEXT; // A9
2028
2029 ADD( 22 ); ADD( 19 ); ADD( 18 );
2030 SUB( 21 ); NEXT; // A10
2031
2032 ADD( 23 ); ADD( 20 ); ADD( 19 );
2033 SUB( 22 ); LAST; // A11
2034
2035cleanup:
2036 return( ret );
2037}
2038#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
2039
2040#undef A
2041#undef LOAD32
2042#undef STORE32
2043#undef MAX32
2044#undef INIT
2045#undef NEXT
2046#undef LAST
2047
2048#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
2049 POLARSSL_ECP_DP_SECP256R1_ENABLED ||
2050 POLARSSL_ECP_DP_SECP384R1_ENABLED */
2051
2052#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
2053/*
2054 * Here we have an actual Mersenne prime, so things are more straightforward.
2055 * However, chunks are aligned on a 'weird' boundary (521 bits).
2056 */
2057
2058/* Size of p521 in terms of t_uint */
2059#define P521_WIDTH ( 521 / 8 / sizeof( t_uint ) + 1 )
2060
2061/* Bits to keep in the most significant t_uint */
2062#if defined(POLARSSL_HAVE_INT8)
2063#define P521_MASK 0x01
2064#else
2065#define P521_MASK 0x01FF
2066#endif
2067
2068/*
2069 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
2070 * Write N as A1 + 2^521 A0, return A0 + A1
2071 */
2072static int ecp_mod_p521( mpi *N )
2073{
2074 int ret;
2075 size_t i;
2076 mpi M;
2077 t_uint Mp[P521_WIDTH + 1];
2078 /* Worst case for the size of M is when t_uint is 16 bits:
2079 * we need to hold bits 513 to 1056, which is 34 limbs, that is
2080 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
2081
2082 if( N->n < P521_WIDTH )
2083 return( 0 );
2084
2085 /* M = A1 */
2086 M.s = 1;
2087 M.n = N->n - ( P521_WIDTH - 1 );
2088 if( M.n > P521_WIDTH + 1 )
2089 M.n = P521_WIDTH + 1;
2090 M.p = Mp;
2091 memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) );
2092 MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
2093
2094 /* N = A0 */
2095 N->p[P521_WIDTH - 1] &= P521_MASK;
2096 for( i = P521_WIDTH; i < N->n; i++ )
2097 N->p[i] = 0;
2098
2099 /* N = A0 + A1 */
2100 MPI_CHK( mpi_add_abs( N, N, &M ) );
2101
2102cleanup:
2103 return( ret );
2104}
2105
2106#undef P521_WIDTH
2107#undef P521_MASK
2108#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
2109
2110#endif /* POLARSSL_ECP_NIST_OPTIM */
2111
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002112#if defined(POLARSSL_SELF_TEST)
2113
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002114/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002115 * Checkup routine
2116 */
2117int ecp_self_test( int verbose )
2118{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002119 int ret;
2120 size_t i;
2121 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002122 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002123 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002124 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002125 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002126 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002127 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002128 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002129 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002130 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002131 "400000000000000000000000000000000000000000000000", /* one and zeros */
2132 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2133 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002134 };
2135
2136 ecp_group_init( &grp );
2137 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002138 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002139 mpi_init( &m );
2140
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002141 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002142#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002143 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002144#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002145 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
2146#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002147
2148 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002149 printf( " ECP test #1 (constant op_count, base point G): " );
2150
2151 /* Do a dummy multiplication first to trigger precomputation */
2152 MPI_CHK( mpi_lset( &m, 2 ) );
2153 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002154
2155 add_count = 0;
2156 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002157 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002158 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002159 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002160
2161 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2162 {
2163 add_c_prev = add_count;
2164 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002165 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002166 add_count = 0;
2167 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002168 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002169
2170 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002171 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002172
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002173 if( add_count != add_c_prev ||
2174 dbl_count != dbl_c_prev ||
2175 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002176 {
2177 if( verbose != 0 )
2178 printf( "failed (%zu)\n", i );
2179
2180 ret = 1;
2181 goto cleanup;
2182 }
2183 }
2184
2185 if( verbose != 0 )
2186 printf( "passed\n" );
2187
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002188 if( verbose != 0 )
2189 printf( " ECP test #2 (constant op_count, other point): " );
2190 /* We computed P = 2G last time, use it */
2191
2192 add_count = 0;
2193 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002194 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002195 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2196 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2197
2198 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2199 {
2200 add_c_prev = add_count;
2201 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002202 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002203 add_count = 0;
2204 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002205 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002206
2207 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2208 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2209
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002210 if( add_count != add_c_prev ||
2211 dbl_count != dbl_c_prev ||
2212 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002213 {
2214 if( verbose != 0 )
2215 printf( "failed (%zu)\n", i );
2216
2217 ret = 1;
2218 goto cleanup;
2219 }
2220 }
2221
2222 if( verbose != 0 )
2223 printf( "passed\n" );
2224
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002225cleanup:
2226
2227 if( ret < 0 && verbose != 0 )
2228 printf( "Unexpected error, return code = %08X\n", ret );
2229
2230 ecp_group_free( &grp );
2231 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002232 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002233 mpi_free( &m );
2234
2235 if( verbose != 0 )
2236 printf( "\n" );
2237
2238 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002239}
2240
2241#endif
2242
2243#endif