blob: 21a2315053fa42193ce98c213d314ef440f1076d [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 *
34 * [1] OKEYA, Katsuyuki and TAKAGI, Tsuyoshi. The width-w NAF method provides
35 * small memory and fast elliptic scalar multiplications secure against
36 * side channel attacks. In : Topics in Cryptology—CT-RSA 2003. Springer
37 * Berlin Heidelberg, 2003. p. 328-343.
38 * <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
39 *
40 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
41 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
42 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
43 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010044 */
45
46#include "polarssl/config.h"
47
48#if defined(POLARSSL_ECP_C)
49
50#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051
52#if defined(POLARSSL_MEMORY_C)
53#include "polarssl/memory.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010059#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010060#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010062#if defined(POLARSSL_SELF_TEST)
63/*
64 * Counts of point addition and doubling operations.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020065 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010066 */
67unsigned long add_count, dbl_count;
68#endif
69
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010070/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020071 * List of supported curves:
72 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020073 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020074 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020075 * - readable name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020076 */
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +020077const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020078{
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020079#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
80 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpool512r1" },
81#endif
82#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
83 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpool384r1" },
84#endif
85#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
86 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpool256r1" },
87#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020088#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020089 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020090#endif
91#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020092 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020093#endif
94#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020095 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020096#endif
97#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020098 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020099#endif
100#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200101 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200102#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200103 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200104};
105
106/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200107 * List of supported curves and associated info
108 */
109const ecp_curve_info *ecp_curve_list( void )
110{
111 return ecp_supported_curves;
112}
113
114/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100115 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100116 */
117void ecp_point_init( ecp_point *pt )
118{
119 if( pt == NULL )
120 return;
121
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100122 mpi_init( &pt->X );
123 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100124 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100125}
126
127/*
128 * Initialize (the components of) a group
129 */
130void ecp_group_init( ecp_group *grp )
131{
132 if( grp == NULL )
133 return;
134
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200135 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100136}
137
138/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200139 * Initialize (the components of) a key pair
140 */
141void ecp_keypair_init( ecp_keypair *key )
142{
143 if ( key == NULL )
144 return;
145
146 ecp_group_init( &key->grp );
147 mpi_init( &key->d );
148 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200149}
150
151/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100152 * Unallocate (the components of) a point
153 */
154void ecp_point_free( ecp_point *pt )
155{
156 if( pt == NULL )
157 return;
158
159 mpi_free( &( pt->X ) );
160 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100161 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100162}
163
164/*
165 * Unallocate (the components of) a group
166 */
167void ecp_group_free( ecp_group *grp )
168{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200169 size_t i;
170
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100171 if( grp == NULL )
172 return;
173
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100174 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200175 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100176 mpi_free( &grp->B );
177 ecp_point_free( &grp->G );
178 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200179
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200180 if( grp->T != NULL )
181 {
182 for( i = 0; i < grp->T_size; i++ )
183 ecp_point_free( &grp->T[i] );
184 polarssl_free( grp->T );
185 }
186
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200187 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100188}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100189
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100190/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200191 * Unallocate (the components of) a key pair
192 */
193void ecp_keypair_free( ecp_keypair *key )
194{
195 if ( key == NULL )
196 return;
197
198 ecp_group_free( &key->grp );
199 mpi_free( &key->d );
200 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200201}
202
203/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100204 * Set point to zero
205 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100206int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100207{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100208 int ret;
209
210 MPI_CHK( mpi_lset( &pt->X , 1 ) );
211 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
212 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
213
214cleanup:
215 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100216}
217
218/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100219 * Tell if a point is zero
220 */
221int ecp_is_zero( ecp_point *pt )
222{
223 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
224}
225
226/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100227 * Copy the contents of Q into P
228 */
229int ecp_copy( ecp_point *P, const ecp_point *Q )
230{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100231 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100232
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100233 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
234 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100235 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100236
237cleanup:
238 return( ret );
239}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100240
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100241/*
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200242 * Copy the contents of a group object
243 */
244int ecp_group_copy( ecp_group *dst, const ecp_group *src )
245{
246 return ecp_use_known_dp( dst, src->id );
247}
248
249/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100250 * Import a non-zero point from ASCII strings
251 */
252int ecp_point_read_string( ecp_point *P, int radix,
253 const char *x, const char *y )
254{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100255 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100256
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100257 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
258 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100259 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100260
261cleanup:
262 return( ret );
263}
264
265/*
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200266 * Import an ECP group from ASCII strings, general case (A used)
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100267 */
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200268static int ecp_group_read_string_gen( ecp_group *grp, int radix,
269 const char *p, const char *a, const char *b,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100270 const char *gx, const char *gy, const char *n)
271{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100272 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100273
274 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200275 MPI_CHK( mpi_read_string( &grp->A, radix, a ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100276 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
277 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
278 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
279
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100280 grp->pbits = mpi_msb( &grp->P );
281 grp->nbits = mpi_msb( &grp->N );
282
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100283cleanup:
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200284 if( ret != 0 )
285 ecp_group_free( grp );
286
287 return( ret );
288}
289
290/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200291 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200292 */
293int ecp_group_read_string( ecp_group *grp, int radix,
294 const char *p, const char *b,
295 const char *gx, const char *gy, const char *n)
296{
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200297 int ret;
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200298
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200299 MPI_CHK( ecp_group_read_string_gen( grp, radix, p, "00", b, gx, gy, n ) );
300 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
301
302cleanup:
303 if( ret != 0 )
304 ecp_group_free( grp );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200305
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100306 return( ret );
307}
308
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100309/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100310 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100311 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100312int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100313 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100314 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100315{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200316 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100317 size_t plen;
318
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100319 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
320 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100321 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100322
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100323 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100324 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100325 */
326 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
327 {
328 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100329 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100330
331 buf[0] = 0x00;
332 *olen = 1;
333
334 return( 0 );
335 }
336
337 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100338
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100339 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
340 {
341 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100342
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100343 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100344 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100345
346 buf[0] = 0x04;
347 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
348 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
349 }
350 else if( format == POLARSSL_ECP_PF_COMPRESSED )
351 {
352 *olen = plen + 1;
353
354 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100355 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100356
357 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
358 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
359 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100360
361cleanup:
362 return( ret );
363}
364
365/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100366 * Import a point from unsigned binary data (SEC1 2.3.4)
367 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100368int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
369 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100370 int ret;
371 size_t plen;
372
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100373 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100374 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100375
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100376 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100377
378 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100379 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100380
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100381 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
382 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
383 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100384
385cleanup:
386 return( ret );
387}
388
389/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100390 * Import a point from a TLS ECPoint record (RFC 4492)
391 * struct {
392 * opaque point <1..2^8-1>;
393 * } ECPoint;
394 */
395int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100396 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100397{
398 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100399 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100400
401 /*
402 * We must have at least two bytes (1 for length, at least of for data)
403 */
404 if( buf_len < 2 )
405 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
406
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100407 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100408 if( data_len < 1 || data_len > buf_len - 1 )
409 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
410
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100411 /*
412 * Save buffer start for read_binary and update buf
413 */
414 buf_start = *buf;
415 *buf += data_len;
416
417 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100418}
419
420/*
421 * Export a point as a TLS ECPoint record (RFC 4492)
422 * struct {
423 * opaque point <1..2^8-1>;
424 * } ECPoint;
425 */
426int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100427 int format, size_t *olen,
428 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100429{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100430 int ret;
431
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100432 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100433 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100434 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100435 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100436 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
437
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100438 if( ( ret = ecp_point_write_binary( grp, pt, format,
439 olen, buf + 1, blen - 1) ) != 0 )
440 return( ret );
441
442 /*
443 * write length to the first byte and update total length
444 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200445 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100446 ++*olen;
447
448 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100449}
450
451/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100452 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
453 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200454 *
455 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100456 */
457static int ecp_modp( mpi *N, const ecp_group *grp )
458{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100459 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100460
461 if( grp->modp == NULL )
462 return( mpi_mod_mpi( N, N, &grp->P ) );
463
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200464 /* N->s < 0 is a much faster test, which fails only if N is 0 */
465 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
466 mpi_msb( N ) > 2 * grp->pbits )
467 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200468 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200469 }
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100470
471 MPI_CHK( grp->modp( N ) );
472
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200473 /* N->s < 0 is a much faster test, which fails only if N is 0 */
474 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100475 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
476
477 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200478 /* we known P, N and the result are positive */
479 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100480
481cleanup:
482 return( ret );
483}
484
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200485#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100486
Manuel Pégourié-Gonnardd1e7a452013-10-22 21:03:16 +0200487/* Add 64-bit chunks (dst += src) and update carry */
488static inline void add_64( t_uint *dst, t_uint *src, t_uint *carry )
489{
490 unsigned char i;
491 t_uint c = 0;
492 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
493 {
494 *dst += c; c = ( *dst < c );
495 *dst += *src; c += ( *dst < *src );
496 }
497 *carry += c;
498}
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100499
Manuel Pégourié-Gonnardd1e7a452013-10-22 21:03:16 +0200500/* Add carry to a 64-bit chunk and update carry */
501static inline void carry64( t_uint *dst, t_uint *carry )
502{
503 unsigned char i;
504 for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
505 {
506 *dst += *carry;
507 *carry = ( *dst < *carry );
508 }
509}
510
511#define OFFSET ( 8 / sizeof( t_uint ) )
512#define A( i ) ( N->p + ( i ) * OFFSET )
513#define ADD( i ) add_64( p, A( i ), &c )
514#define NEXT p += OFFSET; carry64( p, &c )
515#define LAST p += OFFSET; *p = c; while( ++p < end ) *p = 0
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100516
517/*
518 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
519 */
520static int ecp_mod_p192( mpi *N )
521{
522 int ret;
Manuel Pégourié-Gonnardd1e7a452013-10-22 21:03:16 +0200523 t_uint c = 0;
524 t_uint *p, *end;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100525
Manuel Pégourié-Gonnardd1e7a452013-10-22 21:03:16 +0200526 /* Make sure we have the correct number of blocks */
527 MPI_CHK( mpi_grow( N, 6 * OFFSET ) );
528 p = N->p;
529 end = p + N->n;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100530
Manuel Pégourié-Gonnardd1e7a452013-10-22 21:03:16 +0200531 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
532 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
533 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100534
535cleanup:
536 return( ret );
537}
Manuel Pégourié-Gonnardd1e7a452013-10-22 21:03:16 +0200538
539#undef OFFSET
540#undef A
541#undef ADD
542#undef NEXT
543#undef LAST
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200544#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100545
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200546#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200547
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200548static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
549{
550 *dst += src;
551 *carry += ( *dst < src );
552}
553
554static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
555{
556 *carry -= ( *dst < src );
557 *dst -= src;
558}
559
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200560#if defined(POLARSSL_HAVE_INT8)
561
562#define MAX32 N->n / 4
563#define A( j ) (uint32_t)( N->p[4*j+0] ) | \
564 ( N->p[4*j+1] << 8 ) | \
565 ( N->p[4*j+2] << 16 ) | \
566 ( N->p[4*j+3] << 24 )
567#define STORE32 N->p[4*i+0] = (uint8_t)( cur ); \
568 N->p[4*i+1] = (uint8_t)( cur >> 8 ); \
569 N->p[4*i+2] = (uint8_t)( cur >> 16 ); \
570 N->p[4*i+3] = (uint8_t)( cur >> 24 );
571
572#elif defined(POLARSSL_HAVE_INT16)
573
574#define MAX32 N->n / 2
575#define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
576#define STORE32 N->p[2*i+0] = (uint16_t)( cur ); \
577 N->p[2*i+1] = (uint16_t)( cur >> 16 );
578
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200579#elif defined(POLARSSL_HAVE_INT32)
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200580
581#define MAX32 N->n
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200582#define A( j ) N->p[j]
583#define STORE32 N->p[i] = cur;
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200584
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200585#else /* 64-bit */
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200586
587#define MAX32 N->n * 2
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200588#define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
589#define STORE32 \
590 if( i % 2 ) { \
591 N->p[i/2] &= 0x00000000FFFFFFFF; \
592 N->p[i/2] |= ((uint64_t) cur) << 32; \
593 } else { \
594 N->p[i/2] &= 0xFFFFFFFF00000000; \
595 N->p[i/2] |= (uint64_t) cur; \
596 }
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200597
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200598#endif
599
600#define ADD( j ) add32( &cur, A( j ), &c );
601#define SUB( j ) sub32( &cur, A( j ), &c );
602
603#define LOAD32 cur = A( i );
604
605#define FIRST c = 0; i = 0; LOAD32;
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200606
607#define NEXT \
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200608 STORE32; i++; LOAD32; \
609 cc = c; c = 0; \
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200610 if( cc < 0 ) \
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200611 sub32( &cur, -cc, &c ); \
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200612 else \
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200613 add32( &cur, cc, &c ); \
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200614
Manuel Pégourié-Gonnard2a08c0d2013-10-22 21:07:14 +0200615#define LAST \
616 STORE32; i++; \
617 cur = c > 0 ? c : 0; STORE32; \
618 cur = 0; while( ++i < MAX32 ) { STORE32; } \
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200619 if( c < 0 ) fix_negative( N, c, bits );
620
621/*
622 * If the result is negative, we get it in the form c * 2^192 + N,
623 * with c negative and N positive (the c >= 0 case is handled by LAST).
624 */
625static inline int fix_negative( mpi *N, signed char c, size_t bits )
626{
627 int ret;
628 mpi C;
629
630 mpi_init( &C );
631
632 MPI_CHK( mpi_lset( &C, c ) );
633 MPI_CHK( mpi_shift_l( &C, bits ) );
634 MPI_CHK( mpi_add_mpi( N, N, &C ) );
635
636cleanup:
637 mpi_free( &C );
638
639 return( ret );
640}
641
642/*
643 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
644 */
645static int ecp_mod_p224( mpi *N )
646{
647 int ret;
648 signed char c, cc;
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200649 uint32_t cur;
650 size_t i;
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200651 size_t bits = 224;
652
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200653 /* Make sure we have enough blocks */
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200654 MPI_CHK( mpi_grow( N, bits * 2 / 8 / sizeof( t_uint ) ) );
655
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200656 FIRST;
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200657 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
658 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
659 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
660 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
661 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
662 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
663 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
664
665cleanup:
666 return( ret );
667}
668#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
669
670#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100671/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100672 * Size of p521 in terms of t_uint
673 */
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200674#define P521_SIZE_INT ( 521 / 8 / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100675
676/*
677 * Bits to keep in the most significant t_uint
678 */
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200679#if defined(POLARSSL_HAVE_INT8)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100680#define P521_MASK 0x01
681#else
682#define P521_MASK 0x01FF
683#endif
684
685/*
686 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200687 * Write N as A1 + 2^521 A0, return A0 + A1
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100688 */
689static int ecp_mod_p521( mpi *N )
690{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100691 int ret;
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200692 size_t i;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100693 mpi M;
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200694 t_uint Mp[P521_SIZE_INT+1];
695 /* Worst case for the size of M is when sizeof( t_uint ) == 16:
696 * we need to hold bits 513 to 1056, which is 34 limbs, that is
697 * P521_SIZE_INT + 1. Otherwise P521_SIZE is enough. */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100698
699 if( N->n < P521_SIZE_INT )
700 return( 0 );
701
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200702 /* M = A1 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100703 M.s = 1;
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200704 M.n = N->n - ( P521_SIZE_INT - 1 );
705 if( M.n > P521_SIZE_INT + 1 )
706 M.n = P521_SIZE_INT + 1;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100707 M.p = Mp;
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200708 memcpy( Mp, N->p + P521_SIZE_INT - 1, M.n * sizeof( t_uint ) );
709 MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100710
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200711 /* N = A0 */
712 N->p[P521_SIZE_INT - 1] &= P521_MASK;
713 for( i = P521_SIZE_INT; i < N->n; i++ )
714 N->p[i] = 0;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100715
Manuel Pégourié-Gonnardcc67aee2013-10-18 10:55:45 +0200716 /* N = A0 + A1 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100717 MPI_CHK( mpi_add_abs( N, N, &M ) );
718
719cleanup:
720 return( ret );
721}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200722#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100723
724/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100725 * Domain parameters for secp192r1
726 */
727#define SECP192R1_P \
728 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
729#define SECP192R1_B \
730 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
731#define SECP192R1_GX \
732 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
733#define SECP192R1_GY \
734 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
735#define SECP192R1_N \
736 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
737
738/*
739 * Domain parameters for secp224r1
740 */
741#define SECP224R1_P \
742 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
743#define SECP224R1_B \
744 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
745#define SECP224R1_GX \
746 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
747#define SECP224R1_GY \
748 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
749#define SECP224R1_N \
750 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
751
752/*
753 * Domain parameters for secp256r1
754 */
755#define SECP256R1_P \
756 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
757#define SECP256R1_B \
758 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
759#define SECP256R1_GX \
760 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
761#define SECP256R1_GY \
762 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
763#define SECP256R1_N \
764 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
765
766/*
767 * Domain parameters for secp384r1
768 */
769#define SECP384R1_P \
770 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
771 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
772#define SECP384R1_B \
773 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
774 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
775#define SECP384R1_GX \
776 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
777 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
778#define SECP384R1_GY \
779 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
780 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
781#define SECP384R1_N \
782 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
783 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
784
785/*
786 * Domain parameters for secp521r1
787 */
788#define SECP521R1_P \
789 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
790 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
791 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
792#define SECP521R1_B \
793 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
794 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
795 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
796#define SECP521R1_GX \
797 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
798 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
799 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
800#define SECP521R1_GY \
801 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
802 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
803 "3FAD0761353C7086A272C24088BE94769FD16650"
804#define SECP521R1_N \
805 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
806 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
807 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
808
809/*
Manuel Pégourié-Gonnardcec4a532013-10-07 19:52:27 +0200810 * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
811 */
812#define BP256R1_P \
813 "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377"
814#define BP256R1_A \
815 "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9"
816#define BP256R1_B \
817 "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6"
818#define BP256R1_GX \
819 "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262"
820#define BP256R1_GY \
821 "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"
822#define BP256R1_N \
823 "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7"
824
825/*
826 * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
827 */
828#define BP384R1_P \
829 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB711" \
830 "23ACD3A729901D1A71874700133107EC53"
831#define BP384R1_A \
832 "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F9" \
833 "0F8AA5814A503AD4EB04A8C7DD22CE2826"
834#define BP384R1_B \
835 "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62" \
836 "D57CB4390295DBC9943AB78696FA504C11"
837#define BP384R1_GX \
838 "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10" \
839 "E8E826E03436D646AAEF87B2E247D4AF1E"
840#define BP384R1_GY \
841 "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129" \
842 "280E4646217791811142820341263C5315"
843#define BP384R1_N \
844 "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425" \
845 "A7CF3AB6AF6B7FC3103B883202E9046565"
846
847/*
848 * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
849 */
850#define BP512R1_P \
851 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
852 "717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"
853#define BP512R1_A \
854 "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863" \
855 "BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA"
856#define BP512R1_B \
857 "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117" \
858 "A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723"
859#define BP512R1_GX \
860 "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D009" \
861 "8EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822"
862#define BP512R1_GY \
863 "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F81" \
864 "11B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"
865#define BP512R1_N \
866 "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \
867 "70553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"
868
869/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100870 * Set a group using well-known domain parameters
871 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100872int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100873{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100874 grp->id = id;
875
876 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100877 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200878#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100879 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100880 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100881 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100882 SECP192R1_P, SECP192R1_B,
883 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200884#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100885
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200886#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100887 case POLARSSL_ECP_DP_SECP224R1:
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200888 grp->modp = ecp_mod_p224;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100889 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100890 SECP224R1_P, SECP224R1_B,
891 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200892#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100893
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200894#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100895 case POLARSSL_ECP_DP_SECP256R1:
896 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100897 SECP256R1_P, SECP256R1_B,
898 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200899#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100900
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200901#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100902 case POLARSSL_ECP_DP_SECP384R1:
903 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100904 SECP384R1_P, SECP384R1_B,
905 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200906#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100907
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200908#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100909 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100910 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100911 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100912 SECP521R1_P, SECP521R1_B,
913 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200914#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100915
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200916#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
917 case POLARSSL_ECP_DP_BP256R1:
918 return( ecp_group_read_string_gen( grp, 16,
919 BP256R1_P, BP256R1_A, BP256R1_B,
920 BP256R1_GX, BP256R1_GY, BP256R1_N ) );
921#endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
922
923#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
924 case POLARSSL_ECP_DP_BP384R1:
925 return( ecp_group_read_string_gen( grp, 16,
926 BP384R1_P, BP384R1_A, BP384R1_B,
927 BP384R1_GX, BP384R1_GY, BP384R1_N ) );
928#endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
929
930#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
931 case POLARSSL_ECP_DP_BP512R1:
932 return( ecp_group_read_string_gen( grp, 16,
933 BP512R1_P, BP512R1_A, BP512R1_B,
934 BP512R1_GX, BP512R1_GY, BP512R1_N ) );
935#endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
936
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200937 default:
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200938 ecp_group_free( grp );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200939 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
940 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100941}
942
943/*
944 * Set a group from an ECParameters record (RFC 4492)
945 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100946int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100947{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200948 uint16_t tls_id;
949 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100950
951 /*
952 * We expect at least three bytes (see below)
953 */
954 if( len < 3 )
955 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
956
957 /*
958 * First byte is curve_type; only named_curve is handled
959 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100960 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100961 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
962
963 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100964 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100965 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200966 tls_id = *(*buf)++;
967 tls_id <<= 8;
968 tls_id |= *(*buf)++;
969
970 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
971 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
972
973 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100974}
975
976/*
977 * Write the ECParameters record corresponding to a group (RFC 4492)
978 */
979int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
980 unsigned char *buf, size_t blen )
981{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200982 const ecp_curve_info *curve_info;
983
984 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
985 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200986
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100987 /*
988 * We are going to write 3 bytes (see below)
989 */
990 *olen = 3;
991 if( blen < *olen )
992 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
993
994 /*
995 * First byte is curve_type, always named_curve
996 */
997 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
998
999 /*
1000 * Next two bytes are the namedcurve value
1001 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001002 buf[0] = curve_info->tls_id >> 8;
1003 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001004
1005 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001006}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001007
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001008/*
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001009 * Get the curve info from the TLS identifier
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001010 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001011const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001012{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +02001013 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001014
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +02001015 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001016 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
1017 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001018 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +02001019 if( curve_info->tls_id == tls_id )
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001020 return( curve_info );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001021 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001022
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001023 return( NULL );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001024}
1025
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001026/*
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001027 * Get the curve info for the internal identifer
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001028 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001029const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001030{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +02001031 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001032
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +02001033 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001034 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
1035 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001036 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +02001037 if( curve_info->grp_id == grp_id )
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001038 return( curve_info );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001039 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001040
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001041 return( NULL );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001042}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001043
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001044/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001045 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001046 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001047 * In order to guarantee that, we need to ensure that operands of
1048 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001049 * bring the result back to this range.
1050 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001051 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001052 */
1053
1054/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001055 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
1056 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +01001057#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001058
1059/*
1060 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001061 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001062 */
1063#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001064 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001065 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
1066
1067/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001068 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
1069 * We known P, N and the result are positive, so sub_abs is correct, and
1070 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001071 */
1072#define MOD_ADD( N ) \
1073 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001074 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001075
1076/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001077 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001078 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001079static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001080{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001081 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001082 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001083
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001084 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001085 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001086
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001087 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001088
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001089 /*
1090 * X = X / Z^2 mod p
1091 */
1092 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
1093 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1094 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001095
1096 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001097 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001098 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001099 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
1100 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001101
1102 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001103 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001104 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001105 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001106
1107cleanup:
1108
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001109 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001110
1111 return( ret );
1112}
1113
1114/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001115 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001116 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001117 * (See for example Cohen's "A Course in Computational Algebraic Number
1118 * Theory", Algorithm 10.3.4.)
1119 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001120 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001121 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001122 */
1123static int ecp_normalize_many( const ecp_group *grp,
1124 ecp_point T[], size_t t_len )
1125{
1126 int ret;
1127 size_t i;
1128 mpi *c, u, Zi, ZZi;
1129
1130 if( t_len < 2 )
1131 return( ecp_normalize( grp, T ) );
1132
Paul Bakker6e339b52013-07-03 13:37:05 +02001133 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001134 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001135
1136 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
1137 for( i = 0; i < t_len; i++ )
1138 mpi_init( &c[i] );
1139
1140 /*
1141 * c[i] = Z_0 * ... * Z_i
1142 */
1143 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
1144 for( i = 1; i < t_len; i++ )
1145 {
1146 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
1147 MOD_MUL( c[i] );
1148 }
1149
1150 /*
1151 * u = 1 / (Z_0 * ... * Z_n) mod P
1152 */
1153 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
1154
1155 for( i = t_len - 1; ; i-- )
1156 {
1157 /*
1158 * Zi = 1 / Z_i mod p
1159 * u = 1 / (Z_0 * ... * Z_i) mod P
1160 */
1161 if( i == 0 ) {
1162 MPI_CHK( mpi_copy( &Zi, &u ) );
1163 }
1164 else
1165 {
1166 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
1167 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
1168 }
1169
1170 /*
1171 * proceed as in normalize()
1172 */
1173 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1174 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
1175 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
1176 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
1177 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
1178
1179 if( i == 0 )
1180 break;
1181 }
1182
1183cleanup:
1184
1185 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
1186 for( i = 0; i < t_len; i++ )
1187 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +02001188 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001189
1190 return( ret );
1191}
1192
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001193/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001194 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001195 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001196 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001197 * with heavy variable renaming, some reordering and one minor modification
1198 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
1199 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001200 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001201static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
1202 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001203{
1204 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001205 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001206
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001207#if defined(POLARSSL_SELF_TEST)
1208 dbl_count++;
1209#endif
1210
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001211 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
1212 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001213
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001214 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
1215 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
1216 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
1217 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
1218 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
1219 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
1220 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
1221 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
1222 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
1223 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
1224 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
1225 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
1226 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
1227 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
1228 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1229 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
1230 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
1231 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
1232 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
1233 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
1234 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
1235 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
1236 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
1237 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001238
1239 MPI_CHK( mpi_copy( &R->X, &X3 ) );
1240 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
1241 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
1242
1243cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001244 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
1245 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001246
1247 return( ret );
1248}
1249
1250/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001251 * Addition or subtraction: R = P + Q or R = P + Q,
1252 * mixed affine-Jacobian coordinates (GECC 3.22)
1253 *
1254 * The coordinates of Q must be normalized (= affine),
1255 * but those of P don't need to. R is not normalized.
1256 *
1257 * If sign >= 0, perform addition, otherwise perform subtraction,
1258 * taking advantage of the fact that, for Q != 0, we have
1259 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001260 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001261static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001262 const ecp_point *P, const ecp_point *Q,
1263 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001264{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001265 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001266 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001267
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001268#if defined(POLARSSL_SELF_TEST)
1269 add_count++;
1270#endif
1271
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001272 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001273 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001274 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001275 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001276 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1277 return( ecp_copy( R, P ) );
1278
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001279 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1280 {
1281 ret = ecp_copy( R, Q );
1282
1283 /*
1284 * -R.Y mod P = P - R.Y unless R.Y == 0
1285 */
1286 if( ret == 0 && sign < 0)
1287 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1288 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1289
1290 return( ret );
1291 }
1292
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001293 /*
1294 * Make sure Q coordinates are normalized
1295 */
1296 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001297 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001298
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001299 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1300 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001301
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001302 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1303 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1304 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1305 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001306
1307 /*
1308 * For subtraction, -Q.Y should have been used instead of Q.Y,
1309 * so we replace T2 by -T2, which is P - T2 mod P
1310 */
1311 if( sign < 0 )
1312 {
1313 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1314 MOD_SUB( T2 );
1315 }
1316
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001317 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1318 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001319
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001320 if( mpi_cmp_int( &T1, 0 ) == 0 )
1321 {
1322 if( mpi_cmp_int( &T2, 0 ) == 0 )
1323 {
1324 ret = ecp_double_jac( grp, R, P );
1325 goto cleanup;
1326 }
1327 else
1328 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001329 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001330 goto cleanup;
1331 }
1332 }
1333
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001334 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1335 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1336 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1337 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1338 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1339 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1340 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1341 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1342 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1343 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1344 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1345 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001346
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001347 MPI_CHK( mpi_copy( &R->X, &X ) );
1348 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1349 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001350
1351cleanup:
1352
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001353 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1354 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001355
1356 return( ret );
1357}
1358
1359/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001360 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001361 */
1362int ecp_add( const ecp_group *grp, ecp_point *R,
1363 const ecp_point *P, const ecp_point *Q )
1364{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001365 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001366
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001367 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1368 MPI_CHK( ecp_normalize( grp, R ) );
1369
1370cleanup:
1371 return( ret );
1372}
1373
1374/*
1375 * Subtraction: R = P - Q, result's coordinates normalized
1376 */
1377int ecp_sub( const ecp_group *grp, ecp_point *R,
1378 const ecp_point *P, const ecp_point *Q )
1379{
1380 int ret;
1381
1382 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001383 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001384
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001385cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001386 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001387}
1388
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001389/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001390 * Compute a modified width-w non-adjacent form (NAF) of a number,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001391 * with a fixed pattern for resistance to simple timing attacks (even SPA),
1392 * see [1]. (The resulting multiplication algorithm can also been seen as a
1393 * modification of 2^w-ary multiplication, with signed coefficients, all of
1394 * them odd.)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001395 *
1396 * Input:
1397 * m must be an odd positive mpi less than w * k bits long
1398 * x must be an array of k elements
1399 * w must be less than a certain maximum (currently 8)
1400 *
1401 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1402 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1403 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1404 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1405 *
1406 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1407 * p. 335 of the cited reference, here we return only u, not d_w since
1408 * it is known that the other d_w[j] will be 0. Moreover, the returned
1409 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1410 * that u_i is odd. Also, since we always select a positive value for d
1411 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1412 * does. Finally, there is an off-by-one error in the reference: the
1413 * last index should be k-1, not k.
1414 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001415static int ecp_w_naf_fixed( signed char x[], size_t k,
1416 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001417{
1418 int ret;
1419 unsigned int i, u, mask, carry;
1420 mpi M;
1421
1422 mpi_init( &M );
1423
1424 MPI_CHK( mpi_copy( &M, m ) );
1425 mask = ( 1 << w ) - 1;
1426 carry = 1 << ( w - 1 );
1427
1428 for( i = 0; i < k; i++ )
1429 {
1430 u = M.p[0] & mask;
1431
1432 if( ( u & 1 ) == 0 && i > 0 )
1433 x[i - 1] -= carry;
1434
1435 x[i] = u >> 1;
1436 mpi_shift_r( &M, w );
1437 }
1438
1439 /*
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001440 * We should have consumed all bits, unless the input value was too big
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001441 */
1442 if( mpi_cmp_int( &M, 0 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001443 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001444
1445cleanup:
1446
1447 mpi_free( &M );
1448
1449 return( ret );
1450}
1451
1452/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001453 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1454 * The table is filled with T[i] = (2 * i + 1) P.
1455 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001456static int ecp_precompute( const ecp_group *grp,
1457 ecp_point T[], size_t t_len,
1458 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001459{
1460 int ret;
1461 size_t i;
1462 ecp_point PP;
1463
1464 ecp_point_init( &PP );
1465
1466 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1467
1468 MPI_CHK( ecp_copy( &T[0], P ) );
1469
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001470 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001471 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1472
1473 /*
1474 * T[0] = P already has normalized coordinates
1475 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001476 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001477
1478cleanup:
1479
1480 ecp_point_free( &PP );
1481
1482 return( ret );
1483}
1484
1485/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001486 * Randomize jacobian coordinates:
1487 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1488 * This is sort of the reverse operation of ecp_normalize().
1489 */
1490static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1491 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1492{
1493 int ret;
1494 mpi l, ll;
1495 size_t p_size = (grp->pbits + 7) / 8;
1496 int count = 0;
1497
1498 mpi_init( &l ); mpi_init( &ll );
1499
1500 /* Generate l such that 1 < l < p */
1501 do
1502 {
1503 mpi_fill_random( &l, p_size, f_rng, p_rng );
1504
1505 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1506 mpi_shift_r( &l, 1 );
1507
1508 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001509 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001510 }
1511 while( mpi_cmp_int( &l, 1 ) <= 0 );
1512
1513 /* Z = l * Z */
1514 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1515
1516 /* X = l^2 * X */
1517 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1518 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1519
1520 /* Y = l^3 * Y */
1521 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1522 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1523
1524cleanup:
1525 mpi_free( &l ); mpi_free( &ll );
1526
1527 return( ret );
1528}
1529
1530/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001531 * Maximum length of the precomputed table
1532 */
1533#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1534
1535/*
1536 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1537 * (that is: grp->nbits / w + 1)
1538 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1539 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001540#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001541
1542/*
1543 * Integer multiplication: R = m * P
1544 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001545 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed().
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001546 *
1547 * This function executes a fixed number of operations for
1548 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001549 *
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001550 * As an additional countermeasure against potential timing attacks,
1551 * we randomize coordinates before each addition. This was suggested as a
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001552 * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that
1553 * we use jacobian coordinates, not standard projective coordinates).
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001554 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001555int ecp_mul( ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001556 const mpi *m, const ecp_point *P,
1557 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001558{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001559 int ret;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001560 unsigned char w, m_is_odd, p_eq_g;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001561 size_t pre_len = 1, naf_len, i, j;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001562 signed char naf[ MAX_NAF_LEN ];
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001563 ecp_point Q, *T = NULL, S[2];
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001564 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001565
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001566 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001567 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001568
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001569 mpi_init( &M );
1570 ecp_point_init( &Q );
1571 ecp_point_init( &S[0] );
1572 ecp_point_init( &S[1] );
1573
1574 /*
1575 * Check if P == G
1576 */
1577 p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 &&
1578 mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1579 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
1580
1581 /*
1582 * If P == G, pre-compute a lot of points: this will be re-used later,
1583 * otherwise, choose window size depending on curve size
1584 */
1585 if( p_eq_g )
1586 w = POLARSSL_ECP_WINDOW_SIZE;
1587 else
1588 w = grp->nbits >= 512 ? 6 :
1589 grp->nbits >= 224 ? 5 :
1590 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001591
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001592 /*
1593 * Make sure w is within the limits.
1594 * The last test ensures that none of the precomputed points is zero,
1595 * which wouldn't be handled correctly by ecp_normalize_many().
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001596 * It is only useful for very small curves as used in the test suite.
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001597 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001598 if( w > POLARSSL_ECP_WINDOW_SIZE )
1599 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001600 if( w < 2 || w >= grp->nbits )
1601 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001602
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001603 pre_len <<= ( w - 1 );
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001604 naf_len = grp->nbits / w + 1;
1605
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001606 /*
1607 * Prepare precomputed points: if P == G we want to
1608 * use grp->T if already initialized, or initiliaze it.
1609 */
1610 if( ! p_eq_g || grp->T == NULL )
1611 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001612 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1613 if( T == NULL )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001614 {
1615 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1616 goto cleanup;
1617 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001618
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001619 for( i = 0; i < pre_len; i++ )
1620 ecp_point_init( &T[i] );
1621
1622 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
1623
1624 if( p_eq_g )
1625 {
1626 grp->T = T;
1627 grp->T_size = pre_len;
1628 }
1629 }
1630 else
1631 {
1632 T = grp->T;
1633
1634 /* Should never happen, but we want to be extra sure */
1635 if( pre_len != grp->T_size )
1636 {
1637 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
1638 goto cleanup;
1639 }
1640 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001641
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001642 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001643 * Make sure M is odd (M = m + 1 or M = m + 2)
1644 * later we'll get m * P by subtracting P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001645 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001646 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1647
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001648 MPI_CHK( mpi_copy( &M, m ) );
1649 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001650
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001651 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001652 * Compute the fixed-pattern NAF of M
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001653 */
1654 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001655
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001656 /*
1657 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1658 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1659 *
1660 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1661 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1662 * == T[ - naf[i] - 1 ]
1663 */
1664 MPI_CHK( ecp_set_zero( &Q ) );
1665 i = naf_len - 1;
1666 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001667 {
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001668 /* Countermeasure (see comments above) */
1669 if( f_rng != NULL )
1670 ecp_randomize_coordinates( grp, &Q, f_rng, p_rng );
1671
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001672 if( naf[i] < 0 )
1673 {
1674 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1675 }
1676 else
1677 {
1678 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1679 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001680
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001681 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001682 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001683 i--;
1684
1685 for( j = 0; j < w; j++ )
1686 {
1687 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1688 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001689 }
1690
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001691 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001692 * Now get m * P from M * P
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001693 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001694 MPI_CHK( ecp_copy( &S[0], P ) );
1695 MPI_CHK( ecp_add( grp, &S[1], P, P ) );
1696 MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001697
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001698
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001699cleanup:
1700
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001701 if( T != NULL && ! p_eq_g )
1702 {
1703 for( i = 0; i < pre_len; i++ )
1704 ecp_point_free( &T[i] );
1705 polarssl_free( T );
1706 }
1707
1708 ecp_point_free( &S[1] );
1709 ecp_point_free( &S[0] );
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001710 ecp_point_free( &Q );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001711 mpi_free( &M );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001712
1713 return( ret );
1714}
1715
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001716/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001717 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1718 */
1719int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1720{
1721 int ret;
1722 mpi YY, RHS;
1723
1724 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001725 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001726
1727 /*
1728 * pt coordinates must be normalized for our checks
1729 */
1730 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001731 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001732
1733 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1734 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1735 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1736 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001737 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001738
1739 mpi_init( &YY ); mpi_init( &RHS );
1740
1741 /*
1742 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001743 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001744 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001745 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1746 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001747 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001748 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1749 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001750
1751 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001752 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001753
1754cleanup:
1755
1756 mpi_free( &YY ); mpi_free( &RHS );
1757
1758 return( ret );
1759}
1760
1761/*
1762 * Check that an mpi is valid as a private key (SEC1 3.2)
1763 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001764int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001765{
1766 /* We want 1 <= d <= N-1 */
1767 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001768 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001769
1770 return( 0 );
1771}
1772
1773/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001774 * Generate a keypair (SEC1 3.2.1)
1775 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001776int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001777 int (*f_rng)(void *, unsigned char *, size_t),
1778 void *p_rng )
1779{
1780 int count = 0;
1781 size_t n_size = (grp->nbits + 7) / 8;
1782
1783 /*
1784 * Generate d such that 1 <= n < N
1785 */
1786 do
1787 {
1788 mpi_fill_random( d, n_size, f_rng, p_rng );
1789
1790 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1791 mpi_shift_r( d, 1 );
1792
1793 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001794 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001795 }
1796 while( mpi_cmp_int( d, 1 ) < 0 );
1797
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001798 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001799}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001800
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001801#if defined(POLARSSL_SELF_TEST)
1802
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001803/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001804 * Checkup routine
1805 */
1806int ecp_self_test( int verbose )
1807{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001808 int ret;
1809 size_t i;
1810 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001811 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001812 mpi m;
1813 unsigned long add_c_prev, dbl_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001814 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001815 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001816 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001817 "000000000000000000000000000000000000000000000000", /* zero */
1818 "000000000000000000000000000000000000000000000001", /* one */
1819 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1820 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001821 "400000000000000000000000000000000000000000000000",
1822 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1823 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001824 };
1825
1826 ecp_group_init( &grp );
1827 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001828 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001829 mpi_init( &m );
1830
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001831 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001832#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001833 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001834#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001835 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1836#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001837
1838 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001839 printf( " ECP test #1 (constant op_count, base point G): " );
1840
1841 /* Do a dummy multiplication first to trigger precomputation */
1842 MPI_CHK( mpi_lset( &m, 2 ) );
1843 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001844
1845 add_count = 0;
1846 dbl_count = 0;
1847 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001848 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001849
1850 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1851 {
1852 add_c_prev = add_count;
1853 dbl_c_prev = dbl_count;
1854 add_count = 0;
1855 dbl_count = 0;
1856
1857 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001858 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001859
1860 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1861 {
1862 if( verbose != 0 )
1863 printf( "failed (%zu)\n", i );
1864
1865 ret = 1;
1866 goto cleanup;
1867 }
1868 }
1869
1870 if( verbose != 0 )
1871 printf( "passed\n" );
1872
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001873 if( verbose != 0 )
1874 printf( " ECP test #2 (constant op_count, other point): " );
1875 /* We computed P = 2G last time, use it */
1876
1877 add_count = 0;
1878 dbl_count = 0;
1879 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1880 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1881
1882 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1883 {
1884 add_c_prev = add_count;
1885 dbl_c_prev = dbl_count;
1886 add_count = 0;
1887 dbl_count = 0;
1888
1889 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1890 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1891
1892 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1893 {
1894 if( verbose != 0 )
1895 printf( "failed (%zu)\n", i );
1896
1897 ret = 1;
1898 goto cleanup;
1899 }
1900 }
1901
1902 if( verbose != 0 )
1903 printf( "passed\n" );
1904
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001905cleanup:
1906
1907 if( ret < 0 && verbose != 0 )
1908 printf( "Unexpected error, return code = %08X\n", ret );
1909
1910 ecp_group_free( &grp );
1911 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001912 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001913 mpi_free( &m );
1914
1915 if( verbose != 0 )
1916 printf( "\n" );
1917
1918 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001919}
1920
1921#endif
1922
1923#endif