blob: f82624262261d9c309fd41c478130223f6133069 [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
Paul Bakker6a6087e2013-10-28 18:53:08 +010061#if defined(_MSC_VER) && !defined(inline)
62#define inline _inline
63#else
64#if defined(__ARMCC_VERSION) && !defined(inline)
65#define inline __inline
66#endif /* __ARMCC_VERSION */
67#endif /*_MSC_VER */
68
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010069#if defined(POLARSSL_SELF_TEST)
70/*
71 * Counts of point addition and doubling operations.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020072 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010073 */
74unsigned long add_count, dbl_count;
75#endif
76
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010077/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020078 * List of supported curves:
79 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020080 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020081 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020082 * - readable name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020083 */
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +020084const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020085{
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020086#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
87 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpool512r1" },
88#endif
89#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
90 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpool384r1" },
91#endif
92#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
93 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpool256r1" },
94#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020095#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020096 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020097#endif
98#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020099 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200100#endif
101#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200102 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200103#endif
104#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200105 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200106#endif
107#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200108 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200109#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200110 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200111};
112
113/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200114 * List of supported curves and associated info
115 */
116const ecp_curve_info *ecp_curve_list( void )
117{
118 return ecp_supported_curves;
119}
120
121/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200122 * Get the curve info for the internal identifer
123 */
124const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
125{
126 const ecp_curve_info *curve_info;
127
128 for( curve_info = ecp_curve_list();
129 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
130 curve_info++ )
131 {
132 if( curve_info->grp_id == grp_id )
133 return( curve_info );
134 }
135
136 return( NULL );
137}
138
139/*
140 * Get the curve info from the TLS identifier
141 */
142const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
143{
144 const ecp_curve_info *curve_info;
145
146 for( curve_info = ecp_curve_list();
147 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
148 curve_info++ )
149 {
150 if( curve_info->tls_id == tls_id )
151 return( curve_info );
152 }
153
154 return( NULL );
155}
156
157/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100158 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100159 */
160void ecp_point_init( ecp_point *pt )
161{
162 if( pt == NULL )
163 return;
164
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100165 mpi_init( &pt->X );
166 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100167 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100168}
169
170/*
171 * Initialize (the components of) a group
172 */
173void ecp_group_init( ecp_group *grp )
174{
175 if( grp == NULL )
176 return;
177
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200178 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100179}
180
181/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200182 * Initialize (the components of) a key pair
183 */
184void ecp_keypair_init( ecp_keypair *key )
185{
186 if ( key == NULL )
187 return;
188
189 ecp_group_init( &key->grp );
190 mpi_init( &key->d );
191 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200192}
193
194/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100195 * Unallocate (the components of) a point
196 */
197void ecp_point_free( ecp_point *pt )
198{
199 if( pt == NULL )
200 return;
201
202 mpi_free( &( pt->X ) );
203 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100204 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100205}
206
207/*
208 * Unallocate (the components of) a group
209 */
210void ecp_group_free( ecp_group *grp )
211{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200212 size_t i;
213
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100214 if( grp == NULL )
215 return;
216
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100217 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200218 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100219 mpi_free( &grp->B );
220 ecp_point_free( &grp->G );
221 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200222
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200223 if( grp->T != NULL )
224 {
225 for( i = 0; i < grp->T_size; i++ )
226 ecp_point_free( &grp->T[i] );
227 polarssl_free( grp->T );
228 }
229
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200230 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100231}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100232
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100233/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200234 * Unallocate (the components of) a key pair
235 */
236void ecp_keypair_free( ecp_keypair *key )
237{
238 if ( key == NULL )
239 return;
240
241 ecp_group_free( &key->grp );
242 mpi_free( &key->d );
243 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200244}
245
246/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200247 * Copy the contents of a point
248 */
249int ecp_copy( ecp_point *P, const ecp_point *Q )
250{
251 int ret;
252
253 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
254 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
255 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
256
257cleanup:
258 return( ret );
259}
260
261/*
262 * Copy the contents of a group object
263 */
264int ecp_group_copy( ecp_group *dst, const ecp_group *src )
265{
266 return ecp_use_known_dp( dst, src->id );
267}
268
269/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100270 * Set point to zero
271 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100272int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100273{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100274 int ret;
275
276 MPI_CHK( mpi_lset( &pt->X , 1 ) );
277 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
278 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
279
280cleanup:
281 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100282}
283
284/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100285 * Tell if a point is zero
286 */
287int ecp_is_zero( ecp_point *pt )
288{
289 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
290}
291
292/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100293 * Import a non-zero point from ASCII strings
294 */
295int ecp_point_read_string( ecp_point *P, int radix,
296 const char *x, const char *y )
297{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100298 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100299
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100300 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
301 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100302 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100303
304cleanup:
305 return( ret );
306}
307
308/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100309 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100310 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100311int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100312 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100313 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100314{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200315 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100316 size_t plen;
317
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100318 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
319 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100320 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100321
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100322 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100323 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100324 */
325 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
326 {
327 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100328 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100329
330 buf[0] = 0x00;
331 *olen = 1;
332
333 return( 0 );
334 }
335
336 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100337
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100338 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
339 {
340 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100341
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100342 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100343 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100344
345 buf[0] = 0x04;
346 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
347 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
348 }
349 else if( format == POLARSSL_ECP_PF_COMPRESSED )
350 {
351 *olen = plen + 1;
352
353 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100354 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100355
356 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
357 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
358 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100359
360cleanup:
361 return( ret );
362}
363
364/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100365 * Import a point from unsigned binary data (SEC1 2.3.4)
366 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100367int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
368 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100369 int ret;
370 size_t plen;
371
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100372 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100373 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100374
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100375 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100376
377 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100378 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100379
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100380 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
381 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
382 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100383
384cleanup:
385 return( ret );
386}
387
388/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100389 * Import a point from a TLS ECPoint record (RFC 4492)
390 * struct {
391 * opaque point <1..2^8-1>;
392 * } ECPoint;
393 */
394int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100395 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100396{
397 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100398 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100399
400 /*
401 * We must have at least two bytes (1 for length, at least of for data)
402 */
403 if( buf_len < 2 )
404 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
405
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100406 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100407 if( data_len < 1 || data_len > buf_len - 1 )
408 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
409
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100410 /*
411 * Save buffer start for read_binary and update buf
412 */
413 buf_start = *buf;
414 *buf += data_len;
415
416 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100417}
418
419/*
420 * Export a point as a TLS ECPoint record (RFC 4492)
421 * struct {
422 * opaque point <1..2^8-1>;
423 * } ECPoint;
424 */
425int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100426 int format, size_t *olen,
427 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100428{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100429 int ret;
430
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100431 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100432 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100433 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100434 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100435 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
436
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100437 if( ( ret = ecp_point_write_binary( grp, pt, format,
438 olen, buf + 1, blen - 1) ) != 0 )
439 return( ret );
440
441 /*
442 * write length to the first byte and update total length
443 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200444 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100445 ++*olen;
446
447 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100448}
449
450/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200451 * Import an ECP group from ASCII strings, general case (A used)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100452 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200453static int ecp_group_read_string_gen( ecp_group *grp, int radix,
454 const char *p, const char *a, const char *b,
455 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100456{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100457 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100458
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200459 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
460 MPI_CHK( mpi_read_string( &grp->A, radix, a ) );
461 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
462 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
463 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100464
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200465 grp->pbits = mpi_msb( &grp->P );
466 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100467
468cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200469 if( ret != 0 )
470 ecp_group_free( grp );
471
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100472 return( ret );
473}
474
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200475/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200476 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200477 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200478int ecp_group_read_string( ecp_group *grp, int radix,
479 const char *p, const char *b,
480 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100481{
482 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100483
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200484 MPI_CHK( ecp_group_read_string_gen( grp, radix, p, "00", b, gx, gy, n ) );
485 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100486
487cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200488 if( ret != 0 )
489 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200490
491 return( ret );
492}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200493
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100494/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100495 * Domain parameters for secp192r1
496 */
497#define SECP192R1_P \
498 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
499#define SECP192R1_B \
500 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
501#define SECP192R1_GX \
502 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
503#define SECP192R1_GY \
504 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
505#define SECP192R1_N \
506 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
507
508/*
509 * Domain parameters for secp224r1
510 */
511#define SECP224R1_P \
512 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
513#define SECP224R1_B \
514 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
515#define SECP224R1_GX \
516 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
517#define SECP224R1_GY \
518 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
519#define SECP224R1_N \
520 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
521
522/*
523 * Domain parameters for secp256r1
524 */
525#define SECP256R1_P \
526 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
527#define SECP256R1_B \
528 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
529#define SECP256R1_GX \
530 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
531#define SECP256R1_GY \
532 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
533#define SECP256R1_N \
534 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
535
536/*
537 * Domain parameters for secp384r1
538 */
539#define SECP384R1_P \
540 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
541 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
542#define SECP384R1_B \
543 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
544 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
545#define SECP384R1_GX \
546 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
547 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
548#define SECP384R1_GY \
549 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
550 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
551#define SECP384R1_N \
552 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
553 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
554
555/*
556 * Domain parameters for secp521r1
557 */
558#define SECP521R1_P \
559 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
560 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
561 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
562#define SECP521R1_B \
563 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
564 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
565 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
566#define SECP521R1_GX \
567 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
568 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
569 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
570#define SECP521R1_GY \
571 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
572 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
573 "3FAD0761353C7086A272C24088BE94769FD16650"
574#define SECP521R1_N \
575 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
576 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
577 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
578
579/*
Manuel Pégourié-Gonnardcec4a532013-10-07 19:52:27 +0200580 * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
581 */
582#define BP256R1_P \
583 "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377"
584#define BP256R1_A \
585 "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9"
586#define BP256R1_B \
587 "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6"
588#define BP256R1_GX \
589 "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262"
590#define BP256R1_GY \
591 "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"
592#define BP256R1_N \
593 "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7"
594
595/*
596 * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
597 */
598#define BP384R1_P \
599 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB711" \
600 "23ACD3A729901D1A71874700133107EC53"
601#define BP384R1_A \
602 "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F9" \
603 "0F8AA5814A503AD4EB04A8C7DD22CE2826"
604#define BP384R1_B \
605 "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62" \
606 "D57CB4390295DBC9943AB78696FA504C11"
607#define BP384R1_GX \
608 "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10" \
609 "E8E826E03436D646AAEF87B2E247D4AF1E"
610#define BP384R1_GY \
611 "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129" \
612 "280E4646217791811142820341263C5315"
613#define BP384R1_N \
614 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425" \
615 "A7CF3AB6AF6B7FC3103B883202E9046565"
616
617/*
618 * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
619 */
620#define BP512R1_P \
621 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
622 "717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"
623#define BP512R1_A \
624 "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863" \
625 "BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA"
626#define BP512R1_B \
627 "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117" \
628 "A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723"
629#define BP512R1_GX \
630 "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D009" \
631 "8EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822"
632#define BP512R1_GY \
633 "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F81" \
634 "11B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"
635#define BP512R1_N \
636 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
637 "70553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"
638
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200639#if defined(POLARSSL_ECP_NIST_OPTIM)
640/* Forward declarations */
641static int ecp_mod_p192( mpi * );
642static int ecp_mod_p224( mpi * );
643static int ecp_mod_p256( mpi * );
644static int ecp_mod_p384( mpi * );
645static int ecp_mod_p521( mpi * );
646#endif
647
Manuel Pégourié-Gonnardcec4a532013-10-07 19:52:27 +0200648/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100649 * Set a group using well-known domain parameters
650 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100651int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100652{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100653 grp->id = id;
654
655 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100656 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200657#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100658 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200659#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100660 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200661#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100662 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100663 SECP192R1_P, SECP192R1_B,
664 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200665#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100666
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200667#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100668 case POLARSSL_ECP_DP_SECP224R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200669#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200670 grp->modp = ecp_mod_p224;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200671#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100672 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100673 SECP224R1_P, SECP224R1_B,
674 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200675#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100676
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200677#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100678 case POLARSSL_ECP_DP_SECP256R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200679#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnardec655c92013-10-23 14:50:39 +0200680 grp->modp = ecp_mod_p256;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200681#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100682 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100683 SECP256R1_P, SECP256R1_B,
684 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200685#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100686
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200687#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100688 case POLARSSL_ECP_DP_SECP384R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200689#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard0f9149c2013-10-23 15:06:37 +0200690 grp->modp = ecp_mod_p384;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200691#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100692 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100693 SECP384R1_P, SECP384R1_B,
694 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200695#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100696
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200697#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100698 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200699#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100700 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200701#endif
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100702 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100703 SECP521R1_P, SECP521R1_B,
704 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200705#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100706
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200707#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
708 case POLARSSL_ECP_DP_BP256R1:
709 return( ecp_group_read_string_gen( grp, 16,
710 BP256R1_P, BP256R1_A, BP256R1_B,
711 BP256R1_GX, BP256R1_GY, BP256R1_N ) );
712#endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
713
714#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
715 case POLARSSL_ECP_DP_BP384R1:
716 return( ecp_group_read_string_gen( grp, 16,
717 BP384R1_P, BP384R1_A, BP384R1_B,
718 BP384R1_GX, BP384R1_GY, BP384R1_N ) );
719#endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
720
721#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
722 case POLARSSL_ECP_DP_BP512R1:
723 return( ecp_group_read_string_gen( grp, 16,
724 BP512R1_P, BP512R1_A, BP512R1_B,
725 BP512R1_GX, BP512R1_GY, BP512R1_N ) );
726#endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
727
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200728 default:
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200729 ecp_group_free( grp );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200730 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
731 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100732}
733
734/*
735 * Set a group from an ECParameters record (RFC 4492)
736 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100737int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100738{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200739 uint16_t tls_id;
740 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100741
742 /*
743 * We expect at least three bytes (see below)
744 */
745 if( len < 3 )
746 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
747
748 /*
749 * First byte is curve_type; only named_curve is handled
750 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100751 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100752 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
753
754 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100755 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100756 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200757 tls_id = *(*buf)++;
758 tls_id <<= 8;
759 tls_id |= *(*buf)++;
760
761 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
762 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
763
764 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100765}
766
767/*
768 * Write the ECParameters record corresponding to a group (RFC 4492)
769 */
770int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
771 unsigned char *buf, size_t blen )
772{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200773 const ecp_curve_info *curve_info;
774
775 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
776 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200777
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100778 /*
779 * We are going to write 3 bytes (see below)
780 */
781 *olen = 3;
782 if( blen < *olen )
783 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
784
785 /*
786 * First byte is curve_type, always named_curve
787 */
788 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
789
790 /*
791 * Next two bytes are the namedcurve value
792 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200793 buf[0] = curve_info->tls_id >> 8;
794 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100795
796 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100797}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100798
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200799/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200800 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
801 * See the documentation of struct ecp_group.
802 *
803 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200804 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200805static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200806{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200807 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200808
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200809 if( grp->modp == NULL )
810 return( mpi_mod_mpi( N, N, &grp->P ) );
811
812 /* N->s < 0 is a much faster test, which fails only if N is 0 */
813 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
814 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200815 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200816 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200817 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200818
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200819 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200820
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200821 /* N->s < 0 is a much faster test, which fails only if N is 0 */
822 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
823 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200824
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200825 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
826 /* we known P, N and the result are positive */
827 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200828
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200829cleanup:
830 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200831}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200832
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100833/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100834 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100835 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100836 * In order to guarantee that, we need to ensure that operands of
837 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100838 * bring the result back to this range.
839 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100840 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100841 */
842
843/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100844 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
845 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100846#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100847
848/*
849 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200850 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100851 */
852#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200853 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100854 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
855
856/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200857 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
858 * We known P, N and the result are positive, so sub_abs is correct, and
859 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100860 */
861#define MOD_ADD( N ) \
862 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200863 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100864
865/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100866 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100867 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100868 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100869static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100870{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100871 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100872 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100873
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100874 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100875 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100876
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100877 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100878
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100879 /*
880 * X = X / Z^2 mod p
881 */
882 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
883 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
884 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100885
886 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100887 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100888 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100889 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
890 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100891
892 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100893 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100894 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100895 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100896
897cleanup:
898
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100899 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100900
901 return( ret );
902}
903
904/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100905 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100906 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100907 * (See for example Cohen's "A Course in Computational Algebraic Number
908 * Theory", Algorithm 10.3.4.)
909 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200910 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100911 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100912 *
913 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100914 */
915static int ecp_normalize_many( const ecp_group *grp,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100916 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100917{
918 int ret;
919 size_t i;
920 mpi *c, u, Zi, ZZi;
921
922 if( t_len < 2 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100923 return( ecp_normalize( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100924
Paul Bakker6e339b52013-07-03 13:37:05 +0200925 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200926 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100927
928 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
929 for( i = 0; i < t_len; i++ )
930 mpi_init( &c[i] );
931
932 /*
933 * c[i] = Z_0 * ... * Z_i
934 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100935 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100936 for( i = 1; i < t_len; i++ )
937 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100938 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100939 MOD_MUL( c[i] );
940 }
941
942 /*
943 * u = 1 / (Z_0 * ... * Z_n) mod P
944 */
945 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
946
947 for( i = t_len - 1; ; i-- )
948 {
949 /*
950 * Zi = 1 / Z_i mod p
951 * u = 1 / (Z_0 * ... * Z_i) mod P
952 */
953 if( i == 0 ) {
954 MPI_CHK( mpi_copy( &Zi, &u ) );
955 }
956 else
957 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100958 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
959 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100960 }
961
962 /*
963 * proceed as in normalize()
964 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100965 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
966 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
967 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
968 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
969 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100970
971 if( i == 0 )
972 break;
973 }
974
975cleanup:
976
977 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
978 for( i = 0; i < t_len; i++ )
979 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200980 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100981
982 return( ret );
983}
984
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100985/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200986 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200987 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200988 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200989 * with heavy variable renaming, some reordering and one minor modification
990 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
991 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100992 *
993 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200994 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200995static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
996 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200997{
998 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200999 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001000
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001001#if defined(POLARSSL_SELF_TEST)
1002 dbl_count++;
1003#endif
1004
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001005 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
1006 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001007
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001008 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
1009 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
1010 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
1011 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
1012 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
1013 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
1014 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
1015 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
1016 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
1017 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
1018 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
1019 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
1020 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
1021 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
1022 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1023 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1024 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
1025 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
1026 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
1027 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
1028 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
1029 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
1030 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
1031 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001032
1033 MPI_CHK( mpi_copy( &R->X, &X3 ) );
1034 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
1035 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
1036
1037cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001038 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
1039 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001040
1041 return( ret );
1042}
1043
1044/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001045 * Addition or subtraction: R = P + Q or R = P - Q,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001046 * mixed affine-Jacobian coordinates (GECC 3.22)
1047 *
1048 * The coordinates of Q must be normalized (= affine),
1049 * but those of P don't need to. R is not normalized.
1050 *
1051 * If sign >= 0, perform addition, otherwise perform subtraction,
1052 * taking advantage of the fact that, for Q != 0, we have
1053 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001054 *
1055 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001056 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001057static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001058 const ecp_point *P, const ecp_point *Q,
1059 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001060{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001061 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001062 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001063
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001064#if defined(POLARSSL_SELF_TEST)
1065 add_count++;
1066#endif
1067
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001068 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001069 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001070 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001071 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001072 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1073 return( ecp_copy( R, P ) );
1074
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001075 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1076 {
1077 ret = ecp_copy( R, Q );
1078
1079 /*
1080 * -R.Y mod P = P - R.Y unless R.Y == 0
1081 */
1082 if( ret == 0 && sign < 0)
1083 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1084 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1085
1086 return( ret );
1087 }
1088
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001089 /*
1090 * Make sure Q coordinates are normalized
1091 */
1092 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001093 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001094
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001095 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1096 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001097
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001098 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1099 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1100 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1101 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001102
1103 /*
1104 * For subtraction, -Q.Y should have been used instead of Q.Y,
1105 * so we replace T2 by -T2, which is P - T2 mod P
1106 */
1107 if( sign < 0 )
1108 {
1109 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1110 MOD_SUB( T2 );
1111 }
1112
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001113 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1114 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001115
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001116 if( mpi_cmp_int( &T1, 0 ) == 0 )
1117 {
1118 if( mpi_cmp_int( &T2, 0 ) == 0 )
1119 {
1120 ret = ecp_double_jac( grp, R, P );
1121 goto cleanup;
1122 }
1123 else
1124 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001125 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001126 goto cleanup;
1127 }
1128 }
1129
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001130 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1131 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1132 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1133 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1134 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1135 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1136 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1137 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1138 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1139 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1140 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1141 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001142
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001143 MPI_CHK( mpi_copy( &R->X, &X ) );
1144 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1145 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001146
1147cleanup:
1148
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001149 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1150 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001151
1152 return( ret );
1153}
1154
1155/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001156 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001157 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001158 */
1159int ecp_add( const ecp_group *grp, ecp_point *R,
1160 const ecp_point *P, const ecp_point *Q )
1161{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001162 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001163
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001164 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1165 MPI_CHK( ecp_normalize( grp, R ) );
1166
1167cleanup:
1168 return( ret );
1169}
1170
1171/*
1172 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001173 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001174 */
1175int ecp_sub( const ecp_group *grp, ecp_point *R,
1176 const ecp_point *P, const ecp_point *Q )
1177{
1178 int ret;
1179
1180 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001181 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001182
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001183cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001184 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001185}
1186
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001187/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001188 * Randomize jacobian coordinates:
1189 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1190 * This is sort of the reverse operation of ecp_normalize().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001191 *
1192 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001193 */
1194static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1195 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1196{
1197 int ret;
1198 mpi l, ll;
1199 size_t p_size = (grp->pbits + 7) / 8;
1200 int count = 0;
1201
1202 mpi_init( &l ); mpi_init( &ll );
1203
1204 /* Generate l such that 1 < l < p */
1205 do
1206 {
1207 mpi_fill_random( &l, p_size, f_rng, p_rng );
1208
1209 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1210 mpi_shift_r( &l, 1 );
1211
1212 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001213 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001214 }
1215 while( mpi_cmp_int( &l, 1 ) <= 0 );
1216
1217 /* Z = l * Z */
1218 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1219
1220 /* X = l^2 * X */
1221 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1222 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1223
1224 /* Y = l^3 * Y */
1225 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1226 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1227
1228cleanup:
1229 mpi_free( &l ); mpi_free( &ll );
1230
1231 return( ret );
1232}
1233
1234/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001235 * Check and define parameters used by the comb method (see below for details)
1236 */
1237#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1238#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1239#endif
1240
1241/* d = ceil( n / w ) */
1242#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1243
1244/* number of precomputed points */
1245#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1246
1247/*
1248 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001249 *
1250 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001251 * modified version that provides resistance to SPA by avoiding zero
1252 * digits in the representation as in [3]. We modify the method further by
1253 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001254 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001255 *
1256 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1257 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1258 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001259 *
1260 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001261 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001262 * - w is the size, ie number of teeth, of the comb, and must be between
1263 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001264 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1265 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001266 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001267static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001268 unsigned char w, const mpi *m )
1269{
1270 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001271 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001272
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001273 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001274
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001275 /* First get the classical comb values (except for x_d = 0) */
1276 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001277 for( j = 0; j < w; j++ )
1278 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1279
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001280 /* Now make sure x_1 .. x_d are odd */
1281 c = 0;
1282 for( i = 1; i <= d; i++ )
1283 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001284 /* Add carry and update it */
1285 cc = x[i] & c;
1286 x[i] = x[i] ^ c;
1287 c = cc;
1288
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001289 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001290 adjust = 1 - ( x[i] & 0x01 );
1291 c |= x[i] & ( x[i-1] * adjust );
1292 x[i] = x[i] ^ ( x[i-1] * adjust );
1293 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001294 }
1295}
1296
1297/*
1298 * Precompute points for the comb method
1299 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001300 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1301 * 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 +01001302 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001303 * T must be able to hold 2^{w - 1} elements
1304 *
1305 * 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 +01001306 */
1307static int ecp_precompute_comb( const ecp_group *grp,
1308 ecp_point T[], const ecp_point *P,
1309 unsigned char w, size_t d )
1310{
1311 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001312 unsigned char i, k;
1313 size_t j;
1314 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001315
1316 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001317 * Set T[0] = P and
1318 * 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 +01001319 */
1320 MPI_CHK( ecp_copy( &T[0], P ) );
1321
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001322 k = 0;
1323 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001324 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001325 cur = T + i;
1326 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001327 for( j = 0; j < d; j++ )
1328 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001329
1330 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001331 }
1332
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001333 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001334
1335 /*
1336 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001337 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001338 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001339 k = 0;
1340 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001341 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001342 j = i;
1343 while( j-- )
1344 {
1345 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i], +1 );
1346 TT[k++] = &T[i + j];
1347 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001348 }
1349
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001350 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001351
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001352 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001353 * Post-precessing: reclaim some memory by
1354 * - not storing Z (always 1)
1355 * - shrinking other coordinates
1356 * However keep the same number of limbs as P, which will be useful in
1357 * ecp_select_comb()
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001358 */
1359 for( i = 0; i < ( 1U << (w-1) ); i++ )
1360 {
1361 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001362 mpi_shrink( &T[i].X, grp->P.n );
1363 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001364 }
1365
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001366cleanup:
1367 return( ret );
1368}
1369
1370/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001371 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001372 */
1373static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001374 const ecp_point T[], unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001375{
1376 int ret;
1377
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001378 /* Ignore the "sign" bit */
1379 MPI_CHK( ecp_copy( R, &T[ ( i & 0x7Fu ) >> 1 ] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001380
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001381 /* Restore the Z coordinate */
1382 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1383
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001384 /*
1385 * -R = (R.X, -R.Y, R.Z), and
1386 * -R.Y mod P = P - R.Y unless R.Y == 0
1387 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001388 if( ( i & 0x80 ) != 0 )
1389 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1390 MPI_CHK( mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001391
1392cleanup:
1393 return( ret );
1394}
1395
1396/*
1397 * Core multiplication algorithm for the (modified) comb method.
1398 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001399 *
1400 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001401 */
1402static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001403 const ecp_point T[],
1404 const unsigned char x[], size_t d,
1405 int (*f_rng)(void *, unsigned char *, size_t),
1406 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001407{
1408 int ret;
1409 ecp_point Txi;
1410 size_t i;
1411
1412 ecp_point_init( &Txi );
1413
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001414 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001415 i = d;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001416 MPI_CHK( ecp_select_comb( grp, R, T, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001417 if( f_rng != 0 )
1418 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001419
1420 while( i-- != 0 )
1421 {
1422 MPI_CHK( ecp_double_jac( grp, R, R ) );
1423 MPI_CHK( ecp_select_comb( grp, &Txi, T, x[i] ) );
1424 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi, +1 ) );
1425 }
1426
1427cleanup:
1428 ecp_point_free( &Txi );
1429
1430 return( ret );
1431}
1432
1433/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001434 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001435 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001436int ecp_mul( ecp_group *grp, ecp_point *R,
1437 const mpi *m, const ecp_point *P,
1438 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001439{
1440 int ret;
1441 unsigned char w, m_is_odd, p_eq_g;
1442 size_t pre_len, d, i;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001443 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001444 ecp_point Q, *T = NULL, S[2];
1445 mpi M;
1446
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001447 /*
1448 * Sanity checks (before we even initialize anything)
1449 */
1450 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1451 return( ret );
1452
1453 /* We'll need this later, but do it now to possibly avoid cheking P */
1454 p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 &&
1455 mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1456 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
1457 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1458 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001459
1460 mpi_init( &M );
1461 ecp_point_init( &Q );
1462 ecp_point_init( &S[0] );
1463 ecp_point_init( &S[1] );
1464
1465 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001466 * Minimize the number of multiplications, that is minimize
1467 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w
1468 * (see costs of the various parts, with 1S = 1M)
1469 */
1470 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001471
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001472 /*
1473 * If P == G, pre-compute a bit more, since this may be re-used later.
1474 * Just adding one ups the cost of the first mul by at most 3%.
1475 */
1476 if( p_eq_g )
1477 w++;
1478
1479 /*
1480 * Make sure w is within limits.
1481 * (The last test is useful only for very small curves in the test suite.)
1482 */
1483 if( w > POLARSSL_ECP_WINDOW_SIZE )
1484 w = POLARSSL_ECP_WINDOW_SIZE;
1485 if( w < 2 || w >= grp->nbits )
1486 w = 2;
1487
1488 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001489 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001490 d = ( grp->nbits + w - 1 ) / w;
1491
1492 /*
1493 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001494 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001495 */
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001496 if( p_eq_g )
1497 T = grp->T;
1498
1499 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001500 {
1501 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1502 if( T == NULL )
1503 {
1504 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1505 goto cleanup;
1506 }
1507
1508 for( i = 0; i < pre_len; i++ )
1509 ecp_point_init( &T[i] );
1510
1511 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1512
1513 if( p_eq_g )
1514 {
1515 grp->T = T;
1516 grp->T_size = pre_len;
1517 }
1518 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001519
1520 /*
1521 * Make sure M is odd (M = m + 1 or M = m + 2)
1522 * later we'll get m * P by subtracting P or 2 * P to M * P.
1523 */
1524 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1525
1526 MPI_CHK( mpi_copy( &M, m ) );
1527 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
1528
1529 /*
1530 * Go for comb multiplication, Q = M * P
1531 */
1532 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001533 ecp_mul_comb_core( grp, &Q, T, k, d, f_rng, p_rng );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001534
1535 /*
1536 * Now get m * P from M * P
1537 */
1538 MPI_CHK( ecp_copy( &S[0], P ) );
1539 MPI_CHK( ecp_add( grp, &S[1], P, P ) );
1540 MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) );
1541
1542cleanup:
1543
1544 if( T != NULL && ! p_eq_g )
1545 {
1546 for( i = 0; i < pre_len; i++ )
1547 ecp_point_free( &T[i] );
1548 polarssl_free( T );
1549 }
1550
1551 ecp_point_free( &S[1] );
1552 ecp_point_free( &S[0] );
1553 ecp_point_free( &Q );
1554 mpi_free( &M );
1555
1556 return( ret );
1557}
1558
1559/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001560 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1561 */
1562int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1563{
1564 int ret;
1565 mpi YY, RHS;
1566
1567 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001568 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001569
1570 /*
1571 * pt coordinates must be normalized for our checks
1572 */
1573 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001574 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001575
1576 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1577 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1578 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1579 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001580 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001581
1582 mpi_init( &YY ); mpi_init( &RHS );
1583
1584 /*
1585 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001586 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001587 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001588 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1589 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001590 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001591 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1592 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001593
1594 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001595 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001596
1597cleanup:
1598
1599 mpi_free( &YY ); mpi_free( &RHS );
1600
1601 return( ret );
1602}
1603
1604/*
1605 * Check that an mpi is valid as a private key (SEC1 3.2)
1606 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001607int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001608{
1609 /* We want 1 <= d <= N-1 */
1610 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001611 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001612
1613 return( 0 );
1614}
1615
1616/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001617 * Generate a keypair (SEC1 3.2.1)
1618 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001619int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001620 int (*f_rng)(void *, unsigned char *, size_t),
1621 void *p_rng )
1622{
1623 int count = 0;
1624 size_t n_size = (grp->nbits + 7) / 8;
1625
1626 /*
1627 * Generate d such that 1 <= n < N
1628 */
1629 do
1630 {
1631 mpi_fill_random( d, n_size, f_rng, p_rng );
1632
1633 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1634 mpi_shift_r( d, 1 );
1635
1636 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001637 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001638 }
1639 while( mpi_cmp_int( d, 1 ) < 0 );
1640
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001641 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001642}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001643
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001644#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard9fcceac2013-10-23 20:56:12 +02001645/*
1646 * Fast reduction modulo the primes used by the NIST curves.
1647 *
1648 * These functions are: critical for speed, but not need for correct
1649 * operations. So, we make the choice to heavily rely on the internals of our
1650 * bignum library, which creates a tight coupling between these functions and
1651 * our MPI implementation. However, the coupling between the ECP module and
1652 * MPI remains loose, since these functions can be deactivated at will.
1653 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001654
1655#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1656/*
1657 * Compared to the way things are presented in FIPS 186-3 D.2,
1658 * we proceed in columns, from right (least significant chunk) to left,
1659 * adding chunks to N in place, and keeping a carry for the next chunk.
1660 * This avoids moving things around in memory, and uselessly adding zeros,
1661 * compared to the more straightforward, line-oriented approach.
1662 *
1663 * For this prime we need to handle data in chunks of 64 bits.
1664 * Since this is always a multiple of our basic t_uint, we can
1665 * use a t_uint * to designate such a chunk, and small loops to handle it.
1666 */
1667
1668/* Add 64-bit chunks (dst += src) and update carry */
1669static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
1670{
1671 unsigned char i;
1672 t_uint c = 0;
1673 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
1674 {
1675 *dst += c; c = ( *dst < c );
1676 *dst += *src; c += ( *dst < *src );
1677 }
1678 *carry += c;
1679}
1680
1681/* Add carry to a 64-bit chunk and update carry */
1682static inline void carry64( t_uint *dst, t_uint *carry )
1683{
1684 unsigned char i;
1685 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
1686 {
1687 *dst += *carry;
1688 *carry = ( *dst < *carry );
1689 }
1690}
1691
1692#define WIDTH 8 / sizeof( t_uint )
1693#define A( i ) N->p + i * WIDTH
1694#define ADD( i ) add64( p, A( i ), &c )
1695#define NEXT p += WIDTH; carry64( p, &c )
1696#define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
1697
1698/*
1699 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
1700 */
1701static int ecp_mod_p192( mpi *N )
1702{
1703 int ret;
1704 t_uint c = 0;
1705 t_uint *p, *end;
1706
1707 /* Make sure we have enough blocks so that A(5) is legal */
1708 MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
1709
1710 p = N->p;
1711 end = p + N->n;
1712
1713 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
1714 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
1715 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
1716
1717cleanup:
1718 return( ret );
1719}
1720
1721#undef WIDTH
1722#undef A
1723#undef ADD
1724#undef NEXT
1725#undef LAST
1726#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
1727
1728#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
1729 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
1730 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1731/*
1732 * The reader is advised to first understand ecp_mod_p192() since the same
1733 * general structure is used here, but with additional complications:
1734 * (1) chunks of 32 bits, and (2) subtractions.
1735 */
1736
1737/*
1738 * For these primes, we need to handle data in chunks of 32 bits.
1739 * This makes it more complicated if we use 64 bits limbs in MPI,
1740 * which prevents us from using a uniform access method as for p192.
1741 *
1742 * So, we define a mini abstraction layer to access 32 bit chunks,
1743 * load them in 'cur' for work, and store them back from 'cur' when done.
1744 *
1745 * While at it, also define the size of N in terms of 32-bit chunks.
1746 */
1747#define LOAD32 cur = A( i );
1748
1749#if defined(POLARSSL_HAVE_INT8) /* 8 bit */
1750
1751#define MAX32 N->n / 4
1752#define A( j ) (uint32_t)( N->p[4*j+0] ) | \
1753 ( N->p[4*j+1] << 8 ) | \
1754 ( N->p[4*j+2] << 16 ) | \
1755 ( N->p[4*j+3] << 24 )
1756#define STORE32 N->p[4*i+0] = (uint8_t)( cur ); \
1757 N->p[4*i+1] = (uint8_t)( cur >> 8 ); \
1758 N->p[4*i+2] = (uint8_t)( cur >> 16 ); \
1759 N->p[4*i+3] = (uint8_t)( cur >> 24 );
1760
1761#elif defined(POLARSSL_HAVE_INT16) /* 16 bit */
1762
1763#define MAX32 N->n / 2
1764#define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
1765#define STORE32 N->p[2*i+0] = (uint16_t)( cur ); \
1766 N->p[2*i+1] = (uint16_t)( cur >> 16 );
1767
1768#elif defined(POLARSSL_HAVE_INT32) /* 32 bit */
1769
1770#define MAX32 N->n
1771#define A( j ) N->p[j]
1772#define STORE32 N->p[i] = cur;
1773
1774#else /* 64-bit */
1775
1776#define MAX32 N->n * 2
1777#define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
1778#define STORE32 \
1779 if( i % 2 ) { \
1780 N->p[i/2] &= 0x00000000FFFFFFFF; \
1781 N->p[i/2] |= ((uint64_t) cur) << 32; \
1782 } else { \
1783 N->p[i/2] &= 0xFFFFFFFF00000000; \
1784 N->p[i/2] |= (uint64_t) cur; \
1785 }
1786
1787#endif /* sizeof( t_uint ) */
1788
1789/*
1790 * Helpers for addition and subtraction of chunks, with signed carry.
1791 */
1792static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
1793{
1794 *dst += src;
1795 *carry += ( *dst < src );
1796}
1797
1798static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
1799{
1800 *carry -= ( *dst < src );
1801 *dst -= src;
1802}
1803
1804#define ADD( j ) add32( &cur, A( j ), &c );
1805#define SUB( j ) sub32( &cur, A( j ), &c );
1806
1807/*
1808 * Helpers for the main 'loop'
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001809 * (see fix_negative for the motivation of C)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001810 */
1811#define INIT( b ) \
1812 int ret; \
1813 signed char c = 0, cc; \
1814 uint32_t cur; \
1815 size_t i = 0, bits = b; \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001816 mpi C; \
1817 t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \
1818 \
1819 C.s = 1; \
1820 C.n = b / 8 / sizeof( t_uint) + 1; \
1821 C.p = Cp; \
1822 memset( Cp, 0, C.n * sizeof( t_uint ) ); \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001823 \
1824 MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
1825 LOAD32;
1826
1827#define NEXT \
1828 STORE32; i++; LOAD32; \
1829 cc = c; c = 0; \
1830 if( cc < 0 ) \
1831 sub32( &cur, -cc, &c ); \
1832 else \
1833 add32( &cur, cc, &c ); \
1834
1835#define LAST \
1836 STORE32; i++; \
1837 cur = c > 0 ? c : 0; STORE32; \
1838 cur = 0; while( ++i < MAX32 ) { STORE32; } \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001839 if( c < 0 ) fix_negative( N, c, &C, bits );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001840
1841/*
1842 * If the result is negative, we get it in the form
1843 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
1844 */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001845static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001846{
1847 int ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001848
1849 /* C = - c * 2^(bits + 32) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001850#if !defined(POLARSSL_HAVE_INT64)
1851 ((void) bits);
1852#else
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001853 if( bits == 224 )
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001854 C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001855 else
1856#endif
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001857 C->p[ C->n - 1 ] = (t_uint) -c;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001858
1859 /* N = - ( C - N ) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001860 MPI_CHK( mpi_sub_abs( N, C, N ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001861 N->s = -1;
1862
1863cleanup:
1864
1865 return( ret );
1866}
1867
1868#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1869/*
1870 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
1871 */
1872static int ecp_mod_p224( mpi *N )
1873{
1874 INIT( 224 );
1875
1876 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
1877 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
1878 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
1879 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
1880 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
1881 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
1882 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
1883
1884cleanup:
1885 return( ret );
1886}
1887#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1888
1889#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1890/*
1891 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
1892 */
1893static int ecp_mod_p256( mpi *N )
1894{
1895 INIT( 256 );
1896
1897 ADD( 8 ); ADD( 9 );
1898 SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0
1899
1900 ADD( 9 ); ADD( 10 );
1901 SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1
1902
1903 ADD( 10 ); ADD( 11 );
1904 SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2
1905
1906 ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
1907 SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3
1908
1909 ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
1910 SUB( 9 ); SUB( 10 ); NEXT; // A4
1911
1912 ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
1913 SUB( 10 ); SUB( 11 ); NEXT; // A5
1914
1915 ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
1916 SUB( 8 ); SUB( 9 ); NEXT; // A6
1917
1918 ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
1919 SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7
1920
1921cleanup:
1922 return( ret );
1923}
1924#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1925
1926#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1927/*
1928 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
1929 */
1930static int ecp_mod_p384( mpi *N )
1931{
1932 INIT( 384 );
1933
1934 ADD( 12 ); ADD( 21 ); ADD( 20 );
1935 SUB( 23 ); NEXT; // A0
1936
1937 ADD( 13 ); ADD( 22 ); ADD( 23 );
1938 SUB( 12 ); SUB( 20 ); NEXT; // A2
1939
1940 ADD( 14 ); ADD( 23 );
1941 SUB( 13 ); SUB( 21 ); NEXT; // A2
1942
1943 ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
1944 SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3
1945
1946 ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
1947 SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4
1948
1949 ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
1950 SUB( 16 ); NEXT; // A5
1951
1952 ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
1953 SUB( 17 ); NEXT; // A6
1954
1955 ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
1956 SUB( 18 ); NEXT; // A7
1957
1958 ADD( 20 ); ADD( 17 ); ADD( 16 );
1959 SUB( 19 ); NEXT; // A8
1960
1961 ADD( 21 ); ADD( 18 ); ADD( 17 );
1962 SUB( 20 ); NEXT; // A9
1963
1964 ADD( 22 ); ADD( 19 ); ADD( 18 );
1965 SUB( 21 ); NEXT; // A10
1966
1967 ADD( 23 ); ADD( 20 ); ADD( 19 );
1968 SUB( 22 ); LAST; // A11
1969
1970cleanup:
1971 return( ret );
1972}
1973#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1974
1975#undef A
1976#undef LOAD32
1977#undef STORE32
1978#undef MAX32
1979#undef INIT
1980#undef NEXT
1981#undef LAST
1982
1983#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
1984 POLARSSL_ECP_DP_SECP256R1_ENABLED ||
1985 POLARSSL_ECP_DP_SECP384R1_ENABLED */
1986
1987#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1988/*
1989 * Here we have an actual Mersenne prime, so things are more straightforward.
1990 * However, chunks are aligned on a 'weird' boundary (521 bits).
1991 */
1992
1993/* Size of p521 in terms of t_uint */
1994#define P521_WIDTH ( 521 / 8 / sizeof( t_uint ) + 1 )
1995
1996/* Bits to keep in the most significant t_uint */
1997#if defined(POLARSSL_HAVE_INT8)
1998#define P521_MASK 0x01
1999#else
2000#define P521_MASK 0x01FF
2001#endif
2002
2003/*
2004 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
2005 * Write N as A1 + 2^521 A0, return A0 + A1
2006 */
2007static int ecp_mod_p521( mpi *N )
2008{
2009 int ret;
2010 size_t i;
2011 mpi M;
2012 t_uint Mp[P521_WIDTH + 1];
2013 /* Worst case for the size of M is when t_uint is 16 bits:
2014 * we need to hold bits 513 to 1056, which is 34 limbs, that is
2015 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
2016
2017 if( N->n < P521_WIDTH )
2018 return( 0 );
2019
2020 /* M = A1 */
2021 M.s = 1;
2022 M.n = N->n - ( P521_WIDTH - 1 );
2023 if( M.n > P521_WIDTH + 1 )
2024 M.n = P521_WIDTH + 1;
2025 M.p = Mp;
2026 memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) );
2027 MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
2028
2029 /* N = A0 */
2030 N->p[P521_WIDTH - 1] &= P521_MASK;
2031 for( i = P521_WIDTH; i < N->n; i++ )
2032 N->p[i] = 0;
2033
2034 /* N = A0 + A1 */
2035 MPI_CHK( mpi_add_abs( N, N, &M ) );
2036
2037cleanup:
2038 return( ret );
2039}
2040
2041#undef P521_WIDTH
2042#undef P521_MASK
2043#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
2044
2045#endif /* POLARSSL_ECP_NIST_OPTIM */
2046
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002047#if defined(POLARSSL_SELF_TEST)
2048
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002049/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002050 * Checkup routine
2051 */
2052int ecp_self_test( int verbose )
2053{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002054 int ret;
2055 size_t i;
2056 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002057 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002058 mpi m;
2059 unsigned long add_c_prev, dbl_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002060 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002061 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002062 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002063 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002064 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002065 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002066 "400000000000000000000000000000000000000000000000", /* one and zeros */
2067 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2068 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002069 };
2070
2071 ecp_group_init( &grp );
2072 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002073 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002074 mpi_init( &m );
2075
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002076 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002077#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002078 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002079#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002080 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
2081#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002082
2083 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002084 printf( " ECP test #1 (constant op_count, base point G): " );
2085
2086 /* Do a dummy multiplication first to trigger precomputation */
2087 MPI_CHK( mpi_lset( &m, 2 ) );
2088 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002089
2090 add_count = 0;
2091 dbl_count = 0;
2092 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002093 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002094
2095 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2096 {
2097 add_c_prev = add_count;
2098 dbl_c_prev = dbl_count;
2099 add_count = 0;
2100 dbl_count = 0;
2101
2102 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002103 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002104
2105 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2106 {
2107 if( verbose != 0 )
2108 printf( "failed (%zu)\n", i );
2109
2110 ret = 1;
2111 goto cleanup;
2112 }
2113 }
2114
2115 if( verbose != 0 )
2116 printf( "passed\n" );
2117
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002118 if( verbose != 0 )
2119 printf( " ECP test #2 (constant op_count, other point): " );
2120 /* We computed P = 2G last time, use it */
2121
2122 add_count = 0;
2123 dbl_count = 0;
2124 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2125 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2126
2127 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2128 {
2129 add_c_prev = add_count;
2130 dbl_c_prev = dbl_count;
2131 add_count = 0;
2132 dbl_count = 0;
2133
2134 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2135 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2136
2137 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2138 {
2139 if( verbose != 0 )
2140 printf( "failed (%zu)\n", i );
2141
2142 ret = 1;
2143 goto cleanup;
2144 }
2145 }
2146
2147 if( verbose != 0 )
2148 printf( "passed\n" );
2149
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002150cleanup:
2151
2152 if( ret < 0 && verbose != 0 )
2153 printf( "Unexpected error, return code = %08X\n", ret );
2154
2155 ecp_group_free( &grp );
2156 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002157 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002158 mpi_free( &m );
2159
2160 if( verbose != 0 )
2161 printf( "\n" );
2162
2163 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002164}
2165
2166#endif
2167
2168#endif