blob: c8ee3a76ff61cf0fda809af47db7e8a53e2b3d75 [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é-Gonnardf24b4a72013-09-23 18:14:50 +0200706 uint16_t tls_id;
707 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100708
709 /*
710 * We expect at least three bytes (see below)
711 */
712 if( len < 3 )
713 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
714
715 /*
716 * First byte is curve_type; only named_curve is handled
717 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100718 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100719 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
720
721 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100722 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100723 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200724 tls_id = *(*buf)++;
725 tls_id <<= 8;
726 tls_id |= *(*buf)++;
727
728 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
729 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
730
731 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100732}
733
734/*
735 * Write the ECParameters record corresponding to a group (RFC 4492)
736 */
737int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
738 unsigned char *buf, size_t blen )
739{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200740 const ecp_curve_info *curve_info;
741
742 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
743 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200744
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100745 /*
746 * We are going to write 3 bytes (see below)
747 */
748 *olen = 3;
749 if( blen < *olen )
750 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
751
752 /*
753 * First byte is curve_type, always named_curve
754 */
755 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
756
757 /*
758 * Next two bytes are the namedcurve value
759 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200760 buf[0] = curve_info->tls_id >> 8;
761 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100762
763 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100764}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100765
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200766/*
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200767 * Get the curve info from the TLS identifier
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200768 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200769const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200770{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200771 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200772
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200773 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200774 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
775 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200776 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200777 if( curve_info->tls_id == tls_id )
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200778 return( curve_info );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200779 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200780
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200781 return( NULL );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200782}
783
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200784/*
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200785 * Get the curve info for the internal identifer
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200786 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200787const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200788{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200789 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200790
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200791 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200792 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
793 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200794 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200795 if( curve_info->grp_id == grp_id )
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200796 return( curve_info );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200797 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200798
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200799 return( NULL );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200800}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200801
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100802/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100803 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100804 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100805 * In order to guarantee that, we need to ensure that operands of
806 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100807 * bring the result back to this range.
808 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100809 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100810 */
811
812/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100813 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
814 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100815#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100816
817/*
818 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
819 */
820#define MOD_SUB( N ) \
821 while( mpi_cmp_int( &N, 0 ) < 0 ) \
822 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
823
824/*
825 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
826 */
827#define MOD_ADD( N ) \
828 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
829 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
830
831/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100832 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100833 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100834static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100835{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100836 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100837 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100838
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100839 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100840 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100841
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100842 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100843
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100844 /*
845 * X = X / Z^2 mod p
846 */
847 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
848 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
849 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100850
851 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100852 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100853 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100854 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
855 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100856
857 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100858 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100859 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100860 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100861
862cleanup:
863
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100864 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100865
866 return( ret );
867}
868
869/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100870 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100871 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100872 * (See for example Cohen's "A Course in Computational Algebraic Number
873 * Theory", Algorithm 10.3.4.)
874 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200875 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100876 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100877 */
878static int ecp_normalize_many( const ecp_group *grp,
879 ecp_point T[], size_t t_len )
880{
881 int ret;
882 size_t i;
883 mpi *c, u, Zi, ZZi;
884
885 if( t_len < 2 )
886 return( ecp_normalize( grp, T ) );
887
Paul Bakker6e339b52013-07-03 13:37:05 +0200888 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200889 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100890
891 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
892 for( i = 0; i < t_len; i++ )
893 mpi_init( &c[i] );
894
895 /*
896 * c[i] = Z_0 * ... * Z_i
897 */
898 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
899 for( i = 1; i < t_len; i++ )
900 {
901 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
902 MOD_MUL( c[i] );
903 }
904
905 /*
906 * u = 1 / (Z_0 * ... * Z_n) mod P
907 */
908 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
909
910 for( i = t_len - 1; ; i-- )
911 {
912 /*
913 * Zi = 1 / Z_i mod p
914 * u = 1 / (Z_0 * ... * Z_i) mod P
915 */
916 if( i == 0 ) {
917 MPI_CHK( mpi_copy( &Zi, &u ) );
918 }
919 else
920 {
921 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
922 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
923 }
924
925 /*
926 * proceed as in normalize()
927 */
928 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
929 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
930 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
931 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
932 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
933
934 if( i == 0 )
935 break;
936 }
937
938cleanup:
939
940 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
941 for( i = 0; i < t_len; i++ )
942 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200943 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100944
945 return( ret );
946}
947
948
949/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100950 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
951 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100952static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
953 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100954{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100955 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100956 mpi T1, T2, T3, X, Y, Z;
957
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100958#if defined(POLARSSL_SELF_TEST)
959 dbl_count++;
960#endif
961
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100962 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100963 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100964
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100965 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
966 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
967
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100968 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
969 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
970 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
971 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
972 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
973 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
974 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
975 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
976 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
977 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100978
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100979 /*
980 * For Y = Y / 2 mod p, we must make sure that Y is even before
981 * using right-shift. No need to reduce mod p afterwards.
982 */
983 if( mpi_get_bit( &Y, 0 ) == 1 )
984 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
985 MPI_CHK( mpi_shift_r( &Y, 1 ) );
986
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100987 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
988 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
989 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
990 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
991 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
992 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100993
994 MPI_CHK( mpi_copy( &R->X, &X ) );
995 MPI_CHK( mpi_copy( &R->Y, &Y ) );
996 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100997
998cleanup:
999
1000 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
1001 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
1002
1003 return( ret );
1004}
1005
1006/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001007 * Addition or subtraction: R = P + Q or R = P + Q,
1008 * mixed affine-Jacobian coordinates (GECC 3.22)
1009 *
1010 * The coordinates of Q must be normalized (= affine),
1011 * but those of P don't need to. R is not normalized.
1012 *
1013 * If sign >= 0, perform addition, otherwise perform subtraction,
1014 * taking advantage of the fact that, for Q != 0, we have
1015 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001016 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001017static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001018 const ecp_point *P, const ecp_point *Q,
1019 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001020{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001021 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001022 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001023
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001024#if defined(POLARSSL_SELF_TEST)
1025 add_count++;
1026#endif
1027
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001028 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001029 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001030 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001031 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001032 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1033 return( ecp_copy( R, P ) );
1034
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001035 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1036 {
1037 ret = ecp_copy( R, Q );
1038
1039 /*
1040 * -R.Y mod P = P - R.Y unless R.Y == 0
1041 */
1042 if( ret == 0 && sign < 0)
1043 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1044 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1045
1046 return( ret );
1047 }
1048
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001049 /*
1050 * Make sure Q coordinates are normalized
1051 */
1052 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001053 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001054
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001055 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1056 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001057
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001058 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1059 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1060 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1061 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001062
1063 /*
1064 * For subtraction, -Q.Y should have been used instead of Q.Y,
1065 * so we replace T2 by -T2, which is P - T2 mod P
1066 */
1067 if( sign < 0 )
1068 {
1069 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1070 MOD_SUB( T2 );
1071 }
1072
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001073 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1074 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001075
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001076 if( mpi_cmp_int( &T1, 0 ) == 0 )
1077 {
1078 if( mpi_cmp_int( &T2, 0 ) == 0 )
1079 {
1080 ret = ecp_double_jac( grp, R, P );
1081 goto cleanup;
1082 }
1083 else
1084 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001085 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001086 goto cleanup;
1087 }
1088 }
1089
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001090 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1091 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1092 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1093 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1094 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1095 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1096 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1097 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1098 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1099 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1100 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1101 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001102
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001103 MPI_CHK( mpi_copy( &R->X, &X ) );
1104 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1105 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001106
1107cleanup:
1108
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001109 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1110 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001111
1112 return( ret );
1113}
1114
1115/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001116 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001117 */
1118int ecp_add( const ecp_group *grp, ecp_point *R,
1119 const ecp_point *P, const ecp_point *Q )
1120{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001121 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001122
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001123 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1124 MPI_CHK( ecp_normalize( grp, R ) );
1125
1126cleanup:
1127 return( ret );
1128}
1129
1130/*
1131 * Subtraction: R = P - Q, result's coordinates normalized
1132 */
1133int ecp_sub( const ecp_group *grp, ecp_point *R,
1134 const ecp_point *P, const ecp_point *Q )
1135{
1136 int ret;
1137
1138 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001139 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001140
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001141cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001142 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001143}
1144
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001145/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001146 * Compute a modified width-w non-adjacent form (NAF) of a number,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001147 * with a fixed pattern for resistance to simple timing attacks (even SPA),
1148 * see [1]. (The resulting multiplication algorithm can also been seen as a
1149 * modification of 2^w-ary multiplication, with signed coefficients, all of
1150 * them odd.)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001151 *
1152 * Input:
1153 * m must be an odd positive mpi less than w * k bits long
1154 * x must be an array of k elements
1155 * w must be less than a certain maximum (currently 8)
1156 *
1157 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1158 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1159 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1160 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1161 *
1162 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1163 * p. 335 of the cited reference, here we return only u, not d_w since
1164 * it is known that the other d_w[j] will be 0. Moreover, the returned
1165 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1166 * that u_i is odd. Also, since we always select a positive value for d
1167 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1168 * does. Finally, there is an off-by-one error in the reference: the
1169 * last index should be k-1, not k.
1170 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001171static int ecp_w_naf_fixed( signed char x[], size_t k,
1172 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001173{
1174 int ret;
1175 unsigned int i, u, mask, carry;
1176 mpi M;
1177
1178 mpi_init( &M );
1179
1180 MPI_CHK( mpi_copy( &M, m ) );
1181 mask = ( 1 << w ) - 1;
1182 carry = 1 << ( w - 1 );
1183
1184 for( i = 0; i < k; i++ )
1185 {
1186 u = M.p[0] & mask;
1187
1188 if( ( u & 1 ) == 0 && i > 0 )
1189 x[i - 1] -= carry;
1190
1191 x[i] = u >> 1;
1192 mpi_shift_r( &M, w );
1193 }
1194
1195 /*
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001196 * We should have consumed all bits, unless the input value was too big
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001197 */
1198 if( mpi_cmp_int( &M, 0 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001199 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001200
1201cleanup:
1202
1203 mpi_free( &M );
1204
1205 return( ret );
1206}
1207
1208/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001209 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1210 * The table is filled with T[i] = (2 * i + 1) P.
1211 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001212static int ecp_precompute( const ecp_group *grp,
1213 ecp_point T[], size_t t_len,
1214 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001215{
1216 int ret;
1217 size_t i;
1218 ecp_point PP;
1219
1220 ecp_point_init( &PP );
1221
1222 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1223
1224 MPI_CHK( ecp_copy( &T[0], P ) );
1225
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001226 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001227 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1228
1229 /*
1230 * T[0] = P already has normalized coordinates
1231 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001232 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001233
1234cleanup:
1235
1236 ecp_point_free( &PP );
1237
1238 return( ret );
1239}
1240
1241/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001242 * Randomize jacobian coordinates:
1243 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1244 * This is sort of the reverse operation of ecp_normalize().
1245 */
1246static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1247 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1248{
1249 int ret;
1250 mpi l, ll;
1251 size_t p_size = (grp->pbits + 7) / 8;
1252 int count = 0;
1253
1254 mpi_init( &l ); mpi_init( &ll );
1255
1256 /* Generate l such that 1 < l < p */
1257 do
1258 {
1259 mpi_fill_random( &l, p_size, f_rng, p_rng );
1260
1261 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1262 mpi_shift_r( &l, 1 );
1263
1264 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001265 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001266 }
1267 while( mpi_cmp_int( &l, 1 ) <= 0 );
1268
1269 /* Z = l * Z */
1270 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1271
1272 /* X = l^2 * X */
1273 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1274 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1275
1276 /* Y = l^3 * Y */
1277 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1278 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1279
1280cleanup:
1281 mpi_free( &l ); mpi_free( &ll );
1282
1283 return( ret );
1284}
1285
1286/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001287 * Maximum length of the precomputed table
1288 */
1289#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1290
1291/*
1292 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1293 * (that is: grp->nbits / w + 1)
1294 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1295 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001296#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001297
1298/*
1299 * Integer multiplication: R = m * P
1300 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001301 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed().
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001302 *
1303 * This function executes a fixed number of operations for
1304 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001305 *
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001306 * As an additional countermeasure against potential timing attacks,
1307 * we randomize coordinates before each addition. This was suggested as a
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001308 * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that
1309 * we use jacobian coordinates, not standard projective coordinates).
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001310 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001311int ecp_mul( ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001312 const mpi *m, const ecp_point *P,
1313 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001314{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001315 int ret;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001316 unsigned char w, m_is_odd, p_eq_g;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001317 size_t pre_len, naf_len, i, j;
1318 signed char naf[ MAX_NAF_LEN ];
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001319 ecp_point Q, *T = NULL, S[2];
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001320 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001321
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001322 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001323 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001324
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001325 mpi_init( &M );
1326 ecp_point_init( &Q );
1327 ecp_point_init( &S[0] );
1328 ecp_point_init( &S[1] );
1329
1330 /*
1331 * Check if P == G
1332 */
1333 p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 &&
1334 mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1335 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
1336
1337 /*
1338 * If P == G, pre-compute a lot of points: this will be re-used later,
1339 * otherwise, choose window size depending on curve size
1340 */
1341 if( p_eq_g )
1342 w = POLARSSL_ECP_WINDOW_SIZE;
1343 else
1344 w = grp->nbits >= 512 ? 6 :
1345 grp->nbits >= 224 ? 5 :
1346 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001347
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001348 /*
1349 * Make sure w is within the limits.
1350 * The last test ensures that none of the precomputed points is zero,
1351 * which wouldn't be handled correctly by ecp_normalize_many().
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001352 * It is only useful for very small curves as used in the test suite.
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001353 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001354 if( w > POLARSSL_ECP_WINDOW_SIZE )
1355 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001356 if( w < 2 || w >= grp->nbits )
1357 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001358
1359 pre_len = 1 << ( w - 1 );
1360 naf_len = grp->nbits / w + 1;
1361
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001362 /*
1363 * Prepare precomputed points: if P == G we want to
1364 * use grp->T if already initialized, or initiliaze it.
1365 */
1366 if( ! p_eq_g || grp->T == NULL )
1367 {
1368 if( ( T = polarssl_malloc( pre_len * sizeof( ecp_point ) ) ) == NULL )
1369 {
1370 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1371 goto cleanup;
1372 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001373
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001374 for( i = 0; i < pre_len; i++ )
1375 ecp_point_init( &T[i] );
1376
1377 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
1378
1379 if( p_eq_g )
1380 {
1381 grp->T = T;
1382 grp->T_size = pre_len;
1383 }
1384 }
1385 else
1386 {
1387 T = grp->T;
1388
1389 /* Should never happen, but we want to be extra sure */
1390 if( pre_len != grp->T_size )
1391 {
1392 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
1393 goto cleanup;
1394 }
1395 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001396
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001397 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001398 * Make sure M is odd (M = m + 1 or M = m + 2)
1399 * later we'll get m * P by subtracting P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001400 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001401 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1402
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001403 MPI_CHK( mpi_copy( &M, m ) );
1404 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001405
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001406 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001407 * Compute the fixed-pattern NAF of M
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001408 */
1409 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001410
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001411 /*
1412 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1413 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1414 *
1415 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1416 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1417 * == T[ - naf[i] - 1 ]
1418 */
1419 MPI_CHK( ecp_set_zero( &Q ) );
1420 i = naf_len - 1;
1421 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001422 {
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001423 /* Countermeasure (see comments above) */
1424 if( f_rng != NULL )
1425 ecp_randomize_coordinates( grp, &Q, f_rng, p_rng );
1426
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001427 if( naf[i] < 0 )
1428 {
1429 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1430 }
1431 else
1432 {
1433 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1434 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001435
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001436 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001437 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001438 i--;
1439
1440 for( j = 0; j < w; j++ )
1441 {
1442 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1443 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001444 }
1445
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001446 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001447 * Now get m * P from M * P
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001448 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001449 MPI_CHK( ecp_copy( &S[0], P ) );
1450 MPI_CHK( ecp_add( grp, &S[1], P, P ) );
1451 MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001452
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001453
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001454cleanup:
1455
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001456 if( T != NULL && ! p_eq_g )
1457 {
1458 for( i = 0; i < pre_len; i++ )
1459 ecp_point_free( &T[i] );
1460 polarssl_free( T );
1461 }
1462
1463 ecp_point_free( &S[1] );
1464 ecp_point_free( &S[0] );
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001465 ecp_point_free( &Q );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001466 mpi_free( &M );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001467
1468 return( ret );
1469}
1470
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001471/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001472 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1473 */
1474int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1475{
1476 int ret;
1477 mpi YY, RHS;
1478
1479 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001480 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001481
1482 /*
1483 * pt coordinates must be normalized for our checks
1484 */
1485 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001486 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001487
1488 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1489 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1490 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1491 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001492 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001493
1494 mpi_init( &YY ); mpi_init( &RHS );
1495
1496 /*
1497 * YY = Y^2
1498 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
1499 */
1500 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1501 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
1502 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1503 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1504 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
1505
1506 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001507 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001508
1509cleanup:
1510
1511 mpi_free( &YY ); mpi_free( &RHS );
1512
1513 return( ret );
1514}
1515
1516/*
1517 * Check that an mpi is valid as a private key (SEC1 3.2)
1518 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001519int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001520{
1521 /* We want 1 <= d <= N-1 */
1522 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001523 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001524
1525 return( 0 );
1526}
1527
1528/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001529 * Generate a keypair (SEC1 3.2.1)
1530 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001531int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001532 int (*f_rng)(void *, unsigned char *, size_t),
1533 void *p_rng )
1534{
1535 int count = 0;
1536 size_t n_size = (grp->nbits + 7) / 8;
1537
1538 /*
1539 * Generate d such that 1 <= n < N
1540 */
1541 do
1542 {
1543 mpi_fill_random( d, n_size, f_rng, p_rng );
1544
1545 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1546 mpi_shift_r( d, 1 );
1547
1548 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001549 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001550 }
1551 while( mpi_cmp_int( d, 1 ) < 0 );
1552
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001553 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001554}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001555
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001556#if defined(POLARSSL_SELF_TEST)
1557
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001558/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001559 * Checkup routine
1560 */
1561int ecp_self_test( int verbose )
1562{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001563 int ret;
1564 size_t i;
1565 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001566 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001567 mpi m;
1568 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001569 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001570 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001571 "000000000000000000000000000000000000000000000000", /* zero */
1572 "000000000000000000000000000000000000000000000001", /* one */
1573 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1574 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001575 "400000000000000000000000000000000000000000000000",
1576 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1577 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001578 };
1579
1580 ecp_group_init( &grp );
1581 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001582 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001583 mpi_init( &m );
1584
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001585#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001586 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001587#else
1588#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1589 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1590#else
1591#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1592 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1593#else
1594#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1595 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1596#else
1597#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1598 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1599#else
1600#error No curves defines
1601#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1602#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1603#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1604#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1605#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001606
1607 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001608 printf( " ECP test #1 (constant op_count, base point G): " );
1609
1610 /* Do a dummy multiplication first to trigger precomputation */
1611 MPI_CHK( mpi_lset( &m, 2 ) );
1612 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001613
1614 add_count = 0;
1615 dbl_count = 0;
1616 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001617 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001618
1619 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1620 {
1621 add_c_prev = add_count;
1622 dbl_c_prev = dbl_count;
1623 add_count = 0;
1624 dbl_count = 0;
1625
1626 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001627 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001628
1629 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1630 {
1631 if( verbose != 0 )
1632 printf( "failed (%zu)\n", i );
1633
1634 ret = 1;
1635 goto cleanup;
1636 }
1637 }
1638
1639 if( verbose != 0 )
1640 printf( "passed\n" );
1641
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001642 if( verbose != 0 )
1643 printf( " ECP test #2 (constant op_count, other point): " );
1644 /* We computed P = 2G last time, use it */
1645
1646 add_count = 0;
1647 dbl_count = 0;
1648 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1649 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1650
1651 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1652 {
1653 add_c_prev = add_count;
1654 dbl_c_prev = dbl_count;
1655 add_count = 0;
1656 dbl_count = 0;
1657
1658 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1659 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1660
1661 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1662 {
1663 if( verbose != 0 )
1664 printf( "failed (%zu)\n", i );
1665
1666 ret = 1;
1667 goto cleanup;
1668 }
1669 }
1670
1671 if( verbose != 0 )
1672 printf( "passed\n" );
1673
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001674cleanup:
1675
1676 if( ret < 0 && verbose != 0 )
1677 printf( "Unexpected error, return code = %08X\n", ret );
1678
1679 ecp_group_free( &grp );
1680 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001681 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001682 mpi_free( &m );
1683
1684 if( verbose != 0 )
1685 printf( "\n" );
1686
1687 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001688}
1689
1690#endif
1691
1692#endif