blob: 3d2c6e2cfad17bd4720ab7cf5f286fc705252e2a [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é-Gonnard04a02252013-11-20 22:57:38 +01001075 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001076 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001077static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001078 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001079{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001080 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001081 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001082
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001083#if defined(POLARSSL_SELF_TEST)
1084 add_count++;
1085#endif
1086
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001087 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001088 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001089 * This will never happen during ecp_mul() so we don't mind the branches.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001090 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001091 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1092 return( ecp_copy( R, Q ) );
1093
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001094 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1095 return( ecp_copy( R, P ) );
1096
1097 /*
1098 * Make sure Q coordinates are normalized
1099 */
1100 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001101 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001102
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001103 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1104 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001105
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001106 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1107 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1108 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1109 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1110 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1111 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001112
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001113 /* TODO: make sure it never happens during ecp_mul() */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001114 if( mpi_cmp_int( &T1, 0 ) == 0 )
1115 {
1116 if( mpi_cmp_int( &T2, 0 ) == 0 )
1117 {
1118 ret = ecp_double_jac( grp, R, P );
1119 goto cleanup;
1120 }
1121 else
1122 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001123 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001124 goto cleanup;
1125 }
1126 }
1127
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001128 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1129 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1130 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1131 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1132 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1133 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1134 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1135 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1136 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1137 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1138 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1139 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001140
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001141 MPI_CHK( mpi_copy( &R->X, &X ) );
1142 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1143 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001144
1145cleanup:
1146
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001147 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1148 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001149
1150 return( ret );
1151}
1152
1153/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001154 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001155 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001156 */
1157int ecp_add( const ecp_group *grp, ecp_point *R,
1158 const ecp_point *P, const ecp_point *Q )
1159{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001160 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001161
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001162 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001163 MPI_CHK( ecp_normalize( grp, R ) );
1164
1165cleanup:
1166 return( ret );
1167}
1168
1169/*
1170 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001171 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001172 */
1173int ecp_sub( const ecp_group *grp, ecp_point *R,
1174 const ecp_point *P, const ecp_point *Q )
1175{
1176 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001177 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001178
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001179 ecp_point_init( &mQ );
1180
1181 /* mQ = - Q */
1182 ecp_copy( &mQ, Q );
1183 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1184 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1185
1186 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001187 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001188
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001189cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001190 ecp_point_free( &mQ );
1191
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001192 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001193}
1194
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001195/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001196 * Randomize jacobian coordinates:
1197 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1198 * This is sort of the reverse operation of ecp_normalize().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001199 *
1200 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001201 */
1202static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1203 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1204{
1205 int ret;
1206 mpi l, ll;
1207 size_t p_size = (grp->pbits + 7) / 8;
1208 int count = 0;
1209
1210 mpi_init( &l ); mpi_init( &ll );
1211
1212 /* Generate l such that 1 < l < p */
1213 do
1214 {
1215 mpi_fill_random( &l, p_size, f_rng, p_rng );
1216
1217 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1218 mpi_shift_r( &l, 1 );
1219
1220 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001221 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001222 }
1223 while( mpi_cmp_int( &l, 1 ) <= 0 );
1224
1225 /* Z = l * Z */
1226 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1227
1228 /* X = l^2 * X */
1229 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1230 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1231
1232 /* Y = l^3 * Y */
1233 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1234 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1235
1236cleanup:
1237 mpi_free( &l ); mpi_free( &ll );
1238
1239 return( ret );
1240}
1241
1242/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001243 * Check and define parameters used by the comb method (see below for details)
1244 */
1245#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1246#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1247#endif
1248
1249/* d = ceil( n / w ) */
1250#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1251
1252/* number of precomputed points */
1253#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1254
1255/*
1256 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001257 *
1258 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001259 * modified version that provides resistance to SPA by avoiding zero
1260 * digits in the representation as in [3]. We modify the method further by
1261 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001262 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001263 *
1264 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1265 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1266 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001267 *
1268 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001269 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001270 * - w is the size, ie number of teeth, of the comb, and must be between
1271 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001272 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1273 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001274 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001275static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001276 unsigned char w, const mpi *m )
1277{
1278 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001279 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001280
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001281 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001282
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001283 /* First get the classical comb values (except for x_d = 0) */
1284 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001285 for( j = 0; j < w; j++ )
1286 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1287
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001288 /* Now make sure x_1 .. x_d are odd */
1289 c = 0;
1290 for( i = 1; i <= d; i++ )
1291 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001292 /* Add carry and update it */
1293 cc = x[i] & c;
1294 x[i] = x[i] ^ c;
1295 c = cc;
1296
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001297 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001298 adjust = 1 - ( x[i] & 0x01 );
1299 c |= x[i] & ( x[i-1] * adjust );
1300 x[i] = x[i] ^ ( x[i-1] * adjust );
1301 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001302 }
1303}
1304
1305/*
1306 * Precompute points for the comb method
1307 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001308 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1309 * 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 +01001310 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001311 * T must be able to hold 2^{w - 1} elements
1312 *
1313 * 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 +01001314 */
1315static int ecp_precompute_comb( const ecp_group *grp,
1316 ecp_point T[], const ecp_point *P,
1317 unsigned char w, size_t d )
1318{
1319 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001320 unsigned char i, k;
1321 size_t j;
1322 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001323
1324 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001325 * Set T[0] = P and
1326 * 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 +01001327 */
1328 MPI_CHK( ecp_copy( &T[0], P ) );
1329
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001330 k = 0;
1331 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001332 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001333 cur = T + i;
1334 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001335 for( j = 0; j < d; j++ )
1336 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001337
1338 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001339 }
1340
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001341 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001342
1343 /*
1344 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001345 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001346 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001347 k = 0;
1348 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001349 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001350 j = i;
1351 while( j-- )
1352 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001353 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001354 TT[k++] = &T[i + j];
1355 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001356 }
1357
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001358 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001359
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001360 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001361 * Post-precessing: reclaim some memory by
1362 * - not storing Z (always 1)
1363 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001364 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001365 */
1366 for( i = 0; i < ( 1U << (w-1) ); i++ )
1367 {
1368 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001369 mpi_shrink( &T[i].X, grp->P.n );
1370 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001371 }
1372
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001373cleanup:
1374 return( ret );
1375}
1376
1377/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001378 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001379 */
1380static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001381 const ecp_point T[], unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001382{
1383 int ret;
1384
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001385 /* Ignore the "sign" bit */
1386 MPI_CHK( ecp_copy( R, &T[ ( i & 0x7Fu ) >> 1 ] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001387
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001388 /* Restore the Z coordinate */
1389 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1390
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001391 /* Safely invert result if i is "negative" */
1392 MPI_CHK( ecp_safe_invert( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001393
1394cleanup:
1395 return( ret );
1396}
1397
1398/*
1399 * Core multiplication algorithm for the (modified) comb method.
1400 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001401 *
1402 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001403 */
1404static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001405 const ecp_point T[],
1406 const unsigned char x[], size_t d,
1407 int (*f_rng)(void *, unsigned char *, size_t),
1408 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001409{
1410 int ret;
1411 ecp_point Txi;
1412 size_t i;
1413
1414 ecp_point_init( &Txi );
1415
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001416 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001417 i = d;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001418 MPI_CHK( ecp_select_comb( grp, R, T, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001419 if( f_rng != 0 )
1420 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001421
1422 while( i-- != 0 )
1423 {
1424 MPI_CHK( ecp_double_jac( grp, R, R ) );
1425 MPI_CHK( ecp_select_comb( grp, &Txi, T, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001426 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001427 }
1428
1429cleanup:
1430 ecp_point_free( &Txi );
1431
1432 return( ret );
1433}
1434
1435/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001436 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001437 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001438int ecp_mul( ecp_group *grp, ecp_point *R,
1439 const mpi *m, const ecp_point *P,
1440 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001441{
1442 int ret;
1443 unsigned char w, m_is_odd, p_eq_g;
1444 size_t pre_len, d, i;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001445 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001446 ecp_point Q, *T = NULL, S[2];
1447 mpi M;
1448
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001449 /*
1450 * Sanity checks (before we even initialize anything)
1451 */
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001452 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1453 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1454
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001455 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1456 return( ret );
1457
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001458 /* We'll need this later, but do it now to possibly avoid checking P */
1459 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001460 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001461
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001462 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1463 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001464
1465 mpi_init( &M );
1466 ecp_point_init( &Q );
1467 ecp_point_init( &S[0] );
1468 ecp_point_init( &S[1] );
1469
1470 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001471 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001472 * 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 +01001473 * (see costs of the various parts, with 1S = 1M)
1474 */
1475 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001476
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001477 /*
1478 * If P == G, pre-compute a bit more, since this may be re-used later.
1479 * Just adding one ups the cost of the first mul by at most 3%.
1480 */
1481 if( p_eq_g )
1482 w++;
1483
1484 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001485 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001486 * (The last test is useful only for very small curves in the test suite.)
1487 */
1488 if( w > POLARSSL_ECP_WINDOW_SIZE )
1489 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001490 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001491 w = 2;
1492
1493 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001494 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001495 d = ( grp->nbits + w - 1 ) / w;
1496
1497 /*
1498 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001499 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001500 */
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001501 if( p_eq_g )
1502 T = grp->T;
1503
1504 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001505 {
1506 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1507 if( T == NULL )
1508 {
1509 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1510 goto cleanup;
1511 }
1512
1513 for( i = 0; i < pre_len; i++ )
1514 ecp_point_init( &T[i] );
1515
1516 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1517
1518 if( p_eq_g )
1519 {
1520 grp->T = T;
1521 grp->T_size = pre_len;
1522 }
1523 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001524
1525 /*
1526 * Make sure M is odd (M = m + 1 or M = m + 2)
1527 * later we'll get m * P by subtracting P or 2 * P to M * P.
1528 */
1529 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1530
1531 MPI_CHK( mpi_copy( &M, m ) );
1532 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
1533
1534 /*
1535 * Go for comb multiplication, Q = M * P
1536 */
1537 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001538 ecp_mul_comb_core( grp, &Q, T, k, d, f_rng, p_rng );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001539
1540 /*
1541 * Now get m * P from M * P
1542 */
1543 MPI_CHK( ecp_copy( &S[0], P ) );
1544 MPI_CHK( ecp_add( grp, &S[1], P, P ) );
1545 MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) );
1546
1547cleanup:
1548
1549 if( T != NULL && ! p_eq_g )
1550 {
1551 for( i = 0; i < pre_len; i++ )
1552 ecp_point_free( &T[i] );
1553 polarssl_free( T );
1554 }
1555
1556 ecp_point_free( &S[1] );
1557 ecp_point_free( &S[0] );
1558 ecp_point_free( &Q );
1559 mpi_free( &M );
1560
1561 return( ret );
1562}
1563
1564/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001565 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1566 */
1567int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1568{
1569 int ret;
1570 mpi YY, RHS;
1571
1572 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001573 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001574
1575 /*
1576 * pt coordinates must be normalized for our checks
1577 */
1578 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001579 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001580
1581 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1582 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1583 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1584 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001585 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001586
1587 mpi_init( &YY ); mpi_init( &RHS );
1588
1589 /*
1590 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001591 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001592 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001593 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1594 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001595 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001596 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1597 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001598
1599 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001600 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001601
1602cleanup:
1603
1604 mpi_free( &YY ); mpi_free( &RHS );
1605
1606 return( ret );
1607}
1608
1609/*
1610 * Check that an mpi is valid as a private key (SEC1 3.2)
1611 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001612int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001613{
1614 /* We want 1 <= d <= N-1 */
1615 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001616 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001617
1618 return( 0 );
1619}
1620
1621/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001622 * Generate a keypair (SEC1 3.2.1)
1623 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001624int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001625 int (*f_rng)(void *, unsigned char *, size_t),
1626 void *p_rng )
1627{
1628 int count = 0;
1629 size_t n_size = (grp->nbits + 7) / 8;
1630
1631 /*
1632 * Generate d such that 1 <= n < N
1633 */
1634 do
1635 {
1636 mpi_fill_random( d, n_size, f_rng, p_rng );
1637
1638 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1639 mpi_shift_r( d, 1 );
1640
1641 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001642 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001643 }
1644 while( mpi_cmp_int( d, 1 ) < 0 );
1645
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001646 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001647}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001648
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001649#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard9fcceac2013-10-23 20:56:12 +02001650/*
1651 * Fast reduction modulo the primes used by the NIST curves.
1652 *
1653 * These functions are: critical for speed, but not need for correct
1654 * operations. So, we make the choice to heavily rely on the internals of our
1655 * bignum library, which creates a tight coupling between these functions and
1656 * our MPI implementation. However, the coupling between the ECP module and
1657 * MPI remains loose, since these functions can be deactivated at will.
1658 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001659
1660#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1661/*
1662 * Compared to the way things are presented in FIPS 186-3 D.2,
1663 * we proceed in columns, from right (least significant chunk) to left,
1664 * adding chunks to N in place, and keeping a carry for the next chunk.
1665 * This avoids moving things around in memory, and uselessly adding zeros,
1666 * compared to the more straightforward, line-oriented approach.
1667 *
1668 * For this prime we need to handle data in chunks of 64 bits.
1669 * Since this is always a multiple of our basic t_uint, we can
1670 * use a t_uint * to designate such a chunk, and small loops to handle it.
1671 */
1672
1673/* Add 64-bit chunks (dst += src) and update carry */
1674static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
1675{
1676 unsigned char i;
1677 t_uint c = 0;
1678 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
1679 {
1680 *dst += c; c = ( *dst < c );
1681 *dst += *src; c += ( *dst < *src );
1682 }
1683 *carry += c;
1684}
1685
1686/* Add carry to a 64-bit chunk and update carry */
1687static inline void carry64( t_uint *dst, t_uint *carry )
1688{
1689 unsigned char i;
1690 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
1691 {
1692 *dst += *carry;
1693 *carry = ( *dst < *carry );
1694 }
1695}
1696
1697#define WIDTH 8 / sizeof( t_uint )
1698#define A( i ) N->p + i * WIDTH
1699#define ADD( i ) add64( p, A( i ), &c )
1700#define NEXT p += WIDTH; carry64( p, &c )
1701#define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
1702
1703/*
1704 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
1705 */
1706static int ecp_mod_p192( mpi *N )
1707{
1708 int ret;
1709 t_uint c = 0;
1710 t_uint *p, *end;
1711
1712 /* Make sure we have enough blocks so that A(5) is legal */
1713 MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
1714
1715 p = N->p;
1716 end = p + N->n;
1717
1718 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
1719 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
1720 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
1721
1722cleanup:
1723 return( ret );
1724}
1725
1726#undef WIDTH
1727#undef A
1728#undef ADD
1729#undef NEXT
1730#undef LAST
1731#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
1732
1733#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
1734 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
1735 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1736/*
1737 * The reader is advised to first understand ecp_mod_p192() since the same
1738 * general structure is used here, but with additional complications:
1739 * (1) chunks of 32 bits, and (2) subtractions.
1740 */
1741
1742/*
1743 * For these primes, we need to handle data in chunks of 32 bits.
1744 * This makes it more complicated if we use 64 bits limbs in MPI,
1745 * which prevents us from using a uniform access method as for p192.
1746 *
1747 * So, we define a mini abstraction layer to access 32 bit chunks,
1748 * load them in 'cur' for work, and store them back from 'cur' when done.
1749 *
1750 * While at it, also define the size of N in terms of 32-bit chunks.
1751 */
1752#define LOAD32 cur = A( i );
1753
1754#if defined(POLARSSL_HAVE_INT8) /* 8 bit */
1755
1756#define MAX32 N->n / 4
1757#define A( j ) (uint32_t)( N->p[4*j+0] ) | \
1758 ( N->p[4*j+1] << 8 ) | \
1759 ( N->p[4*j+2] << 16 ) | \
1760 ( N->p[4*j+3] << 24 )
1761#define STORE32 N->p[4*i+0] = (uint8_t)( cur ); \
1762 N->p[4*i+1] = (uint8_t)( cur >> 8 ); \
1763 N->p[4*i+2] = (uint8_t)( cur >> 16 ); \
1764 N->p[4*i+3] = (uint8_t)( cur >> 24 );
1765
1766#elif defined(POLARSSL_HAVE_INT16) /* 16 bit */
1767
1768#define MAX32 N->n / 2
1769#define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
1770#define STORE32 N->p[2*i+0] = (uint16_t)( cur ); \
1771 N->p[2*i+1] = (uint16_t)( cur >> 16 );
1772
1773#elif defined(POLARSSL_HAVE_INT32) /* 32 bit */
1774
1775#define MAX32 N->n
1776#define A( j ) N->p[j]
1777#define STORE32 N->p[i] = cur;
1778
1779#else /* 64-bit */
1780
1781#define MAX32 N->n * 2
1782#define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
1783#define STORE32 \
1784 if( i % 2 ) { \
1785 N->p[i/2] &= 0x00000000FFFFFFFF; \
1786 N->p[i/2] |= ((uint64_t) cur) << 32; \
1787 } else { \
1788 N->p[i/2] &= 0xFFFFFFFF00000000; \
1789 N->p[i/2] |= (uint64_t) cur; \
1790 }
1791
1792#endif /* sizeof( t_uint ) */
1793
1794/*
1795 * Helpers for addition and subtraction of chunks, with signed carry.
1796 */
1797static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
1798{
1799 *dst += src;
1800 *carry += ( *dst < src );
1801}
1802
1803static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
1804{
1805 *carry -= ( *dst < src );
1806 *dst -= src;
1807}
1808
1809#define ADD( j ) add32( &cur, A( j ), &c );
1810#define SUB( j ) sub32( &cur, A( j ), &c );
1811
1812/*
1813 * Helpers for the main 'loop'
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001814 * (see fix_negative for the motivation of C)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001815 */
1816#define INIT( b ) \
1817 int ret; \
1818 signed char c = 0, cc; \
1819 uint32_t cur; \
1820 size_t i = 0, bits = b; \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001821 mpi C; \
1822 t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \
1823 \
1824 C.s = 1; \
1825 C.n = b / 8 / sizeof( t_uint) + 1; \
1826 C.p = Cp; \
1827 memset( Cp, 0, C.n * sizeof( t_uint ) ); \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001828 \
1829 MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
1830 LOAD32;
1831
1832#define NEXT \
1833 STORE32; i++; LOAD32; \
1834 cc = c; c = 0; \
1835 if( cc < 0 ) \
1836 sub32( &cur, -cc, &c ); \
1837 else \
1838 add32( &cur, cc, &c ); \
1839
1840#define LAST \
1841 STORE32; i++; \
1842 cur = c > 0 ? c : 0; STORE32; \
1843 cur = 0; while( ++i < MAX32 ) { STORE32; } \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001844 if( c < 0 ) fix_negative( N, c, &C, bits );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001845
1846/*
1847 * If the result is negative, we get it in the form
1848 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
1849 */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001850static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001851{
1852 int ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001853
1854 /* C = - c * 2^(bits + 32) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001855#if !defined(POLARSSL_HAVE_INT64)
1856 ((void) bits);
1857#else
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001858 if( bits == 224 )
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001859 C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001860 else
1861#endif
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001862 C->p[ C->n - 1 ] = (t_uint) -c;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001863
1864 /* N = - ( C - N ) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001865 MPI_CHK( mpi_sub_abs( N, C, N ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001866 N->s = -1;
1867
1868cleanup:
1869
1870 return( ret );
1871}
1872
1873#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1874/*
1875 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
1876 */
1877static int ecp_mod_p224( mpi *N )
1878{
1879 INIT( 224 );
1880
1881 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
1882 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
1883 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
1884 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
1885 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
1886 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
1887 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
1888
1889cleanup:
1890 return( ret );
1891}
1892#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1893
1894#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1895/*
1896 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
1897 */
1898static int ecp_mod_p256( mpi *N )
1899{
1900 INIT( 256 );
1901
1902 ADD( 8 ); ADD( 9 );
1903 SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0
1904
1905 ADD( 9 ); ADD( 10 );
1906 SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1
1907
1908 ADD( 10 ); ADD( 11 );
1909 SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2
1910
1911 ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
1912 SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3
1913
1914 ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
1915 SUB( 9 ); SUB( 10 ); NEXT; // A4
1916
1917 ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
1918 SUB( 10 ); SUB( 11 ); NEXT; // A5
1919
1920 ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
1921 SUB( 8 ); SUB( 9 ); NEXT; // A6
1922
1923 ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
1924 SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7
1925
1926cleanup:
1927 return( ret );
1928}
1929#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1930
1931#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1932/*
1933 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
1934 */
1935static int ecp_mod_p384( mpi *N )
1936{
1937 INIT( 384 );
1938
1939 ADD( 12 ); ADD( 21 ); ADD( 20 );
1940 SUB( 23 ); NEXT; // A0
1941
1942 ADD( 13 ); ADD( 22 ); ADD( 23 );
1943 SUB( 12 ); SUB( 20 ); NEXT; // A2
1944
1945 ADD( 14 ); ADD( 23 );
1946 SUB( 13 ); SUB( 21 ); NEXT; // A2
1947
1948 ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
1949 SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3
1950
1951 ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
1952 SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4
1953
1954 ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
1955 SUB( 16 ); NEXT; // A5
1956
1957 ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
1958 SUB( 17 ); NEXT; // A6
1959
1960 ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
1961 SUB( 18 ); NEXT; // A7
1962
1963 ADD( 20 ); ADD( 17 ); ADD( 16 );
1964 SUB( 19 ); NEXT; // A8
1965
1966 ADD( 21 ); ADD( 18 ); ADD( 17 );
1967 SUB( 20 ); NEXT; // A9
1968
1969 ADD( 22 ); ADD( 19 ); ADD( 18 );
1970 SUB( 21 ); NEXT; // A10
1971
1972 ADD( 23 ); ADD( 20 ); ADD( 19 );
1973 SUB( 22 ); LAST; // A11
1974
1975cleanup:
1976 return( ret );
1977}
1978#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1979
1980#undef A
1981#undef LOAD32
1982#undef STORE32
1983#undef MAX32
1984#undef INIT
1985#undef NEXT
1986#undef LAST
1987
1988#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
1989 POLARSSL_ECP_DP_SECP256R1_ENABLED ||
1990 POLARSSL_ECP_DP_SECP384R1_ENABLED */
1991
1992#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1993/*
1994 * Here we have an actual Mersenne prime, so things are more straightforward.
1995 * However, chunks are aligned on a 'weird' boundary (521 bits).
1996 */
1997
1998/* Size of p521 in terms of t_uint */
1999#define P521_WIDTH ( 521 / 8 / sizeof( t_uint ) + 1 )
2000
2001/* Bits to keep in the most significant t_uint */
2002#if defined(POLARSSL_HAVE_INT8)
2003#define P521_MASK 0x01
2004#else
2005#define P521_MASK 0x01FF
2006#endif
2007
2008/*
2009 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
2010 * Write N as A1 + 2^521 A0, return A0 + A1
2011 */
2012static int ecp_mod_p521( mpi *N )
2013{
2014 int ret;
2015 size_t i;
2016 mpi M;
2017 t_uint Mp[P521_WIDTH + 1];
2018 /* Worst case for the size of M is when t_uint is 16 bits:
2019 * we need to hold bits 513 to 1056, which is 34 limbs, that is
2020 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
2021
2022 if( N->n < P521_WIDTH )
2023 return( 0 );
2024
2025 /* M = A1 */
2026 M.s = 1;
2027 M.n = N->n - ( P521_WIDTH - 1 );
2028 if( M.n > P521_WIDTH + 1 )
2029 M.n = P521_WIDTH + 1;
2030 M.p = Mp;
2031 memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) );
2032 MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
2033
2034 /* N = A0 */
2035 N->p[P521_WIDTH - 1] &= P521_MASK;
2036 for( i = P521_WIDTH; i < N->n; i++ )
2037 N->p[i] = 0;
2038
2039 /* N = A0 + A1 */
2040 MPI_CHK( mpi_add_abs( N, N, &M ) );
2041
2042cleanup:
2043 return( ret );
2044}
2045
2046#undef P521_WIDTH
2047#undef P521_MASK
2048#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
2049
2050#endif /* POLARSSL_ECP_NIST_OPTIM */
2051
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002052#if defined(POLARSSL_SELF_TEST)
2053
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002054/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002055 * Checkup routine
2056 */
2057int ecp_self_test( int verbose )
2058{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002059 int ret;
2060 size_t i;
2061 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002062 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002063 mpi m;
2064 unsigned long add_c_prev, dbl_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002065 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002066 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002067 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002068 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002069 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002070 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002071 "400000000000000000000000000000000000000000000000", /* one and zeros */
2072 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2073 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002074 };
2075
2076 ecp_group_init( &grp );
2077 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002078 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002079 mpi_init( &m );
2080
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002081 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002082#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002083 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002084#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002085 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
2086#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002087
2088 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002089 printf( " ECP test #1 (constant op_count, base point G): " );
2090
2091 /* Do a dummy multiplication first to trigger precomputation */
2092 MPI_CHK( mpi_lset( &m, 2 ) );
2093 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002094
2095 add_count = 0;
2096 dbl_count = 0;
2097 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002098 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002099
2100 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2101 {
2102 add_c_prev = add_count;
2103 dbl_c_prev = dbl_count;
2104 add_count = 0;
2105 dbl_count = 0;
2106
2107 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002108 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002109
2110 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2111 {
2112 if( verbose != 0 )
2113 printf( "failed (%zu)\n", i );
2114
2115 ret = 1;
2116 goto cleanup;
2117 }
2118 }
2119
2120 if( verbose != 0 )
2121 printf( "passed\n" );
2122
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002123 if( verbose != 0 )
2124 printf( " ECP test #2 (constant op_count, other point): " );
2125 /* We computed P = 2G last time, use it */
2126
2127 add_count = 0;
2128 dbl_count = 0;
2129 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2130 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2131
2132 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2133 {
2134 add_c_prev = add_count;
2135 dbl_c_prev = dbl_count;
2136 add_count = 0;
2137 dbl_count = 0;
2138
2139 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2140 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2141
2142 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2143 {
2144 if( verbose != 0 )
2145 printf( "failed (%zu)\n", i );
2146
2147 ret = 1;
2148 goto cleanup;
2149 }
2150 }
2151
2152 if( verbose != 0 )
2153 printf( "passed\n" );
2154
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002155cleanup:
2156
2157 if( ret < 0 && verbose != 0 )
2158 printf( "Unexpected error, return code = %08X\n", ret );
2159
2160 ecp_group_free( &grp );
2161 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002162 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002163 mpi_free( &m );
2164
2165 if( verbose != 0 )
2166 printf( "\n" );
2167
2168 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002169}
2170
2171#endif
2172
2173#endif