blob: f0dd7d285c9a0265d6ed05be77564de194cb13c4 [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é-Gonnardcae6f3e2013-10-23 20:19:57 +02001070 * Addition or subtraction: R = P + Q or R = P - Q,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001071 * mixed affine-Jacobian coordinates (GECC 3.22)
1072 *
1073 * The coordinates of Q must be normalized (= affine),
1074 * but those of P don't need to. R is not normalized.
1075 *
1076 * If sign >= 0, perform addition, otherwise perform subtraction,
1077 * taking advantage of the fact that, for Q != 0, we have
1078 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001079 *
1080 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001081 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001082static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001083 const ecp_point *P, const ecp_point *Q,
1084 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001085{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001086 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001087 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001088
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001089#if defined(POLARSSL_SELF_TEST)
1090 add_count++;
1091#endif
1092
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001093 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001094 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001095 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001096 * This will never happen during ecp_mul() so we don't mind the branches.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001097 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001098 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1099 return( ecp_copy( R, P ) );
1100
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001101 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1102 {
1103 ret = ecp_copy( R, Q );
1104
1105 /*
1106 * -R.Y mod P = P - R.Y unless R.Y == 0
1107 */
1108 if( ret == 0 && sign < 0)
1109 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1110 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1111
1112 return( ret );
1113 }
1114
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001115 /*
1116 * Make sure Q coordinates are normalized
1117 */
1118 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001119 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001120
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001121 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1122 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001123
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001124 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1125 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1126 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1127 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001128
1129 /*
1130 * For subtraction, -Q.Y should have been used instead of Q.Y,
1131 * so we replace T2 by -T2, which is P - T2 mod P
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001132 * (Again, not used by ecp_mul(), so not worry about the branch.)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001133 */
1134 if( sign < 0 )
1135 {
1136 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1137 MOD_SUB( T2 );
1138 }
1139
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001140 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1141 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001142
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001143 /* TODO: make sure it never happens during ecp_mul() */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001144 if( mpi_cmp_int( &T1, 0 ) == 0 )
1145 {
1146 if( mpi_cmp_int( &T2, 0 ) == 0 )
1147 {
1148 ret = ecp_double_jac( grp, R, P );
1149 goto cleanup;
1150 }
1151 else
1152 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001153 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001154 goto cleanup;
1155 }
1156 }
1157
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001158 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1159 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1160 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1161 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1162 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1163 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1164 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1165 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1166 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1167 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1168 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1169 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001170
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001171 MPI_CHK( mpi_copy( &R->X, &X ) );
1172 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1173 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001174
1175cleanup:
1176
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001177 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1178 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001179
1180 return( ret );
1181}
1182
1183/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001184 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001185 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001186 */
1187int ecp_add( const ecp_group *grp, ecp_point *R,
1188 const ecp_point *P, const ecp_point *Q )
1189{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001190 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001191
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001192 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1193 MPI_CHK( ecp_normalize( grp, R ) );
1194
1195cleanup:
1196 return( ret );
1197}
1198
1199/*
1200 * Subtraction: R = P - Q, result's coordinates normalized
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001201 * Cost: 1A + 1N = 1I + 11M + 4S
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001202 */
1203int ecp_sub( const ecp_group *grp, ecp_point *R,
1204 const ecp_point *P, const ecp_point *Q )
1205{
1206 int ret;
1207
1208 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001209 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001210
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001211cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001212 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001213}
1214
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001215/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001216 * Randomize jacobian coordinates:
1217 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1218 * This is sort of the reverse operation of ecp_normalize().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001219 *
1220 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001221 */
1222static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1223 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1224{
1225 int ret;
1226 mpi l, ll;
1227 size_t p_size = (grp->pbits + 7) / 8;
1228 int count = 0;
1229
1230 mpi_init( &l ); mpi_init( &ll );
1231
1232 /* Generate l such that 1 < l < p */
1233 do
1234 {
1235 mpi_fill_random( &l, p_size, f_rng, p_rng );
1236
1237 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1238 mpi_shift_r( &l, 1 );
1239
1240 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001241 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001242 }
1243 while( mpi_cmp_int( &l, 1 ) <= 0 );
1244
1245 /* Z = l * Z */
1246 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1247
1248 /* X = l^2 * X */
1249 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1250 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1251
1252 /* Y = l^3 * Y */
1253 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1254 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1255
1256cleanup:
1257 mpi_free( &l ); mpi_free( &ll );
1258
1259 return( ret );
1260}
1261
1262/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001263 * Check and define parameters used by the comb method (see below for details)
1264 */
1265#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1266#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1267#endif
1268
1269/* d = ceil( n / w ) */
1270#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1271
1272/* number of precomputed points */
1273#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1274
1275/*
1276 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001277 *
1278 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001279 * modified version that provides resistance to SPA by avoiding zero
1280 * digits in the representation as in [3]. We modify the method further by
1281 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001282 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001283 *
1284 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1285 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1286 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001287 *
1288 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001289 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001290 * - w is the size, ie number of teeth, of the comb, and must be between
1291 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001292 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1293 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001294 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001295static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001296 unsigned char w, const mpi *m )
1297{
1298 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001299 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001300
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001301 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001302
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001303 /* First get the classical comb values (except for x_d = 0) */
1304 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001305 for( j = 0; j < w; j++ )
1306 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1307
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001308 /* Now make sure x_1 .. x_d are odd */
1309 c = 0;
1310 for( i = 1; i <= d; i++ )
1311 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001312 /* Add carry and update it */
1313 cc = x[i] & c;
1314 x[i] = x[i] ^ c;
1315 c = cc;
1316
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001317 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001318 adjust = 1 - ( x[i] & 0x01 );
1319 c |= x[i] & ( x[i-1] * adjust );
1320 x[i] = x[i] ^ ( x[i-1] * adjust );
1321 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001322 }
1323}
1324
1325/*
1326 * Precompute points for the comb method
1327 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001328 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1329 * 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 +01001330 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001331 * T must be able to hold 2^{w - 1} elements
1332 *
1333 * 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 +01001334 */
1335static int ecp_precompute_comb( const ecp_group *grp,
1336 ecp_point T[], const ecp_point *P,
1337 unsigned char w, size_t d )
1338{
1339 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001340 unsigned char i, k;
1341 size_t j;
1342 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001343
1344 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001345 * Set T[0] = P and
1346 * 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 +01001347 */
1348 MPI_CHK( ecp_copy( &T[0], P ) );
1349
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001350 k = 0;
1351 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001352 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001353 cur = T + i;
1354 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001355 for( j = 0; j < d; j++ )
1356 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001357
1358 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001359 }
1360
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001361 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001362
1363 /*
1364 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001365 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001366 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001367 k = 0;
1368 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001369 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001370 j = i;
1371 while( j-- )
1372 {
1373 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i], +1 );
1374 TT[k++] = &T[i + j];
1375 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001376 }
1377
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001378 ecp_normalize_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001379
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001380 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001381 * Post-precessing: reclaim some memory by
1382 * - not storing Z (always 1)
1383 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001384 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001385 */
1386 for( i = 0; i < ( 1U << (w-1) ); i++ )
1387 {
1388 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001389 mpi_shrink( &T[i].X, grp->P.n );
1390 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001391 }
1392
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001393cleanup:
1394 return( ret );
1395}
1396
1397/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001398 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001399 */
1400static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001401 const ecp_point T[], unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001402{
1403 int ret;
1404
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001405 /* Ignore the "sign" bit */
1406 MPI_CHK( ecp_copy( R, &T[ ( i & 0x7Fu ) >> 1 ] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001407
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001408 /* Restore the Z coordinate */
1409 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1410
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001411 /* Safely invert result if i is "negative" */
1412 MPI_CHK( ecp_safe_invert( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001413
1414cleanup:
1415 return( ret );
1416}
1417
1418/*
1419 * Core multiplication algorithm for the (modified) comb method.
1420 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001421 *
1422 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001423 */
1424static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001425 const ecp_point T[],
1426 const unsigned char x[], size_t d,
1427 int (*f_rng)(void *, unsigned char *, size_t),
1428 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001429{
1430 int ret;
1431 ecp_point Txi;
1432 size_t i;
1433
1434 ecp_point_init( &Txi );
1435
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001436 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001437 i = d;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001438 MPI_CHK( ecp_select_comb( grp, R, T, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001439 if( f_rng != 0 )
1440 MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001441
1442 while( i-- != 0 )
1443 {
1444 MPI_CHK( ecp_double_jac( grp, R, R ) );
1445 MPI_CHK( ecp_select_comb( grp, &Txi, T, x[i] ) );
1446 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi, +1 ) );
1447 }
1448
1449cleanup:
1450 ecp_point_free( &Txi );
1451
1452 return( ret );
1453}
1454
1455/*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001456 * Multiplication using the comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001457 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +01001458int ecp_mul( ecp_group *grp, ecp_point *R,
1459 const mpi *m, const ecp_point *P,
1460 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001461{
1462 int ret;
1463 unsigned char w, m_is_odd, p_eq_g;
1464 size_t pre_len, d, i;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001465 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001466 ecp_point Q, *T = NULL, S[2];
1467 mpi M;
1468
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001469 /*
1470 * Sanity checks (before we even initialize anything)
1471 */
1472 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 )
1473 return( ret );
1474
1475 /* We'll need this later, but do it now to possibly avoid cheking P */
1476 p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 &&
1477 mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1478 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
1479 if( ! p_eq_g && ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1480 return( ret );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001481
1482 mpi_init( &M );
1483 ecp_point_init( &Q );
1484 ecp_point_init( &S[0] );
1485 ecp_point_init( &S[1] );
1486
1487 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001488 * Minimize the number of multiplications, that is minimize
1489 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w
1490 * (see costs of the various parts, with 1S = 1M)
1491 */
1492 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001493
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001494 /*
1495 * If P == G, pre-compute a bit more, since this may be re-used later.
1496 * Just adding one ups the cost of the first mul by at most 3%.
1497 */
1498 if( p_eq_g )
1499 w++;
1500
1501 /*
1502 * Make sure w is within limits.
1503 * (The last test is useful only for very small curves in the test suite.)
1504 */
1505 if( w > POLARSSL_ECP_WINDOW_SIZE )
1506 w = POLARSSL_ECP_WINDOW_SIZE;
1507 if( w < 2 || w >= grp->nbits )
1508 w = 2;
1509
1510 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001511 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001512 d = ( grp->nbits + w - 1 ) / w;
1513
1514 /*
1515 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001516 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001517 */
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001518 if( p_eq_g )
1519 T = grp->T;
1520
1521 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001522 {
1523 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1524 if( T == NULL )
1525 {
1526 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1527 goto cleanup;
1528 }
1529
1530 for( i = 0; i < pre_len; i++ )
1531 ecp_point_init( &T[i] );
1532
1533 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1534
1535 if( p_eq_g )
1536 {
1537 grp->T = T;
1538 grp->T_size = pre_len;
1539 }
1540 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001541
1542 /*
1543 * Make sure M is odd (M = m + 1 or M = m + 2)
1544 * later we'll get m * P by subtracting P or 2 * P to M * P.
1545 */
1546 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1547
1548 MPI_CHK( mpi_copy( &M, m ) );
1549 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
1550
1551 /*
1552 * Go for comb multiplication, Q = M * P
1553 */
1554 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001555 ecp_mul_comb_core( grp, &Q, T, k, d, f_rng, p_rng );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001556
1557 /*
1558 * Now get m * P from M * P
1559 */
1560 MPI_CHK( ecp_copy( &S[0], P ) );
1561 MPI_CHK( ecp_add( grp, &S[1], P, P ) );
1562 MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) );
1563
1564cleanup:
1565
1566 if( T != NULL && ! p_eq_g )
1567 {
1568 for( i = 0; i < pre_len; i++ )
1569 ecp_point_free( &T[i] );
1570 polarssl_free( T );
1571 }
1572
1573 ecp_point_free( &S[1] );
1574 ecp_point_free( &S[0] );
1575 ecp_point_free( &Q );
1576 mpi_free( &M );
1577
1578 return( ret );
1579}
1580
1581/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001582 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1583 */
1584int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1585{
1586 int ret;
1587 mpi YY, RHS;
1588
1589 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001590 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001591
1592 /*
1593 * pt coordinates must be normalized for our checks
1594 */
1595 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001596 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001597
1598 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1599 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1600 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1601 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001602 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001603
1604 mpi_init( &YY ); mpi_init( &RHS );
1605
1606 /*
1607 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001608 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001609 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001610 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1611 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001612 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001613 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1614 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001615
1616 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001617 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001618
1619cleanup:
1620
1621 mpi_free( &YY ); mpi_free( &RHS );
1622
1623 return( ret );
1624}
1625
1626/*
1627 * Check that an mpi is valid as a private key (SEC1 3.2)
1628 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001629int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001630{
1631 /* We want 1 <= d <= N-1 */
1632 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001633 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001634
1635 return( 0 );
1636}
1637
1638/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001639 * Generate a keypair (SEC1 3.2.1)
1640 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001641int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001642 int (*f_rng)(void *, unsigned char *, size_t),
1643 void *p_rng )
1644{
1645 int count = 0;
1646 size_t n_size = (grp->nbits + 7) / 8;
1647
1648 /*
1649 * Generate d such that 1 <= n < N
1650 */
1651 do
1652 {
1653 mpi_fill_random( d, n_size, f_rng, p_rng );
1654
1655 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1656 mpi_shift_r( d, 1 );
1657
1658 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001659 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001660 }
1661 while( mpi_cmp_int( d, 1 ) < 0 );
1662
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001663 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001664}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001665
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001666#if defined(POLARSSL_ECP_NIST_OPTIM)
Manuel Pégourié-Gonnard9fcceac2013-10-23 20:56:12 +02001667/*
1668 * Fast reduction modulo the primes used by the NIST curves.
1669 *
1670 * These functions are: critical for speed, but not need for correct
1671 * operations. So, we make the choice to heavily rely on the internals of our
1672 * bignum library, which creates a tight coupling between these functions and
1673 * our MPI implementation. However, the coupling between the ECP module and
1674 * MPI remains loose, since these functions can be deactivated at will.
1675 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001676
1677#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1678/*
1679 * Compared to the way things are presented in FIPS 186-3 D.2,
1680 * we proceed in columns, from right (least significant chunk) to left,
1681 * adding chunks to N in place, and keeping a carry for the next chunk.
1682 * This avoids moving things around in memory, and uselessly adding zeros,
1683 * compared to the more straightforward, line-oriented approach.
1684 *
1685 * For this prime we need to handle data in chunks of 64 bits.
1686 * Since this is always a multiple of our basic t_uint, we can
1687 * use a t_uint * to designate such a chunk, and small loops to handle it.
1688 */
1689
1690/* Add 64-bit chunks (dst += src) and update carry */
1691static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
1692{
1693 unsigned char i;
1694 t_uint c = 0;
1695 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
1696 {
1697 *dst += c; c = ( *dst < c );
1698 *dst += *src; c += ( *dst < *src );
1699 }
1700 *carry += c;
1701}
1702
1703/* Add carry to a 64-bit chunk and update carry */
1704static inline void carry64( t_uint *dst, t_uint *carry )
1705{
1706 unsigned char i;
1707 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
1708 {
1709 *dst += *carry;
1710 *carry = ( *dst < *carry );
1711 }
1712}
1713
1714#define WIDTH 8 / sizeof( t_uint )
1715#define A( i ) N->p + i * WIDTH
1716#define ADD( i ) add64( p, A( i ), &c )
1717#define NEXT p += WIDTH; carry64( p, &c )
1718#define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
1719
1720/*
1721 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
1722 */
1723static int ecp_mod_p192( mpi *N )
1724{
1725 int ret;
1726 t_uint c = 0;
1727 t_uint *p, *end;
1728
1729 /* Make sure we have enough blocks so that A(5) is legal */
1730 MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
1731
1732 p = N->p;
1733 end = p + N->n;
1734
1735 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
1736 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
1737 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
1738
1739cleanup:
1740 return( ret );
1741}
1742
1743#undef WIDTH
1744#undef A
1745#undef ADD
1746#undef NEXT
1747#undef LAST
1748#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
1749
1750#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
1751 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
1752 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1753/*
1754 * The reader is advised to first understand ecp_mod_p192() since the same
1755 * general structure is used here, but with additional complications:
1756 * (1) chunks of 32 bits, and (2) subtractions.
1757 */
1758
1759/*
1760 * For these primes, we need to handle data in chunks of 32 bits.
1761 * This makes it more complicated if we use 64 bits limbs in MPI,
1762 * which prevents us from using a uniform access method as for p192.
1763 *
1764 * So, we define a mini abstraction layer to access 32 bit chunks,
1765 * load them in 'cur' for work, and store them back from 'cur' when done.
1766 *
1767 * While at it, also define the size of N in terms of 32-bit chunks.
1768 */
1769#define LOAD32 cur = A( i );
1770
1771#if defined(POLARSSL_HAVE_INT8) /* 8 bit */
1772
1773#define MAX32 N->n / 4
1774#define A( j ) (uint32_t)( N->p[4*j+0] ) | \
1775 ( N->p[4*j+1] << 8 ) | \
1776 ( N->p[4*j+2] << 16 ) | \
1777 ( N->p[4*j+3] << 24 )
1778#define STORE32 N->p[4*i+0] = (uint8_t)( cur ); \
1779 N->p[4*i+1] = (uint8_t)( cur >> 8 ); \
1780 N->p[4*i+2] = (uint8_t)( cur >> 16 ); \
1781 N->p[4*i+3] = (uint8_t)( cur >> 24 );
1782
1783#elif defined(POLARSSL_HAVE_INT16) /* 16 bit */
1784
1785#define MAX32 N->n / 2
1786#define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
1787#define STORE32 N->p[2*i+0] = (uint16_t)( cur ); \
1788 N->p[2*i+1] = (uint16_t)( cur >> 16 );
1789
1790#elif defined(POLARSSL_HAVE_INT32) /* 32 bit */
1791
1792#define MAX32 N->n
1793#define A( j ) N->p[j]
1794#define STORE32 N->p[i] = cur;
1795
1796#else /* 64-bit */
1797
1798#define MAX32 N->n * 2
1799#define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
1800#define STORE32 \
1801 if( i % 2 ) { \
1802 N->p[i/2] &= 0x00000000FFFFFFFF; \
1803 N->p[i/2] |= ((uint64_t) cur) << 32; \
1804 } else { \
1805 N->p[i/2] &= 0xFFFFFFFF00000000; \
1806 N->p[i/2] |= (uint64_t) cur; \
1807 }
1808
1809#endif /* sizeof( t_uint ) */
1810
1811/*
1812 * Helpers for addition and subtraction of chunks, with signed carry.
1813 */
1814static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
1815{
1816 *dst += src;
1817 *carry += ( *dst < src );
1818}
1819
1820static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
1821{
1822 *carry -= ( *dst < src );
1823 *dst -= src;
1824}
1825
1826#define ADD( j ) add32( &cur, A( j ), &c );
1827#define SUB( j ) sub32( &cur, A( j ), &c );
1828
1829/*
1830 * Helpers for the main 'loop'
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001831 * (see fix_negative for the motivation of C)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001832 */
1833#define INIT( b ) \
1834 int ret; \
1835 signed char c = 0, cc; \
1836 uint32_t cur; \
1837 size_t i = 0, bits = b; \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001838 mpi C; \
1839 t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \
1840 \
1841 C.s = 1; \
1842 C.n = b / 8 / sizeof( t_uint) + 1; \
1843 C.p = Cp; \
1844 memset( Cp, 0, C.n * sizeof( t_uint ) ); \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001845 \
1846 MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
1847 LOAD32;
1848
1849#define NEXT \
1850 STORE32; i++; LOAD32; \
1851 cc = c; c = 0; \
1852 if( cc < 0 ) \
1853 sub32( &cur, -cc, &c ); \
1854 else \
1855 add32( &cur, cc, &c ); \
1856
1857#define LAST \
1858 STORE32; i++; \
1859 cur = c > 0 ? c : 0; STORE32; \
1860 cur = 0; while( ++i < MAX32 ) { STORE32; } \
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001861 if( c < 0 ) fix_negative( N, c, &C, bits );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001862
1863/*
1864 * If the result is negative, we get it in the form
1865 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
1866 */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001867static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001868{
1869 int ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001870
1871 /* C = - c * 2^(bits + 32) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001872#if !defined(POLARSSL_HAVE_INT64)
1873 ((void) bits);
1874#else
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001875 if( bits == 224 )
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001876 C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001877 else
1878#endif
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001879 C->p[ C->n - 1 ] = (t_uint) -c;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001880
1881 /* N = - ( C - N ) */
Manuel Pégourié-Gonnardb21c81f2013-10-23 20:45:04 +02001882 MPI_CHK( mpi_sub_abs( N, C, N ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001883 N->s = -1;
1884
1885cleanup:
1886
1887 return( ret );
1888}
1889
1890#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1891/*
1892 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
1893 */
1894static int ecp_mod_p224( mpi *N )
1895{
1896 INIT( 224 );
1897
1898 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
1899 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
1900 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
1901 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
1902 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
1903 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
1904 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
1905
1906cleanup:
1907 return( ret );
1908}
1909#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1910
1911#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1912/*
1913 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
1914 */
1915static int ecp_mod_p256( mpi *N )
1916{
1917 INIT( 256 );
1918
1919 ADD( 8 ); ADD( 9 );
1920 SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0
1921
1922 ADD( 9 ); ADD( 10 );
1923 SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1
1924
1925 ADD( 10 ); ADD( 11 );
1926 SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2
1927
1928 ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
1929 SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3
1930
1931 ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
1932 SUB( 9 ); SUB( 10 ); NEXT; // A4
1933
1934 ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
1935 SUB( 10 ); SUB( 11 ); NEXT; // A5
1936
1937 ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
1938 SUB( 8 ); SUB( 9 ); NEXT; // A6
1939
1940 ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
1941 SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7
1942
1943cleanup:
1944 return( ret );
1945}
1946#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1947
1948#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1949/*
1950 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
1951 */
1952static int ecp_mod_p384( mpi *N )
1953{
1954 INIT( 384 );
1955
1956 ADD( 12 ); ADD( 21 ); ADD( 20 );
1957 SUB( 23 ); NEXT; // A0
1958
1959 ADD( 13 ); ADD( 22 ); ADD( 23 );
1960 SUB( 12 ); SUB( 20 ); NEXT; // A2
1961
1962 ADD( 14 ); ADD( 23 );
1963 SUB( 13 ); SUB( 21 ); NEXT; // A2
1964
1965 ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
1966 SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3
1967
1968 ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
1969 SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4
1970
1971 ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
1972 SUB( 16 ); NEXT; // A5
1973
1974 ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
1975 SUB( 17 ); NEXT; // A6
1976
1977 ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
1978 SUB( 18 ); NEXT; // A7
1979
1980 ADD( 20 ); ADD( 17 ); ADD( 16 );
1981 SUB( 19 ); NEXT; // A8
1982
1983 ADD( 21 ); ADD( 18 ); ADD( 17 );
1984 SUB( 20 ); NEXT; // A9
1985
1986 ADD( 22 ); ADD( 19 ); ADD( 18 );
1987 SUB( 21 ); NEXT; // A10
1988
1989 ADD( 23 ); ADD( 20 ); ADD( 19 );
1990 SUB( 22 ); LAST; // A11
1991
1992cleanup:
1993 return( ret );
1994}
1995#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1996
1997#undef A
1998#undef LOAD32
1999#undef STORE32
2000#undef MAX32
2001#undef INIT
2002#undef NEXT
2003#undef LAST
2004
2005#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
2006 POLARSSL_ECP_DP_SECP256R1_ENABLED ||
2007 POLARSSL_ECP_DP_SECP384R1_ENABLED */
2008
2009#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
2010/*
2011 * Here we have an actual Mersenne prime, so things are more straightforward.
2012 * However, chunks are aligned on a 'weird' boundary (521 bits).
2013 */
2014
2015/* Size of p521 in terms of t_uint */
2016#define P521_WIDTH ( 521 / 8 / sizeof( t_uint ) + 1 )
2017
2018/* Bits to keep in the most significant t_uint */
2019#if defined(POLARSSL_HAVE_INT8)
2020#define P521_MASK 0x01
2021#else
2022#define P521_MASK 0x01FF
2023#endif
2024
2025/*
2026 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
2027 * Write N as A1 + 2^521 A0, return A0 + A1
2028 */
2029static int ecp_mod_p521( mpi *N )
2030{
2031 int ret;
2032 size_t i;
2033 mpi M;
2034 t_uint Mp[P521_WIDTH + 1];
2035 /* Worst case for the size of M is when t_uint is 16 bits:
2036 * we need to hold bits 513 to 1056, which is 34 limbs, that is
2037 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
2038
2039 if( N->n < P521_WIDTH )
2040 return( 0 );
2041
2042 /* M = A1 */
2043 M.s = 1;
2044 M.n = N->n - ( P521_WIDTH - 1 );
2045 if( M.n > P521_WIDTH + 1 )
2046 M.n = P521_WIDTH + 1;
2047 M.p = Mp;
2048 memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) );
2049 MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
2050
2051 /* N = A0 */
2052 N->p[P521_WIDTH - 1] &= P521_MASK;
2053 for( i = P521_WIDTH; i < N->n; i++ )
2054 N->p[i] = 0;
2055
2056 /* N = A0 + A1 */
2057 MPI_CHK( mpi_add_abs( N, N, &M ) );
2058
2059cleanup:
2060 return( ret );
2061}
2062
2063#undef P521_WIDTH
2064#undef P521_MASK
2065#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
2066
2067#endif /* POLARSSL_ECP_NIST_OPTIM */
2068
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002069#if defined(POLARSSL_SELF_TEST)
2070
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002071/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002072 * Checkup routine
2073 */
2074int ecp_self_test( int verbose )
2075{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002076 int ret;
2077 size_t i;
2078 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002079 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002080 mpi m;
2081 unsigned long add_c_prev, dbl_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002082 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002083 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002084 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002085 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002086 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002087 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002088 "400000000000000000000000000000000000000000000000", /* one and zeros */
2089 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2090 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002091 };
2092
2093 ecp_group_init( &grp );
2094 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002095 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002096 mpi_init( &m );
2097
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002098 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002099#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002100 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002101#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002102 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
2103#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002104
2105 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002106 printf( " ECP test #1 (constant op_count, base point G): " );
2107
2108 /* Do a dummy multiplication first to trigger precomputation */
2109 MPI_CHK( mpi_lset( &m, 2 ) );
2110 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002111
2112 add_count = 0;
2113 dbl_count = 0;
2114 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002115 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002116
2117 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2118 {
2119 add_c_prev = add_count;
2120 dbl_c_prev = dbl_count;
2121 add_count = 0;
2122 dbl_count = 0;
2123
2124 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002125 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002126
2127 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2128 {
2129 if( verbose != 0 )
2130 printf( "failed (%zu)\n", i );
2131
2132 ret = 1;
2133 goto cleanup;
2134 }
2135 }
2136
2137 if( verbose != 0 )
2138 printf( "passed\n" );
2139
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002140 if( verbose != 0 )
2141 printf( " ECP test #2 (constant op_count, other point): " );
2142 /* We computed P = 2G last time, use it */
2143
2144 add_count = 0;
2145 dbl_count = 0;
2146 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2147 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2148
2149 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2150 {
2151 add_c_prev = add_count;
2152 dbl_c_prev = dbl_count;
2153 add_count = 0;
2154 dbl_count = 0;
2155
2156 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2157 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2158
2159 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
2160 {
2161 if( verbose != 0 )
2162 printf( "failed (%zu)\n", i );
2163
2164 ret = 1;
2165 goto cleanup;
2166 }
2167 }
2168
2169 if( verbose != 0 )
2170 printf( "passed\n" );
2171
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002172cleanup:
2173
2174 if( ret < 0 && verbose != 0 )
2175 printf( "Unexpected error, return code = %08X\n", ret );
2176
2177 ecp_group_free( &grp );
2178 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002179 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002180 mpi_free( &m );
2181
2182 if( verbose != 0 )
2183 printf( "\n" );
2184
2185 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002186}
2187
2188#endif
2189
2190#endif