blob: 756beba15bfa09d3c34e24369754772116b3aaa8 [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é-Gonnard01fca5e2013-11-21 17:47:12 +0100986 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
987 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
988 */
989static int ecp_safe_invert( const ecp_group *grp,
990 ecp_point *Q,
991 unsigned char inv )
992{
993 int ret;
994 unsigned char nonzero;
995 mpi mQY;
996
997 mpi_init( &mQY );
998
999 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
1000 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1001 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
1002 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
1003
1004cleanup:
1005 mpi_free( &mQY );
1006
1007 return( ret );
1008}
1009
1010/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001011 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001012 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001013 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001014 * with heavy variable renaming, some reordering and one minor modification
1015 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
1016 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001017 *
1018 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001019 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001020static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
1021 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001022{
1023 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001024 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001025
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001026#if defined(POLARSSL_SELF_TEST)
1027 dbl_count++;
1028#endif
1029
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001030 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
1031 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001032
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001033 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
1034 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
1035 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
1036 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
1037 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
1038 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
1039 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
1040 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
1041 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
1042 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
1043 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
1044 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
1045 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
1046 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
1047 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1048 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1049 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
1050 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
1051 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
1052 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
1053 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
1054 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
1055 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
1056 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001057
1058 MPI_CHK( mpi_copy( &R->X, &X3 ) );
1059 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
1060 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
1061
1062cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001063 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
1064 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001065
1066 return( ret );
1067}
1068
1069/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001070 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001071 *
1072 * The coordinates of Q must be normalized (= affine),
1073 * but those of P don't need to. R is not normalized.
1074 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001075 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
1076 * None of these cases can happen as intermediate step in ecp_mul():
1077 * - at each step, P, Q and R are multiples of the base point, the factor
1078 * being less than its order, so none of them is zero;
1079 * - Q is an odd multiple of the base point, P an even multiple,
1080 * due to the choice of precomputed points in the modified comb method.
1081 * So branches for these cases do not leak secret information.
1082 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001083 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001084 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001085static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001086 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001087{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001088 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001089 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001090
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001091#if defined(POLARSSL_SELF_TEST)
1092 add_count++;
1093#endif
1094
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001095 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001096 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001097 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001098 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1099 return( ecp_copy( R, Q ) );
1100
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001101 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1102 return( ecp_copy( R, P ) );
1103
1104 /*
1105 * Make sure Q coordinates are normalized
1106 */
1107 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001108 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001109
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001110 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1111 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001112
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001113 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1114 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1115 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1116 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1117 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1118 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001119
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001120 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001121 if( mpi_cmp_int( &T1, 0 ) == 0 )
1122 {
1123 if( mpi_cmp_int( &T2, 0 ) == 0 )
1124 {
1125 ret = ecp_double_jac( grp, R, P );
1126 goto cleanup;
1127 }
1128 else
1129 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001130 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001131 goto cleanup;
1132 }
1133 }
1134
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001135 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1136 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1137 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1138 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1139 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1140 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1141 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1142 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1143 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1144 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1145 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1146 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001147
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001148 MPI_CHK( mpi_copy( &R->X, &X ) );
1149 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1150 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001151
1152cleanup:
1153
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001154 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1155 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001156
1157 return( ret );
1158}
1159
1160/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001161 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001162 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001163 */
1164int ecp_add( const ecp_group *grp, ecp_point *R,
1165 const ecp_point *P, const ecp_point *Q )
1166{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001167 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001168
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001169 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001170 MPI_CHK( ecp_normalize( grp, R ) );
1171
1172cleanup:
1173 return( ret );
1174}
1175
1176/*
1177 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001178 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001179 */
1180int ecp_sub( const ecp_group *grp, ecp_point *R,
1181 const ecp_point *P, const ecp_point *Q )
1182{
1183 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001184 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001185
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001186 ecp_point_init( &mQ );
1187
1188 /* mQ = - Q */
1189 ecp_copy( &mQ, Q );
1190 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1191 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1192
1193 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001194 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001195
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001196cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001197 ecp_point_free( &mQ );
1198
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001199 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001200}
1201
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001202/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001203 * Randomize jacobian coordinates:
1204 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1205 * This is sort of the reverse operation of ecp_normalize().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001206 *
1207 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001208 */
1209static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1210 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1211{
1212 int ret;
1213 mpi l, ll;
1214 size_t p_size = (grp->pbits + 7) / 8;
1215 int count = 0;
1216
1217 mpi_init( &l ); mpi_init( &ll );
1218
1219 /* Generate l such that 1 < l < p */
1220 do
1221 {
1222 mpi_fill_random( &l, p_size, f_rng, p_rng );
1223
1224 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1225 mpi_shift_r( &l, 1 );
1226
1227 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001228 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001229 }
1230 while( mpi_cmp_int( &l, 1 ) <= 0 );
1231
1232 /* Z = l * Z */
1233 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1234
1235 /* X = l^2 * X */
1236 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1237 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1238
1239 /* Y = l^3 * Y */
1240 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1241 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1242
1243cleanup:
1244 mpi_free( &l ); mpi_free( &ll );
1245
1246 return( ret );
1247}
1248
1249/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001250 * Check and define parameters used by the comb method (see below for details)
1251 */
1252#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1253#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1254#endif
1255
1256/* d = ceil( n / w ) */
1257#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1258
1259/* number of precomputed points */
1260#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1261
1262/*
1263 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001264 *
1265 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001266 * modified version that provides resistance to SPA by avoiding zero
1267 * digits in the representation as in [3]. We modify the method further by
1268 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001269 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001270 *
1271 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1272 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1273 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001274 *
1275 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001276 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001277 * - w is the size, ie number of teeth, of the comb, and must be between
1278 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001279 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1280 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001281 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001282static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001283 unsigned char w, const mpi *m )
1284{
1285 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001286 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001287
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001288 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001289
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001290 /* First get the classical comb values (except for x_d = 0) */
1291 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001292 for( j = 0; j < w; j++ )
1293 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1294
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001295 /* Now make sure x_1 .. x_d are odd */
1296 c = 0;
1297 for( i = 1; i <= d; i++ )
1298 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001299 /* Add carry and update it */
1300 cc = x[i] & c;
1301 x[i] = x[i] ^ c;
1302 c = cc;
1303
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001304 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001305 adjust = 1 - ( x[i] & 0x01 );
1306 c |= x[i] & ( x[i-1] * adjust );
1307 x[i] = x[i] ^ ( x[i-1] * adjust );
1308 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001309 }
1310}
1311
1312/*
1313 * Precompute points for the comb method
1314 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001315 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1316 * 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 +01001317 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001318 * T must be able to hold 2^{w - 1} elements
1319 *
1320 * 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 +01001321 */
1322static int ecp_precompute_comb( const ecp_group *grp,
1323 ecp_point T[], const ecp_point *P,
1324 unsigned char w, size_t d )
1325{
1326 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001327 unsigned char i, k;
1328 size_t j;
1329 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001330
1331 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001332 * Set T[0] = P and
1333 * 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 +01001334 */
1335 MPI_CHK( ecp_copy( &T[0], P ) );
1336
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001337 k = 0;
1338 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001339 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001340 cur = T + i;
1341 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001342 for( j = 0; j < d; j++ )
1343 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001344
1345 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001346 }
1347
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001348 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001349
1350 /*
1351 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001352 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001353 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001354 k = 0;
1355 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001356 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001357 j = i;
1358 while( j-- )
1359 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001360 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001361 TT[k++] = &T[i + j];
1362 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001363 }
1364
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001365 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001366
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001367 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001368 * Post-precessing: reclaim some memory by
1369 * - not storing Z (always 1)
1370 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001371 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001372 */
1373 for( i = 0; i < ( 1U << (w-1) ); i++ )
1374 {
1375 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001376 mpi_shrink( &T[i].X, grp->P.n );
1377 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001378 }
1379
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001380cleanup:
1381 return( ret );
1382}
1383
1384/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001385 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001386 */
1387static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001388 ecp_point T[], unsigned char t_len,
1389 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001390{
1391 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001392 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001393
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001394 /* Ignore the "sign" bit and scale down */
1395 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001396
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001397 /* Read the whole table to thwart cache-based timing attacks */
1398 for( j = 0; j < t_len; j++ )
1399 {
1400 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1401 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1402 }
1403
1404 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001405 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1406
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001407 /* Safely invert result if i is "negative" */
1408 MPI_CHK( ecp_safe_invert( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001409
1410cleanup:
1411 return( ret );
1412}
1413
1414/*
1415 * Core multiplication algorithm for the (modified) comb method.
1416 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001417 *
1418 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001419 */
1420static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001421 ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001422 const unsigned char x[], size_t d,
1423 int (*f_rng)(void *, unsigned char *, size_t),
1424 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001425{
1426 int ret;
1427 ecp_point Txi;
1428 size_t i;
1429
1430 ecp_point_init( &Txi );
1431
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001432 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001433 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001434 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001435 if( f_rng != 0 )
1436 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001437
1438 while( i-- != 0 )
1439 {
1440 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001441 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001442 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001443 }
1444
1445cleanup:
1446 ecp_point_free( &Txi );
1447
1448 return( ret );
1449}
1450
1451/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001452 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001453 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001454int ecp_mul( ecp_group *grp, ecp_point *R,
1455 const mpi *m, const ecp_point *P,
1456 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001457{
1458 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001459 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1460 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001461 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001462 ecp_point *T;
1463 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001464
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001465 /*
1466 * Sanity checks (before we even initialize anything)
1467 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001468 if( mpi_cmp_int( &P->Z, 1 ) != 0 ||
1469 mpi_get_bit( &grp->N, 0 ) != 1 )
1470 {
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001471 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001472 }
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001473
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001474 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1475 return( ret );
1476
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001477 /* We'll need this later, but do it now to possibly avoid checking P */
1478 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001479 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001480
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001481 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1482 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001483
1484 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001485 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001486
1487 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001488 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001489 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001490 * (see costs of the various parts, with 1S = 1M)
1491 */
1492 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001493
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001494 /*
1495 * If P == G, pre-compute a bit more, since this may be re-used later.
1496 * Just adding one ups the cost of the first mul by at most 3%.
1497 */
1498 if( p_eq_g )
1499 w++;
1500
1501 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001502 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001503 * (The last test is useful only for very small curves in the test suite.)
1504 */
1505 if( w > POLARSSL_ECP_WINDOW_SIZE )
1506 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001507 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001508 w = 2;
1509
1510 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001511 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001512 d = ( grp->nbits + w - 1 ) / w;
1513
1514 /*
1515 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001516 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001517 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001518 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001519
1520 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001521 {
1522 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1523 if( T == NULL )
1524 {
1525 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1526 goto cleanup;
1527 }
1528
1529 for( i = 0; i < pre_len; i++ )
1530 ecp_point_init( &T[i] );
1531
1532 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1533
1534 if( p_eq_g )
1535 {
1536 grp->T = T;
1537 grp->T_size = pre_len;
1538 }
1539 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001540
1541 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001542 * Make sure M is odd (M = m or M = N - m, since N is odd)
1543 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001544 */
1545 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001546 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001547 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1548 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001549
1550 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001551 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001552 */
1553 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001554 MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001555
1556 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001557 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001558 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001559 MPI_CHK( ecp_safe_invert( grp, R, ! m_is_odd ) );
1560 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001561
1562cleanup:
1563
1564 if( T != NULL && ! p_eq_g )
1565 {
1566 for( i = 0; i < pre_len; i++ )
1567 ecp_point_free( &T[i] );
1568 polarssl_free( T );
1569 }
1570
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001571 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001572 mpi_free( &mm );
1573
1574 if( ret != 0 )
1575 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001576
1577 return( ret );
1578}
1579
1580/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001581 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1582 */
1583int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1584{
1585 int ret;
1586 mpi YY, RHS;
1587
1588 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001589 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001590
1591 /*
1592 * pt coordinates must be normalized for our checks
1593 */
1594 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001595 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001596
1597 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1598 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1599 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1600 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001601 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001602
1603 mpi_init( &YY ); mpi_init( &RHS );
1604
1605 /*
1606 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001607 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001608 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001609 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1610 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001611 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001612 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1613 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001614
1615 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001616 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001617
1618cleanup:
1619
1620 mpi_free( &YY ); mpi_free( &RHS );
1621
1622 return( ret );
1623}
1624
1625/*
1626 * Check that an mpi is valid as a private key (SEC1 3.2)
1627 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001628int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001629{
1630 /* We want 1 <= d <= N-1 */
1631 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001632 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001633
1634 return( 0 );
1635}
1636
1637/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001638 * Generate a keypair (SEC1 3.2.1)
1639 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001640int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001641 int (*f_rng)(void *, unsigned char *, size_t),
1642 void *p_rng )
1643{
1644 int count = 0;
1645 size_t n_size = (grp->nbits + 7) / 8;
1646
1647 /*
1648 * Generate d such that 1 <= n < N
1649 */
1650 do
1651 {
1652 mpi_fill_random( d, n_size, f_rng, p_rng );
1653
1654 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1655 mpi_shift_r( d, 1 );
1656
1657 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001658 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001659 }
1660 while( mpi_cmp_int( d, 1 ) < 0 );
1661
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001662 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001663}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001664
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001665#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard9fcceac2013-10-23 20:56:12 +02001666/*
1667 * Fast reduction modulo the primes used by the NIST curves.
1668 *
1669 * These functions are: critical for speed, but not need for correct
1670 * operations. So, we make the choice to heavily rely on the internals of our
1671 * bignum library, which creates a tight coupling between these functions and
1672 * our MPI implementation. However, the coupling between the ECP module and
1673 * MPI remains loose, since these functions can be deactivated at will.
1674 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001675
1676#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1677/*
1678 * Compared to the way things are presented in FIPS 186-3 D.2,
1679 * we proceed in columns, from right (least significant chunk) to left,
1680 * adding chunks to N in place, and keeping a carry for the next chunk.
1681 * This avoids moving things around in memory, and uselessly adding zeros,
1682 * compared to the more straightforward, line-oriented approach.
1683 *
1684 * For this prime we need to handle data in chunks of 64 bits.
1685 * Since this is always a multiple of our basic t_uint, we can
1686 * use a t_uint * to designate such a chunk, and small loops to handle it.
1687 */
1688
1689/* Add 64-bit chunks (dst += src) and update carry */
1690static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
1691{
1692 unsigned char i;
1693 t_uint c = 0;
1694 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
1695 {
1696 *dst += c; c = ( *dst < c );
1697 *dst += *src; c += ( *dst < *src );
1698 }
1699 *carry += c;
1700}
1701
1702/* Add carry to a 64-bit chunk and update carry */
1703static inline void carry64( t_uint *dst, t_uint *carry )
1704{
1705 unsigned char i;
1706 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
1707 {
1708 *dst += *carry;
1709 *carry = ( *dst < *carry );
1710 }
1711}
1712
1713#define WIDTH 8 / sizeof( t_uint )
1714#define A( i ) N->p + i * WIDTH
1715#define ADD( i ) add64( p, A( i ), &c )
1716#define NEXT p += WIDTH; carry64( p, &c )
1717#define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
1718
1719/*
1720 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
1721 */
1722static int ecp_mod_p192( mpi *N )
1723{
1724 int ret;
1725 t_uint c = 0;
1726 t_uint *p, *end;
1727
1728 /* Make sure we have enough blocks so that A(5) is legal */
1729 MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
1730
1731 p = N->p;
1732 end = p + N->n;
1733
1734 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
1735 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
1736 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
1737
1738cleanup:
1739 return( ret );
1740}
1741
1742#undef WIDTH
1743#undef A
1744#undef ADD
1745#undef NEXT
1746#undef LAST
1747#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
1748
1749#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
1750 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
1751 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1752/*
1753 * The reader is advised to first understand ecp_mod_p192() since the same
1754 * general structure is used here, but with additional complications:
1755 * (1) chunks of 32 bits, and (2) subtractions.
1756 */
1757
1758/*
1759 * For these primes, we need to handle data in chunks of 32 bits.
1760 * This makes it more complicated if we use 64 bits limbs in MPI,
1761 * which prevents us from using a uniform access method as for p192.
1762 *
1763 * So, we define a mini abstraction layer to access 32 bit chunks,
1764 * load them in 'cur' for work, and store them back from 'cur' when done.
1765 *
1766 * While at it, also define the size of N in terms of 32-bit chunks.
1767 */
1768#define LOAD32 cur = A( i );
1769
1770#if defined(POLARSSL_HAVE_INT8) /* 8 bit */
1771
1772#define MAX32 N->n / 4
1773#define A( j ) (uint32_t)( N->p[4*j+0] ) | \
1774 ( N->p[4*j+1] << 8 ) | \
1775 ( N->p[4*j+2] << 16 ) | \
1776 ( N->p[4*j+3] << 24 )
1777#define STORE32 N->p[4*i+0] = (uint8_t)( cur ); \
1778 N->p[4*i+1] = (uint8_t)( cur >> 8 ); \
1779 N->p[4*i+2] = (uint8_t)( cur >> 16 ); \
1780 N->p[4*i+3] = (uint8_t)( cur >> 24 );
1781
1782#elif defined(POLARSSL_HAVE_INT16) /* 16 bit */
1783
1784#define MAX32 N->n / 2
1785#define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
1786#define STORE32 N->p[2*i+0] = (uint16_t)( cur ); \
1787 N->p[2*i+1] = (uint16_t)( cur >> 16 );
1788
1789#elif defined(POLARSSL_HAVE_INT32) /* 32 bit */
1790
1791#define MAX32 N->n
1792#define A( j ) N->p[j]
1793#define STORE32 N->p[i] = cur;
1794
1795#else /* 64-bit */
1796
1797#define MAX32 N->n * 2
1798#define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
1799#define STORE32 \
1800 if( i % 2 ) { \
1801 N->p[i/2] &= 0x00000000FFFFFFFF; \
1802 N->p[i/2] |= ((uint64_t) cur) << 32; \
1803 } else { \
1804 N->p[i/2] &= 0xFFFFFFFF00000000; \
1805 N->p[i/2] |= (uint64_t) cur; \
1806 }
1807
1808#endif /* sizeof( t_uint ) */
1809
1810/*
1811 * Helpers for addition and subtraction of chunks, with signed carry.
1812 */
1813static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
1814{
1815 *dst += src;
1816 *carry += ( *dst < src );
1817}
1818
1819static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
1820{
1821 *carry -= ( *dst < src );
1822 *dst -= src;
1823}
1824
1825#define ADD( j ) add32( &cur, A( j ), &c );
1826#define SUB( j ) sub32( &cur, A( j ), &c );
1827
1828/*
1829 * Helpers for the main 'loop'
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001830 * (see fix_negative for the motivation of C)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001831 */
1832#define INIT( b ) \
1833 int ret; \
1834 signed char c = 0, cc; \
1835 uint32_t cur; \
1836 size_t i = 0, bits = b; \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001837 mpi C; \
1838 t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \
1839 \
1840 C.s = 1; \
1841 C.n = b / 8 / sizeof( t_uint) + 1; \
1842 C.p = Cp; \
1843 memset( Cp, 0, C.n * sizeof( t_uint ) ); \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001844 \
1845 MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
1846 LOAD32;
1847
1848#define NEXT \
1849 STORE32; i++; LOAD32; \
1850 cc = c; c = 0; \
1851 if( cc < 0 ) \
1852 sub32( &cur, -cc, &c ); \
1853 else \
1854 add32( &cur, cc, &c ); \
1855
1856#define LAST \
1857 STORE32; i++; \
1858 cur = c > 0 ? c : 0; STORE32; \
1859 cur = 0; while( ++i < MAX32 ) { STORE32; } \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001860 if( c < 0 ) fix_negative( N, c, &C, bits );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001861
1862/*
1863 * If the result is negative, we get it in the form
1864 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
1865 */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001866static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001867{
1868 int ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001869
1870 /* C = - c * 2^(bits + 32) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001871#if !defined(POLARSSL_HAVE_INT64)
1872 ((void) bits);
1873#else
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001874 if( bits == 224 )
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001875 C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001876 else
1877#endif
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001878 C->p[ C->n - 1 ] = (t_uint) -c;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001879
1880 /* N = - ( C - N ) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001881 MPI_CHK( mpi_sub_abs( N, C, N ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001882 N->s = -1;
1883
1884cleanup:
1885
1886 return( ret );
1887}
1888
1889#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1890/*
1891 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
1892 */
1893static int ecp_mod_p224( mpi *N )
1894{
1895 INIT( 224 );
1896
1897 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
1898 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
1899 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
1900 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
1901 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
1902 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
1903 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
1904
1905cleanup:
1906 return( ret );
1907}
1908#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1909
1910#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1911/*
1912 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
1913 */
1914static int ecp_mod_p256( mpi *N )
1915{
1916 INIT( 256 );
1917
1918 ADD( 8 ); ADD( 9 );
1919 SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0
1920
1921 ADD( 9 ); ADD( 10 );
1922 SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1
1923
1924 ADD( 10 ); ADD( 11 );
1925 SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2
1926
1927 ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
1928 SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3
1929
1930 ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
1931 SUB( 9 ); SUB( 10 ); NEXT; // A4
1932
1933 ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
1934 SUB( 10 ); SUB( 11 ); NEXT; // A5
1935
1936 ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
1937 SUB( 8 ); SUB( 9 ); NEXT; // A6
1938
1939 ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
1940 SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7
1941
1942cleanup:
1943 return( ret );
1944}
1945#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1946
1947#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1948/*
1949 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
1950 */
1951static int ecp_mod_p384( mpi *N )
1952{
1953 INIT( 384 );
1954
1955 ADD( 12 ); ADD( 21 ); ADD( 20 );
1956 SUB( 23 ); NEXT; // A0
1957
1958 ADD( 13 ); ADD( 22 ); ADD( 23 );
1959 SUB( 12 ); SUB( 20 ); NEXT; // A2
1960
1961 ADD( 14 ); ADD( 23 );
1962 SUB( 13 ); SUB( 21 ); NEXT; // A2
1963
1964 ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
1965 SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3
1966
1967 ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
1968 SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4
1969
1970 ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
1971 SUB( 16 ); NEXT; // A5
1972
1973 ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
1974 SUB( 17 ); NEXT; // A6
1975
1976 ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
1977 SUB( 18 ); NEXT; // A7
1978
1979 ADD( 20 ); ADD( 17 ); ADD( 16 );
1980 SUB( 19 ); NEXT; // A8
1981
1982 ADD( 21 ); ADD( 18 ); ADD( 17 );
1983 SUB( 20 ); NEXT; // A9
1984
1985 ADD( 22 ); ADD( 19 ); ADD( 18 );
1986 SUB( 21 ); NEXT; // A10
1987
1988 ADD( 23 ); ADD( 20 ); ADD( 19 );
1989 SUB( 22 ); LAST; // A11
1990
1991cleanup:
1992 return( ret );
1993}
1994#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1995
1996#undef A
1997#undef LOAD32
1998#undef STORE32
1999#undef MAX32
2000#undef INIT
2001#undef NEXT
2002#undef LAST
2003
2004#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
2005 POLARSSL_ECP_DP_SECP256R1_ENABLED ||
2006 POLARSSL_ECP_DP_SECP384R1_ENABLED */
2007
2008#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
2009/*
2010 * Here we have an actual Mersenne prime, so things are more straightforward.
2011 * However, chunks are aligned on a 'weird' boundary (521 bits).
2012 */
2013
2014/* Size of p521 in terms of t_uint */
2015#define P521_WIDTH ( 521 / 8 / sizeof( t_uint ) + 1 )
2016
2017/* Bits to keep in the most significant t_uint */
2018#if defined(POLARSSL_HAVE_INT8)
2019#define P521_MASK 0x01
2020#else
2021#define P521_MASK 0x01FF
2022#endif
2023
2024/*
2025 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
2026 * Write N as A1 + 2^521 A0, return A0 + A1
2027 */
2028static int ecp_mod_p521( mpi *N )
2029{
2030 int ret;
2031 size_t i;
2032 mpi M;
2033 t_uint Mp[P521_WIDTH + 1];
2034 /* Worst case for the size of M is when t_uint is 16 bits:
2035 * we need to hold bits 513 to 1056, which is 34 limbs, that is
2036 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
2037
2038 if( N->n < P521_WIDTH )
2039 return( 0 );
2040
2041 /* M = A1 */
2042 M.s = 1;
2043 M.n = N->n - ( P521_WIDTH - 1 );
2044 if( M.n > P521_WIDTH + 1 )
2045 M.n = P521_WIDTH + 1;
2046 M.p = Mp;
2047 memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) );
2048 MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
2049
2050 /* N = A0 */
2051 N->p[P521_WIDTH - 1] &= P521_MASK;
2052 for( i = P521_WIDTH; i < N->n; i++ )
2053 N->p[i] = 0;
2054
2055 /* N = A0 + A1 */
2056 MPI_CHK( mpi_add_abs( N, N, &M ) );
2057
2058cleanup:
2059 return( ret );
2060}
2061
2062#undef P521_WIDTH
2063#undef P521_MASK
2064#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
2065
2066#endif /* POLARSSL_ECP_NIST_OPTIM */
2067
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002068#if defined(POLARSSL_SELF_TEST)
2069
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002070/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002071 * Checkup routine
2072 */
2073int ecp_self_test( int verbose )
2074{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002075 int ret;
2076 size_t i;
2077 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002078 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002079 mpi m;
2080 unsigned long add_c_prev, dbl_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002081 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002082 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002083 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002084 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002085 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002086 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002087 "400000000000000000000000000000000000000000000000", /* one and zeros */
2088 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2089 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002090 };
2091
2092 ecp_group_init( &grp );
2093 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002094 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002095 mpi_init( &m );
2096
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002097 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002098#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002099 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002100#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002101 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
2102#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002103
2104 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002105 printf( " ECP test #1 (constant op_count, base point G): " );
2106
2107 /* Do a dummy multiplication first to trigger precomputation */
2108 MPI_CHK( mpi_lset( &m, 2 ) );
2109 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002110
2111 add_count = 0;
2112 dbl_count = 0;
2113 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002114 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002115
2116 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2117 {
2118 add_c_prev = add_count;
2119 dbl_c_prev = dbl_count;
2120 add_count = 0;
2121 dbl_count = 0;
2122
2123 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002124 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002125
2126 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2127 {
2128 if( verbose != 0 )
2129 printf( "failed (%zu)\n", i );
2130
2131 ret = 1;
2132 goto cleanup;
2133 }
2134 }
2135
2136 if( verbose != 0 )
2137 printf( "passed\n" );
2138
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002139 if( verbose != 0 )
2140 printf( " ECP test #2 (constant op_count, other point): " );
2141 /* We computed P = 2G last time, use it */
2142
2143 add_count = 0;
2144 dbl_count = 0;
2145 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2146 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2147
2148 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2149 {
2150 add_c_prev = add_count;
2151 dbl_c_prev = dbl_count;
2152 add_count = 0;
2153 dbl_count = 0;
2154
2155 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2156 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2157
2158 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2159 {
2160 if( verbose != 0 )
2161 printf( "failed (%zu)\n", i );
2162
2163 ret = 1;
2164 goto cleanup;
2165 }
2166 }
2167
2168 if( verbose != 0 )
2169 printf( "passed\n" );
2170
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002171cleanup:
2172
2173 if( ret < 0 && verbose != 0 )
2174 printf( "Unexpected error, return code = %08X\n", ret );
2175
2176 ecp_group_free( &grp );
2177 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002178 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002179 mpi_free( &m );
2180
2181 if( verbose != 0 )
2182 printf( "\n" );
2183
2184 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002185}
2186
2187#endif
2188
2189#endif