blob: 9ab3763178184d77cde3a8f651f89d6985cdc62c [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é-Gonnard56cd3192013-09-17 17:23:07 +020073 * - TLS NamedCurve ID (RFC 4492 section 5.1.1)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020074 * - size in bits
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020075 * - readeble 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{
79#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020080 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020081#endif
82#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020083 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020084#endif
85#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020086 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020087#endif
88#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020089 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020090#endif
91#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020092 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020093#endif
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020094 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020095};
96
97/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020098 * List of supported curves and associated info
99 */
100const ecp_curve_info *ecp_curve_list( void )
101{
102 return ecp_supported_curves;
103}
104
105/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100106 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100107 */
108void ecp_point_init( ecp_point *pt )
109{
110 if( pt == NULL )
111 return;
112
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100113 mpi_init( &pt->X );
114 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100115 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100116}
117
118/*
119 * Initialize (the components of) a group
120 */
121void ecp_group_init( ecp_group *grp )
122{
123 if( grp == NULL )
124 return;
125
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200126 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100127}
128
129/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200130 * Initialize (the components of) a key pair
131 */
132void ecp_keypair_init( ecp_keypair *key )
133{
134 if ( key == NULL )
135 return;
136
137 ecp_group_init( &key->grp );
138 mpi_init( &key->d );
139 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200140}
141
142/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100143 * Unallocate (the components of) a point
144 */
145void ecp_point_free( ecp_point *pt )
146{
147 if( pt == NULL )
148 return;
149
150 mpi_free( &( pt->X ) );
151 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100152 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100153}
154
155/*
156 * Unallocate (the components of) a group
157 */
158void ecp_group_free( ecp_group *grp )
159{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200160 size_t i;
161
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100162 if( grp == NULL )
163 return;
164
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100165 mpi_free( &grp->P );
166 mpi_free( &grp->B );
167 ecp_point_free( &grp->G );
168 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200169
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200170 if( grp->T != NULL )
171 {
172 for( i = 0; i < grp->T_size; i++ )
173 ecp_point_free( &grp->T[i] );
174 polarssl_free( grp->T );
175 }
176
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200177 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100178}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100179
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100180/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200181 * Unallocate (the components of) a key pair
182 */
183void ecp_keypair_free( ecp_keypair *key )
184{
185 if ( key == NULL )
186 return;
187
188 ecp_group_free( &key->grp );
189 mpi_free( &key->d );
190 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200191}
192
193/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100194 * Set point to zero
195 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100196int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100197{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100198 int ret;
199
200 MPI_CHK( mpi_lset( &pt->X , 1 ) );
201 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
202 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
203
204cleanup:
205 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100206}
207
208/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100209 * Tell if a point is zero
210 */
211int ecp_is_zero( ecp_point *pt )
212{
213 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
214}
215
216/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100217 * Copy the contents of Q into P
218 */
219int ecp_copy( ecp_point *P, const ecp_point *Q )
220{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100221 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100222
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100223 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
224 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100225 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100226
227cleanup:
228 return( ret );
229}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100230
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100231/*
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200232 * Copy the contents of a group object
233 */
234int ecp_group_copy( ecp_group *dst, const ecp_group *src )
235{
236 return ecp_use_known_dp( dst, src->id );
237}
238
239/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100240 * Import a non-zero point from ASCII strings
241 */
242int ecp_point_read_string( ecp_point *P, int radix,
243 const char *x, const char *y )
244{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100245 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100246
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100247 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
248 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100249 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100250
251cleanup:
252 return( ret );
253}
254
255/*
256 * Import an ECP group from ASCII strings
257 */
258int ecp_group_read_string( ecp_group *grp, int radix,
259 const char *p, const char *b,
260 const char *gx, const char *gy, const char *n)
261{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100262 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100263
264 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
265 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
266 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
267 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
268
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100269 grp->pbits = mpi_msb( &grp->P );
270 grp->nbits = mpi_msb( &grp->N );
271
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100272cleanup:
273 return( ret );
274}
275
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100276/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100277 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100278 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100279int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100280 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100281 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100282{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200283 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100284 size_t plen;
285
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100286 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
287 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100288 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100289
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100290 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100291 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100292 */
293 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
294 {
295 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100296 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100297
298 buf[0] = 0x00;
299 *olen = 1;
300
301 return( 0 );
302 }
303
304 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100305
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100306 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
307 {
308 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100309
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100310 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100311 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100312
313 buf[0] = 0x04;
314 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
315 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
316 }
317 else if( format == POLARSSL_ECP_PF_COMPRESSED )
318 {
319 *olen = plen + 1;
320
321 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100322 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100323
324 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
325 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
326 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100327
328cleanup:
329 return( ret );
330}
331
332/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100333 * Import a point from unsigned binary data (SEC1 2.3.4)
334 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100335int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
336 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100337 int ret;
338 size_t plen;
339
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100340 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100341 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100342
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100343 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100344
345 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100346 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100347
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100348 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
349 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
350 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100351
352cleanup:
353 return( ret );
354}
355
356/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100357 * Import a point from a TLS ECPoint record (RFC 4492)
358 * struct {
359 * opaque point <1..2^8-1>;
360 * } ECPoint;
361 */
362int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100363 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100364{
365 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100366 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100367
368 /*
369 * We must have at least two bytes (1 for length, at least of for data)
370 */
371 if( buf_len < 2 )
372 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
373
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100374 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100375 if( data_len < 1 || data_len > buf_len - 1 )
376 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
377
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100378 /*
379 * Save buffer start for read_binary and update buf
380 */
381 buf_start = *buf;
382 *buf += data_len;
383
384 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100385}
386
387/*
388 * Export a point as a TLS ECPoint record (RFC 4492)
389 * struct {
390 * opaque point <1..2^8-1>;
391 * } ECPoint;
392 */
393int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100394 int format, size_t *olen,
395 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100396{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100397 int ret;
398
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100399 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100400 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100401 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100402 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100403 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
404
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100405 if( ( ret = ecp_point_write_binary( grp, pt, format,
406 olen, buf + 1, blen - 1) ) != 0 )
407 return( ret );
408
409 /*
410 * write length to the first byte and update total length
411 */
412 buf[0] = *olen;
413 ++*olen;
414
415 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100416}
417
418/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100419 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
420 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100421 */
422static int ecp_modp( mpi *N, const ecp_group *grp )
423{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100424 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100425
426 if( grp->modp == NULL )
427 return( mpi_mod_mpi( N, N, &grp->P ) );
428
429 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200430 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100431
432 MPI_CHK( grp->modp( N ) );
433
434 while( mpi_cmp_int( N, 0 ) < 0 )
435 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
436
437 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
438 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
439
440cleanup:
441 return( ret );
442}
443
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200444#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100445/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100446 * 192 bits in terms of t_uint
447 */
448#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
449
450/*
451 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
452 * -1 means let this chunk be 0
453 * a positive value i means A_i.
454 */
455#define P192_CHUNKS 3
456#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
457#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
458
459const signed char p192_tbl[][P192_CHUNKS] = {
460 { -1, 3, 3 }, /* S1 */
461 { 4, 4, -1 }, /* S2 */
462 { 5, 5, 5 }, /* S3 */
463};
464
465/*
466 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
467 */
468static int ecp_mod_p192( mpi *N )
469{
470 int ret;
471 unsigned char i, j, offset;
472 signed char chunk;
473 mpi tmp, acc;
474 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
475
476 tmp.s = 1;
477 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
478 tmp.p = tmp_p;
479
480 acc.s = 1;
481 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
482 acc.p = acc_p;
483
484 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
485
486 /*
487 * acc = T
488 */
489 memset( acc_p, 0, sizeof( acc_p ) );
490 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
491
492 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
493 {
494 /*
495 * tmp = S_i
496 */
497 memset( tmp_p, 0, sizeof( tmp_p ) );
498 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
499 {
500 chunk = p192_tbl[i][j];
501 if( chunk >= 0 )
502 memcpy( tmp_p + offset * P192_CHUNK_INT,
503 N->p + chunk * P192_CHUNK_INT,
504 P192_CHUNK_CHAR );
505 }
506
507 /*
508 * acc += tmp
509 */
510 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
511 }
512
513 MPI_CHK( mpi_copy( N, &acc ) );
514
515cleanup:
516 return( ret );
517}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200518#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100519
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200520#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100521/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100522 * Size of p521 in terms of t_uint
523 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100524#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100525
526/*
527 * Bits to keep in the most significant t_uint
528 */
529#if defined(POLARSS_HAVE_INT8)
530#define P521_MASK 0x01
531#else
532#define P521_MASK 0x01FF
533#endif
534
535/*
536 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100537 */
538static int ecp_mod_p521( mpi *N )
539{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100540 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100541 t_uint Mp[P521_SIZE_INT];
542 mpi M;
543
544 if( N->n < P521_SIZE_INT )
545 return( 0 );
546
547 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
548 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
549 Mp[P521_SIZE_INT - 1] &= P521_MASK;
550
551 M.s = 1;
552 M.n = P521_SIZE_INT;
553 M.p = Mp;
554
555 MPI_CHK( mpi_shift_r( N, 521 ) );
556
557 MPI_CHK( mpi_add_abs( N, N, &M ) );
558
559cleanup:
560 return( ret );
561}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200562#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100563
564/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100565 * Domain parameters for secp192r1
566 */
567#define SECP192R1_P \
568 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
569#define SECP192R1_B \
570 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
571#define SECP192R1_GX \
572 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
573#define SECP192R1_GY \
574 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
575#define SECP192R1_N \
576 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
577
578/*
579 * Domain parameters for secp224r1
580 */
581#define SECP224R1_P \
582 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
583#define SECP224R1_B \
584 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
585#define SECP224R1_GX \
586 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
587#define SECP224R1_GY \
588 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
589#define SECP224R1_N \
590 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
591
592/*
593 * Domain parameters for secp256r1
594 */
595#define SECP256R1_P \
596 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
597#define SECP256R1_B \
598 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
599#define SECP256R1_GX \
600 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
601#define SECP256R1_GY \
602 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
603#define SECP256R1_N \
604 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
605
606/*
607 * Domain parameters for secp384r1
608 */
609#define SECP384R1_P \
610 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
611 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
612#define SECP384R1_B \
613 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
614 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
615#define SECP384R1_GX \
616 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
617 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
618#define SECP384R1_GY \
619 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
620 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
621#define SECP384R1_N \
622 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
623 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
624
625/*
626 * Domain parameters for secp521r1
627 */
628#define SECP521R1_P \
629 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
630 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
631 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
632#define SECP521R1_B \
633 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
634 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
635 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
636#define SECP521R1_GX \
637 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
638 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
639 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
640#define SECP521R1_GY \
641 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
642 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
643 "3FAD0761353C7086A272C24088BE94769FD16650"
644#define SECP521R1_N \
645 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
646 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
647 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
648
649/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100650 * Set a group using well-known domain parameters
651 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100652int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100653{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100654 grp->id = id;
655
656 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100657 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200658#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100659 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100660 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100661 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100662 SECP192R1_P, SECP192R1_B,
663 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200664#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100665
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200666#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100667 case POLARSSL_ECP_DP_SECP224R1:
668 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100669 SECP224R1_P, SECP224R1_B,
670 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200671#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100672
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200673#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100674 case POLARSSL_ECP_DP_SECP256R1:
675 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100676 SECP256R1_P, SECP256R1_B,
677 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200678#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100679
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200680#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100681 case POLARSSL_ECP_DP_SECP384R1:
682 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100683 SECP384R1_P, SECP384R1_B,
684 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200685#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100686
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200687#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100688 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100689 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100690 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100691 SECP521R1_P, SECP521R1_B,
692 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200693#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100694
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200695 default:
696 grp->id = POLARSSL_ECP_DP_NONE;
697 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
698 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100699}
700
701/*
702 * Set a group from an ECParameters record (RFC 4492)
703 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100704int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100705{
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200706 unsigned int named_curve;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100707
708 /*
709 * We expect at least three bytes (see below)
710 */
711 if( len < 3 )
712 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
713
714 /*
715 * First byte is curve_type; only named_curve is handled
716 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100717 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100718 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
719
720 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100721 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100722 */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200723 named_curve = *(*buf)++;
724 named_curve <<= 8;
725 named_curve |= *(*buf)++;
726 return ecp_use_known_dp( grp, ecp_grp_id_from_named_curve( named_curve ) );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100727}
728
729/*
730 * Write the ECParameters record corresponding to a group (RFC 4492)
731 */
732int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
733 unsigned char *buf, size_t blen )
734{
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200735 unsigned int named_curve;
736
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100737 /*
738 * We are going to write 3 bytes (see below)
739 */
740 *olen = 3;
741 if( blen < *olen )
742 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
743
744 /*
745 * First byte is curve_type, always named_curve
746 */
747 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
748
749 /*
750 * Next two bytes are the namedcurve value
751 */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200752 named_curve = ecp_named_curve_from_grp_id( grp->id );
753 buf[0] = named_curve >> 8;
754 buf[1] = named_curve & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100755
756 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100757}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100758
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200759/*
760 * Get the internal identifer from the TLS name
761 */
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200762ecp_group_id ecp_grp_id_from_named_curve( uint16_t tls_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200763{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200764 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200765
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200766 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200767 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
768 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200769 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200770 if( curve_info->tls_id == tls_id )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200771 return( curve_info->grp_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200772 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200773
774 return( POLARSSL_ECP_DP_NONE );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200775}
776
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200777/*
778 * Get the TLS name for the internal identifer
779 */
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200780uint16_t ecp_named_curve_from_grp_id( ecp_group_id grp_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200781{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200782 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200783
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200784 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200785 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
786 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200787 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200788 if( curve_info->grp_id == grp_id )
789 return( curve_info->tls_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200790 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200791
792 return( 0 );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200793}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200794
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100795/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100796 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100797 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100798 * In order to guarantee that, we need to ensure that operands of
799 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100800 * bring the result back to this range.
801 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100802 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100803 */
804
805/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100806 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
807 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100808#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100809
810/*
811 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
812 */
813#define MOD_SUB( N ) \
814 while( mpi_cmp_int( &N, 0 ) < 0 ) \
815 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
816
817/*
818 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
819 */
820#define MOD_ADD( N ) \
821 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
822 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
823
824/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100825 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100826 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100827static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100828{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100829 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100830 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100831
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100832 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100833 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100834
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100835 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100836
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100837 /*
838 * X = X / Z^2 mod p
839 */
840 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
841 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
842 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100843
844 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100845 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100846 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100847 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
848 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100849
850 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100851 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100852 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100853 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100854
855cleanup:
856
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100857 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100858
859 return( ret );
860}
861
862/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100863 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100864 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100865 * (See for example Cohen's "A Course in Computational Algebraic Number
866 * Theory", Algorithm 10.3.4.)
867 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200868 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100869 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100870 */
871static int ecp_normalize_many( const ecp_group *grp,
872 ecp_point T[], size_t t_len )
873{
874 int ret;
875 size_t i;
876 mpi *c, u, Zi, ZZi;
877
878 if( t_len < 2 )
879 return( ecp_normalize( grp, T ) );
880
Paul Bakker6e339b52013-07-03 13:37:05 +0200881 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200882 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100883
884 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
885 for( i = 0; i < t_len; i++ )
886 mpi_init( &c[i] );
887
888 /*
889 * c[i] = Z_0 * ... * Z_i
890 */
891 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
892 for( i = 1; i < t_len; i++ )
893 {
894 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
895 MOD_MUL( c[i] );
896 }
897
898 /*
899 * u = 1 / (Z_0 * ... * Z_n) mod P
900 */
901 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
902
903 for( i = t_len - 1; ; i-- )
904 {
905 /*
906 * Zi = 1 / Z_i mod p
907 * u = 1 / (Z_0 * ... * Z_i) mod P
908 */
909 if( i == 0 ) {
910 MPI_CHK( mpi_copy( &Zi, &u ) );
911 }
912 else
913 {
914 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
915 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
916 }
917
918 /*
919 * proceed as in normalize()
920 */
921 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
922 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
923 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
924 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
925 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
926
927 if( i == 0 )
928 break;
929 }
930
931cleanup:
932
933 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
934 for( i = 0; i < t_len; i++ )
935 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200936 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100937
938 return( ret );
939}
940
941
942/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100943 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
944 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100945static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
946 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100947{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100948 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100949 mpi T1, T2, T3, X, Y, Z;
950
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100951#if defined(POLARSSL_SELF_TEST)
952 dbl_count++;
953#endif
954
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100955 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100956 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100957
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100958 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
959 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
960
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100961 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
962 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
963 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
964 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
965 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
966 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
967 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
968 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
969 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
970 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100971
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100972 /*
973 * For Y = Y / 2 mod p, we must make sure that Y is even before
974 * using right-shift. No need to reduce mod p afterwards.
975 */
976 if( mpi_get_bit( &Y, 0 ) == 1 )
977 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
978 MPI_CHK( mpi_shift_r( &Y, 1 ) );
979
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100980 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
981 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
982 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
983 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
984 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
985 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100986
987 MPI_CHK( mpi_copy( &R->X, &X ) );
988 MPI_CHK( mpi_copy( &R->Y, &Y ) );
989 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100990
991cleanup:
992
993 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
994 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
995
996 return( ret );
997}
998
999/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001000 * Addition or subtraction: R = P + Q or R = P + Q,
1001 * mixed affine-Jacobian coordinates (GECC 3.22)
1002 *
1003 * The coordinates of Q must be normalized (= affine),
1004 * but those of P don't need to. R is not normalized.
1005 *
1006 * If sign >= 0, perform addition, otherwise perform subtraction,
1007 * taking advantage of the fact that, for Q != 0, we have
1008 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001009 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001010static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001011 const ecp_point *P, const ecp_point *Q,
1012 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001013{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001014 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001015 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001016
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001017#if defined(POLARSSL_SELF_TEST)
1018 add_count++;
1019#endif
1020
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001021 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001022 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001023 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001024 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001025 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1026 return( ecp_copy( R, P ) );
1027
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001028 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1029 {
1030 ret = ecp_copy( R, Q );
1031
1032 /*
1033 * -R.Y mod P = P - R.Y unless R.Y == 0
1034 */
1035 if( ret == 0 && sign < 0)
1036 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1037 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1038
1039 return( ret );
1040 }
1041
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001042 /*
1043 * Make sure Q coordinates are normalized
1044 */
1045 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001046 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001047
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001048 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1049 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001050
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001051 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1052 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1053 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1054 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001055
1056 /*
1057 * For subtraction, -Q.Y should have been used instead of Q.Y,
1058 * so we replace T2 by -T2, which is P - T2 mod P
1059 */
1060 if( sign < 0 )
1061 {
1062 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1063 MOD_SUB( T2 );
1064 }
1065
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001066 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1067 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001068
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001069 if( mpi_cmp_int( &T1, 0 ) == 0 )
1070 {
1071 if( mpi_cmp_int( &T2, 0 ) == 0 )
1072 {
1073 ret = ecp_double_jac( grp, R, P );
1074 goto cleanup;
1075 }
1076 else
1077 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001078 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001079 goto cleanup;
1080 }
1081 }
1082
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001083 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1084 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1085 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1086 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1087 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1088 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1089 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1090 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1091 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1092 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1093 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1094 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001095
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001096 MPI_CHK( mpi_copy( &R->X, &X ) );
1097 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1098 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001099
1100cleanup:
1101
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001102 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1103 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001104
1105 return( ret );
1106}
1107
1108/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001109 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001110 */
1111int ecp_add( const ecp_group *grp, ecp_point *R,
1112 const ecp_point *P, const ecp_point *Q )
1113{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001114 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001115
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001116 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1117 MPI_CHK( ecp_normalize( grp, R ) );
1118
1119cleanup:
1120 return( ret );
1121}
1122
1123/*
1124 * Subtraction: R = P - Q, result's coordinates normalized
1125 */
1126int ecp_sub( const ecp_group *grp, ecp_point *R,
1127 const ecp_point *P, const ecp_point *Q )
1128{
1129 int ret;
1130
1131 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001132 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001133
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001134cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001135 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001136}
1137
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001138/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001139 * Compute a modified width-w non-adjacent form (NAF) of a number,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001140 * with a fixed pattern for resistance to simple timing attacks (even SPA),
1141 * see [1]. (The resulting multiplication algorithm can also been seen as a
1142 * modification of 2^w-ary multiplication, with signed coefficients, all of
1143 * them odd.)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001144 *
1145 * Input:
1146 * m must be an odd positive mpi less than w * k bits long
1147 * x must be an array of k elements
1148 * w must be less than a certain maximum (currently 8)
1149 *
1150 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1151 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1152 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1153 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1154 *
1155 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1156 * p. 335 of the cited reference, here we return only u, not d_w since
1157 * it is known that the other d_w[j] will be 0. Moreover, the returned
1158 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1159 * that u_i is odd. Also, since we always select a positive value for d
1160 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1161 * does. Finally, there is an off-by-one error in the reference: the
1162 * last index should be k-1, not k.
1163 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001164static int ecp_w_naf_fixed( signed char x[], size_t k,
1165 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001166{
1167 int ret;
1168 unsigned int i, u, mask, carry;
1169 mpi M;
1170
1171 mpi_init( &M );
1172
1173 MPI_CHK( mpi_copy( &M, m ) );
1174 mask = ( 1 << w ) - 1;
1175 carry = 1 << ( w - 1 );
1176
1177 for( i = 0; i < k; i++ )
1178 {
1179 u = M.p[0] & mask;
1180
1181 if( ( u & 1 ) == 0 && i > 0 )
1182 x[i - 1] -= carry;
1183
1184 x[i] = u >> 1;
1185 mpi_shift_r( &M, w );
1186 }
1187
1188 /*
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001189 * We should have consumed all bits, unless the input value was too big
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001190 */
1191 if( mpi_cmp_int( &M, 0 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001192 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001193
1194cleanup:
1195
1196 mpi_free( &M );
1197
1198 return( ret );
1199}
1200
1201/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001202 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1203 * The table is filled with T[i] = (2 * i + 1) P.
1204 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001205static int ecp_precompute( const ecp_group *grp,
1206 ecp_point T[], size_t t_len,
1207 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001208{
1209 int ret;
1210 size_t i;
1211 ecp_point PP;
1212
1213 ecp_point_init( &PP );
1214
1215 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1216
1217 MPI_CHK( ecp_copy( &T[0], P ) );
1218
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001219 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001220 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1221
1222 /*
1223 * T[0] = P already has normalized coordinates
1224 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001225 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001226
1227cleanup:
1228
1229 ecp_point_free( &PP );
1230
1231 return( ret );
1232}
1233
1234/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001235 * Randomize jacobian coordinates:
1236 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1237 * This is sort of the reverse operation of ecp_normalize().
1238 */
1239static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1240 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1241{
1242 int ret;
1243 mpi l, ll;
1244 size_t p_size = (grp->pbits + 7) / 8;
1245 int count = 0;
1246
1247 mpi_init( &l ); mpi_init( &ll );
1248
1249 /* Generate l such that 1 < l < p */
1250 do
1251 {
1252 mpi_fill_random( &l, p_size, f_rng, p_rng );
1253
1254 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1255 mpi_shift_r( &l, 1 );
1256
1257 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001258 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001259 }
1260 while( mpi_cmp_int( &l, 1 ) <= 0 );
1261
1262 /* Z = l * Z */
1263 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1264
1265 /* X = l^2 * X */
1266 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1267 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1268
1269 /* Y = l^3 * Y */
1270 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1271 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1272
1273cleanup:
1274 mpi_free( &l ); mpi_free( &ll );
1275
1276 return( ret );
1277}
1278
1279/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001280 * Maximum length of the precomputed table
1281 */
1282#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1283
1284/*
1285 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1286 * (that is: grp->nbits / w + 1)
1287 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1288 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001289#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001290
1291/*
1292 * Integer multiplication: R = m * P
1293 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001294 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed().
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001295 *
1296 * This function executes a fixed number of operations for
1297 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001298 *
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001299 * As an additional countermeasure against potential timing attacks,
1300 * we randomize coordinates before each addition. This was suggested as a
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001301 * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that
1302 * we use jacobian coordinates, not standard projective coordinates).
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001303 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001304int ecp_mul( ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001305 const mpi *m, const ecp_point *P,
1306 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001307{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001308 int ret;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001309 unsigned char w, m_is_odd, p_eq_g;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001310 size_t pre_len, naf_len, i, j;
1311 signed char naf[ MAX_NAF_LEN ];
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001312 ecp_point Q, *T = NULL, S[2];
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001313 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001314
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001315 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001316 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001317
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001318 mpi_init( &M );
1319 ecp_point_init( &Q );
1320 ecp_point_init( &S[0] );
1321 ecp_point_init( &S[1] );
1322
1323 /*
1324 * Check if P == G
1325 */
1326 p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 &&
1327 mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1328 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
1329
1330 /*
1331 * If P == G, pre-compute a lot of points: this will be re-used later,
1332 * otherwise, choose window size depending on curve size
1333 */
1334 if( p_eq_g )
1335 w = POLARSSL_ECP_WINDOW_SIZE;
1336 else
1337 w = grp->nbits >= 512 ? 6 :
1338 grp->nbits >= 224 ? 5 :
1339 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001340
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001341 /*
1342 * Make sure w is within the limits.
1343 * The last test ensures that none of the precomputed points is zero,
1344 * which wouldn't be handled correctly by ecp_normalize_many().
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001345 * It is only useful for very small curves as used in the test suite.
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001346 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001347 if( w > POLARSSL_ECP_WINDOW_SIZE )
1348 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001349 if( w < 2 || w >= grp->nbits )
1350 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001351
1352 pre_len = 1 << ( w - 1 );
1353 naf_len = grp->nbits / w + 1;
1354
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001355 /*
1356 * Prepare precomputed points: if P == G we want to
1357 * use grp->T if already initialized, or initiliaze it.
1358 */
1359 if( ! p_eq_g || grp->T == NULL )
1360 {
1361 if( ( T = polarssl_malloc( pre_len * sizeof( ecp_point ) ) ) == NULL )
1362 {
1363 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1364 goto cleanup;
1365 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001366
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001367 for( i = 0; i < pre_len; i++ )
1368 ecp_point_init( &T[i] );
1369
1370 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
1371
1372 if( p_eq_g )
1373 {
1374 grp->T = T;
1375 grp->T_size = pre_len;
1376 }
1377 }
1378 else
1379 {
1380 T = grp->T;
1381
1382 /* Should never happen, but we want to be extra sure */
1383 if( pre_len != grp->T_size )
1384 {
1385 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
1386 goto cleanup;
1387 }
1388 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001389
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001390 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001391 * Make sure M is odd (M = m + 1 or M = m + 2)
1392 * later we'll get m * P by subtracting P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001393 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001394 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1395
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001396 MPI_CHK( mpi_copy( &M, m ) );
1397 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001398
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001399 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001400 * Compute the fixed-pattern NAF of M
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001401 */
1402 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001403
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001404 /*
1405 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1406 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1407 *
1408 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1409 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1410 * == T[ - naf[i] - 1 ]
1411 */
1412 MPI_CHK( ecp_set_zero( &Q ) );
1413 i = naf_len - 1;
1414 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001415 {
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001416 /* Countermeasure (see comments above) */
1417 if( f_rng != NULL )
1418 ecp_randomize_coordinates( grp, &Q, f_rng, p_rng );
1419
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001420 if( naf[i] < 0 )
1421 {
1422 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1423 }
1424 else
1425 {
1426 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1427 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001428
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001429 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001430 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001431 i--;
1432
1433 for( j = 0; j < w; j++ )
1434 {
1435 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1436 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001437 }
1438
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001439 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001440 * Now get m * P from M * P
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001441 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001442 MPI_CHK( ecp_copy( &S[0], P ) );
1443 MPI_CHK( ecp_add( grp, &S[1], P, P ) );
1444 MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001445
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001446
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001447cleanup:
1448
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001449 if( T != NULL && ! p_eq_g )
1450 {
1451 for( i = 0; i < pre_len; i++ )
1452 ecp_point_free( &T[i] );
1453 polarssl_free( T );
1454 }
1455
1456 ecp_point_free( &S[1] );
1457 ecp_point_free( &S[0] );
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001458 ecp_point_free( &Q );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001459 mpi_free( &M );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001460
1461 return( ret );
1462}
1463
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001464/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001465 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1466 */
1467int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1468{
1469 int ret;
1470 mpi YY, RHS;
1471
1472 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001473 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001474
1475 /*
1476 * pt coordinates must be normalized for our checks
1477 */
1478 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001479 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001480
1481 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1482 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1483 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1484 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001485 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001486
1487 mpi_init( &YY ); mpi_init( &RHS );
1488
1489 /*
1490 * YY = Y^2
1491 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
1492 */
1493 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1494 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
1495 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1496 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1497 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
1498
1499 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001500 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001501
1502cleanup:
1503
1504 mpi_free( &YY ); mpi_free( &RHS );
1505
1506 return( ret );
1507}
1508
1509/*
1510 * Check that an mpi is valid as a private key (SEC1 3.2)
1511 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001512int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001513{
1514 /* We want 1 <= d <= N-1 */
1515 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001516 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001517
1518 return( 0 );
1519}
1520
1521/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001522 * Generate a keypair (SEC1 3.2.1)
1523 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001524int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001525 int (*f_rng)(void *, unsigned char *, size_t),
1526 void *p_rng )
1527{
1528 int count = 0;
1529 size_t n_size = (grp->nbits + 7) / 8;
1530
1531 /*
1532 * Generate d such that 1 <= n < N
1533 */
1534 do
1535 {
1536 mpi_fill_random( d, n_size, f_rng, p_rng );
1537
1538 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1539 mpi_shift_r( d, 1 );
1540
1541 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001542 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001543 }
1544 while( mpi_cmp_int( d, 1 ) < 0 );
1545
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001546 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001547}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001548
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001549#if defined(POLARSSL_SELF_TEST)
1550
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001551/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001552 * Checkup routine
1553 */
1554int ecp_self_test( int verbose )
1555{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001556 int ret;
1557 size_t i;
1558 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001559 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001560 mpi m;
1561 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001562 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001563 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001564 "000000000000000000000000000000000000000000000000", /* zero */
1565 "000000000000000000000000000000000000000000000001", /* one */
1566 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1567 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001568 "400000000000000000000000000000000000000000000000",
1569 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1570 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001571 };
1572
1573 ecp_group_init( &grp );
1574 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001575 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001576 mpi_init( &m );
1577
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001578#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001579 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001580#else
1581#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1582 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1583#else
1584#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1585 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1586#else
1587#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1588 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1589#else
1590#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1591 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1592#else
1593#error No curves defines
1594#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1595#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1596#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1597#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1598#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001599
1600 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001601 printf( " ECP test #1 (constant op_count, base point G): " );
1602
1603 /* Do a dummy multiplication first to trigger precomputation */
1604 MPI_CHK( mpi_lset( &m, 2 ) );
1605 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001606
1607 add_count = 0;
1608 dbl_count = 0;
1609 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001610 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001611
1612 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1613 {
1614 add_c_prev = add_count;
1615 dbl_c_prev = dbl_count;
1616 add_count = 0;
1617 dbl_count = 0;
1618
1619 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001620 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001621
1622 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1623 {
1624 if( verbose != 0 )
1625 printf( "failed (%zu)\n", i );
1626
1627 ret = 1;
1628 goto cleanup;
1629 }
1630 }
1631
1632 if( verbose != 0 )
1633 printf( "passed\n" );
1634
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001635 if( verbose != 0 )
1636 printf( " ECP test #2 (constant op_count, other point): " );
1637 /* We computed P = 2G last time, use it */
1638
1639 add_count = 0;
1640 dbl_count = 0;
1641 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1642 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1643
1644 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1645 {
1646 add_c_prev = add_count;
1647 dbl_c_prev = dbl_count;
1648 add_count = 0;
1649 dbl_count = 0;
1650
1651 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1652 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1653
1654 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1655 {
1656 if( verbose != 0 )
1657 printf( "failed (%zu)\n", i );
1658
1659 ret = 1;
1660 goto cleanup;
1661 }
1662 }
1663
1664 if( verbose != 0 )
1665 printf( "passed\n" );
1666
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001667cleanup:
1668
1669 if( ret < 0 && verbose != 0 )
1670 printf( "Unexpected error, return code = %08X\n", ret );
1671
1672 ecp_group_free( &grp );
1673 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001674 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001675 mpi_free( &m );
1676
1677 if( verbose != 0 )
1678 printf( "\n" );
1679
1680 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001681}
1682
1683#endif
1684
1685#endif