blob: 47216e0341e0b4dd5b88f7a68069acd8f6d1bc0c [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
73 * - TLS NamedCurve number (RFC 4492 section 5.1.1)
74 * - size in bits
75 */
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +020076const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020077{
78#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
79 { POLARSSL_ECP_DP_SECP521R1, 25, 521, },
80#endif
81#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
82 { POLARSSL_ECP_DP_SECP384R1, 24, 384, },
83#endif
84#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
85 { POLARSSL_ECP_DP_SECP256R1, 23, 256, },
86#endif
87#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
88 { POLARSSL_ECP_DP_SECP224R1, 21, 224, },
89#endif
90#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
91 { POLARSSL_ECP_DP_SECP192R1, 19, 192, },
92#endif
93 { POLARSSL_ECP_DP_NONE, 0, 0 },
94};
95
96/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010097 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010098 */
99void ecp_point_init( ecp_point *pt )
100{
101 if( pt == NULL )
102 return;
103
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100104 mpi_init( &pt->X );
105 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100106 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100107}
108
109/*
110 * Initialize (the components of) a group
111 */
112void ecp_group_init( ecp_group *grp )
113{
114 if( grp == NULL )
115 return;
116
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200117 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100118}
119
120/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200121 * Initialize (the components of) a key pair
122 */
123void ecp_keypair_init( ecp_keypair *key )
124{
125 if ( key == NULL )
126 return;
127
128 ecp_group_init( &key->grp );
129 mpi_init( &key->d );
130 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200131}
132
133/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100134 * Unallocate (the components of) a point
135 */
136void ecp_point_free( ecp_point *pt )
137{
138 if( pt == NULL )
139 return;
140
141 mpi_free( &( pt->X ) );
142 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100143 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100144}
145
146/*
147 * Unallocate (the components of) a group
148 */
149void ecp_group_free( ecp_group *grp )
150{
151 if( grp == NULL )
152 return;
153
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100154 mpi_free( &grp->P );
155 mpi_free( &grp->B );
156 ecp_point_free( &grp->G );
157 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200158
159 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100160}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100161
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100162/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200163 * Unallocate (the components of) a key pair
164 */
165void ecp_keypair_free( ecp_keypair *key )
166{
167 if ( key == NULL )
168 return;
169
170 ecp_group_free( &key->grp );
171 mpi_free( &key->d );
172 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200173}
174
175/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100176 * Set point to zero
177 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100178int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100179{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100180 int ret;
181
182 MPI_CHK( mpi_lset( &pt->X , 1 ) );
183 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
184 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
185
186cleanup:
187 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100188}
189
190/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100191 * Tell if a point is zero
192 */
193int ecp_is_zero( ecp_point *pt )
194{
195 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
196}
197
198/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100199 * Copy the contents of Q into P
200 */
201int ecp_copy( ecp_point *P, const ecp_point *Q )
202{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100203 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100204
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100205 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
206 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100207 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100208
209cleanup:
210 return( ret );
211}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100212
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100213/*
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200214 * Copy the contents of a group object
215 */
216int ecp_group_copy( ecp_group *dst, const ecp_group *src )
217{
218 return ecp_use_known_dp( dst, src->id );
219}
220
221/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100222 * Import a non-zero point from ASCII strings
223 */
224int ecp_point_read_string( ecp_point *P, int radix,
225 const char *x, const char *y )
226{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100227 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100228
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100229 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
230 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100231 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100232
233cleanup:
234 return( ret );
235}
236
237/*
238 * Import an ECP group from ASCII strings
239 */
240int ecp_group_read_string( ecp_group *grp, int radix,
241 const char *p, const char *b,
242 const char *gx, const char *gy, const char *n)
243{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100244 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100245
246 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
247 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
248 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
249 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
250
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100251 grp->pbits = mpi_msb( &grp->P );
252 grp->nbits = mpi_msb( &grp->N );
253
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100254cleanup:
255 return( ret );
256}
257
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100258/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100259 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100260 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100261int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100262 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100263 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100264{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200265 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100266 size_t plen;
267
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100268 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
269 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100270 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100271
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100272 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100273 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100274 */
275 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
276 {
277 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100278 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100279
280 buf[0] = 0x00;
281 *olen = 1;
282
283 return( 0 );
284 }
285
286 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100287
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100288 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
289 {
290 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100291
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100292 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100293 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100294
295 buf[0] = 0x04;
296 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
297 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
298 }
299 else if( format == POLARSSL_ECP_PF_COMPRESSED )
300 {
301 *olen = plen + 1;
302
303 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100304 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100305
306 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
307 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
308 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100309
310cleanup:
311 return( ret );
312}
313
314/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100315 * Import a point from unsigned binary data (SEC1 2.3.4)
316 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100317int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
318 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100319 int ret;
320 size_t plen;
321
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100322 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100323 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100324
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100325 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100326
327 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100328 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100329
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100330 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
331 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
332 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100333
334cleanup:
335 return( ret );
336}
337
338/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100339 * Import a point from a TLS ECPoint record (RFC 4492)
340 * struct {
341 * opaque point <1..2^8-1>;
342 * } ECPoint;
343 */
344int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100345 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100346{
347 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100348 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100349
350 /*
351 * We must have at least two bytes (1 for length, at least of for data)
352 */
353 if( buf_len < 2 )
354 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
355
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100356 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100357 if( data_len < 1 || data_len > buf_len - 1 )
358 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
359
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100360 /*
361 * Save buffer start for read_binary and update buf
362 */
363 buf_start = *buf;
364 *buf += data_len;
365
366 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100367}
368
369/*
370 * Export a point as a TLS ECPoint record (RFC 4492)
371 * struct {
372 * opaque point <1..2^8-1>;
373 * } ECPoint;
374 */
375int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100376 int format, size_t *olen,
377 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100378{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100379 int ret;
380
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100381 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100382 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100383 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100384 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100385 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
386
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100387 if( ( ret = ecp_point_write_binary( grp, pt, format,
388 olen, buf + 1, blen - 1) ) != 0 )
389 return( ret );
390
391 /*
392 * write length to the first byte and update total length
393 */
394 buf[0] = *olen;
395 ++*olen;
396
397 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100398}
399
400/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100401 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
402 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100403 */
404static int ecp_modp( mpi *N, const ecp_group *grp )
405{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100406 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100407
408 if( grp->modp == NULL )
409 return( mpi_mod_mpi( N, N, &grp->P ) );
410
411 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200412 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100413
414 MPI_CHK( grp->modp( N ) );
415
416 while( mpi_cmp_int( N, 0 ) < 0 )
417 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
418
419 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
420 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
421
422cleanup:
423 return( ret );
424}
425
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200426#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100427/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100428 * 192 bits in terms of t_uint
429 */
430#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
431
432/*
433 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
434 * -1 means let this chunk be 0
435 * a positive value i means A_i.
436 */
437#define P192_CHUNKS 3
438#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
439#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
440
441const signed char p192_tbl[][P192_CHUNKS] = {
442 { -1, 3, 3 }, /* S1 */
443 { 4, 4, -1 }, /* S2 */
444 { 5, 5, 5 }, /* S3 */
445};
446
447/*
448 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
449 */
450static int ecp_mod_p192( mpi *N )
451{
452 int ret;
453 unsigned char i, j, offset;
454 signed char chunk;
455 mpi tmp, acc;
456 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
457
458 tmp.s = 1;
459 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
460 tmp.p = tmp_p;
461
462 acc.s = 1;
463 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
464 acc.p = acc_p;
465
466 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
467
468 /*
469 * acc = T
470 */
471 memset( acc_p, 0, sizeof( acc_p ) );
472 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
473
474 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
475 {
476 /*
477 * tmp = S_i
478 */
479 memset( tmp_p, 0, sizeof( tmp_p ) );
480 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
481 {
482 chunk = p192_tbl[i][j];
483 if( chunk >= 0 )
484 memcpy( tmp_p + offset * P192_CHUNK_INT,
485 N->p + chunk * P192_CHUNK_INT,
486 P192_CHUNK_CHAR );
487 }
488
489 /*
490 * acc += tmp
491 */
492 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
493 }
494
495 MPI_CHK( mpi_copy( N, &acc ) );
496
497cleanup:
498 return( ret );
499}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200500#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100501
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200502#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100503/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100504 * Size of p521 in terms of t_uint
505 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100506#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100507
508/*
509 * Bits to keep in the most significant t_uint
510 */
511#if defined(POLARSS_HAVE_INT8)
512#define P521_MASK 0x01
513#else
514#define P521_MASK 0x01FF
515#endif
516
517/*
518 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100519 */
520static int ecp_mod_p521( mpi *N )
521{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100522 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100523 t_uint Mp[P521_SIZE_INT];
524 mpi M;
525
526 if( N->n < P521_SIZE_INT )
527 return( 0 );
528
529 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
530 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
531 Mp[P521_SIZE_INT - 1] &= P521_MASK;
532
533 M.s = 1;
534 M.n = P521_SIZE_INT;
535 M.p = Mp;
536
537 MPI_CHK( mpi_shift_r( N, 521 ) );
538
539 MPI_CHK( mpi_add_abs( N, N, &M ) );
540
541cleanup:
542 return( ret );
543}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200544#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100545
546/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100547 * Domain parameters for secp192r1
548 */
549#define SECP192R1_P \
550 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
551#define SECP192R1_B \
552 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
553#define SECP192R1_GX \
554 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
555#define SECP192R1_GY \
556 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
557#define SECP192R1_N \
558 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
559
560/*
561 * Domain parameters for secp224r1
562 */
563#define SECP224R1_P \
564 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
565#define SECP224R1_B \
566 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
567#define SECP224R1_GX \
568 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
569#define SECP224R1_GY \
570 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
571#define SECP224R1_N \
572 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
573
574/*
575 * Domain parameters for secp256r1
576 */
577#define SECP256R1_P \
578 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
579#define SECP256R1_B \
580 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
581#define SECP256R1_GX \
582 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
583#define SECP256R1_GY \
584 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
585#define SECP256R1_N \
586 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
587
588/*
589 * Domain parameters for secp384r1
590 */
591#define SECP384R1_P \
592 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
593 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
594#define SECP384R1_B \
595 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
596 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
597#define SECP384R1_GX \
598 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
599 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
600#define SECP384R1_GY \
601 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
602 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
603#define SECP384R1_N \
604 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
605 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
606
607/*
608 * Domain parameters for secp521r1
609 */
610#define SECP521R1_P \
611 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
612 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
613 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
614#define SECP521R1_B \
615 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
616 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
617 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
618#define SECP521R1_GX \
619 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
620 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
621 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
622#define SECP521R1_GY \
623 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
624 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
625 "3FAD0761353C7086A272C24088BE94769FD16650"
626#define SECP521R1_N \
627 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
628 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
629 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
630
631/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100632 * Set a group using well-known domain parameters
633 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100634int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100635{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100636 grp->id = id;
637
638 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100639 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200640#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100641 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100642 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100643 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100644 SECP192R1_P, SECP192R1_B,
645 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200646#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100647
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200648#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100649 case POLARSSL_ECP_DP_SECP224R1:
650 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100651 SECP224R1_P, SECP224R1_B,
652 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200653#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100654
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200655#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100656 case POLARSSL_ECP_DP_SECP256R1:
657 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100658 SECP256R1_P, SECP256R1_B,
659 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200660#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100661
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200662#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100663 case POLARSSL_ECP_DP_SECP384R1:
664 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100665 SECP384R1_P, SECP384R1_B,
666 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200667#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100668
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200669#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100670 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100671 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100672 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100673 SECP521R1_P, SECP521R1_B,
674 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200675#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100676
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200677 default:
678 grp->id = POLARSSL_ECP_DP_NONE;
679 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
680 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100681}
682
683/*
684 * Set a group from an ECParameters record (RFC 4492)
685 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100686int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100687{
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200688 unsigned int named_curve;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100689
690 /*
691 * We expect at least three bytes (see below)
692 */
693 if( len < 3 )
694 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
695
696 /*
697 * First byte is curve_type; only named_curve is handled
698 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100699 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100700 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
701
702 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100703 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100704 */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200705 named_curve = *(*buf)++;
706 named_curve <<= 8;
707 named_curve |= *(*buf)++;
708 return ecp_use_known_dp( grp, ecp_grp_id_from_named_curve( named_curve ) );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100709}
710
711/*
712 * Write the ECParameters record corresponding to a group (RFC 4492)
713 */
714int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
715 unsigned char *buf, size_t blen )
716{
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200717 unsigned int named_curve;
718
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100719 /*
720 * We are going to write 3 bytes (see below)
721 */
722 *olen = 3;
723 if( blen < *olen )
724 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
725
726 /*
727 * First byte is curve_type, always named_curve
728 */
729 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
730
731 /*
732 * Next two bytes are the namedcurve value
733 */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200734 named_curve = ecp_named_curve_from_grp_id( grp->id );
735 buf[0] = named_curve >> 8;
736 buf[1] = named_curve & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100737
738 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100739}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100740
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200741/*
742 * Get the internal identifer from the TLS name
743 */
744ecp_group_id ecp_grp_id_from_named_curve( uint16_t name )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200745{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200746 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200747
748 for( curve_info = ecp_supported_curves;
749 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
750 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200751 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200752 if( curve_info->name == name )
753 return( curve_info->grp_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200754 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200755
756 return( POLARSSL_ECP_DP_NONE );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200757}
758
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200759/*
760 * Get the TLS name for the internal identifer
761 */
762uint16_t ecp_named_curve_from_grp_id( ecp_group_id 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
766 for( curve_info = ecp_supported_curves;
767 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
768 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200769 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200770 if( curve_info->grp_id == id )
771 return( curve_info->name );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200772 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200773
774 return( 0 );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200775}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200776
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100777/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100778 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100779 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100780 * In order to guarantee that, we need to ensure that operands of
781 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100782 * bring the result back to this range.
783 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100784 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100785 */
786
787/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100788 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
789 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100790#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100791
792/*
793 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
794 */
795#define MOD_SUB( N ) \
796 while( mpi_cmp_int( &N, 0 ) < 0 ) \
797 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
798
799/*
800 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
801 */
802#define MOD_ADD( N ) \
803 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
804 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
805
806/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100807 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100808 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100809static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100810{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100811 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100812 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100813
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100814 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100815 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100816
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100817 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100818
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100819 /*
820 * X = X / Z^2 mod p
821 */
822 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
823 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
824 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100825
826 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100827 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100828 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100829 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
830 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100831
832 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100833 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100834 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100835 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100836
837cleanup:
838
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100839 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100840
841 return( ret );
842}
843
844/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100845 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100846 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100847 * (See for example Cohen's "A Course in Computational Algebraic Number
848 * Theory", Algorithm 10.3.4.)
849 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200850 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100851 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100852 */
853static int ecp_normalize_many( const ecp_group *grp,
854 ecp_point T[], size_t t_len )
855{
856 int ret;
857 size_t i;
858 mpi *c, u, Zi, ZZi;
859
860 if( t_len < 2 )
861 return( ecp_normalize( grp, T ) );
862
Paul Bakker6e339b52013-07-03 13:37:05 +0200863 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200864 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100865
866 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
867 for( i = 0; i < t_len; i++ )
868 mpi_init( &c[i] );
869
870 /*
871 * c[i] = Z_0 * ... * Z_i
872 */
873 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
874 for( i = 1; i < t_len; i++ )
875 {
876 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
877 MOD_MUL( c[i] );
878 }
879
880 /*
881 * u = 1 / (Z_0 * ... * Z_n) mod P
882 */
883 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
884
885 for( i = t_len - 1; ; i-- )
886 {
887 /*
888 * Zi = 1 / Z_i mod p
889 * u = 1 / (Z_0 * ... * Z_i) mod P
890 */
891 if( i == 0 ) {
892 MPI_CHK( mpi_copy( &Zi, &u ) );
893 }
894 else
895 {
896 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
897 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
898 }
899
900 /*
901 * proceed as in normalize()
902 */
903 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
904 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
905 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
906 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
907 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
908
909 if( i == 0 )
910 break;
911 }
912
913cleanup:
914
915 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
916 for( i = 0; i < t_len; i++ )
917 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200918 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100919
920 return( ret );
921}
922
923
924/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100925 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
926 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100927static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
928 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100929{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100930 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100931 mpi T1, T2, T3, X, Y, Z;
932
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100933#if defined(POLARSSL_SELF_TEST)
934 dbl_count++;
935#endif
936
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100937 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100938 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100939
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100940 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
941 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
942
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100943 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
944 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
945 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
946 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
947 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
948 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
949 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
950 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
951 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
952 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100953
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100954 /*
955 * For Y = Y / 2 mod p, we must make sure that Y is even before
956 * using right-shift. No need to reduce mod p afterwards.
957 */
958 if( mpi_get_bit( &Y, 0 ) == 1 )
959 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
960 MPI_CHK( mpi_shift_r( &Y, 1 ) );
961
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100962 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
963 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
964 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
965 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
966 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
967 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100968
969 MPI_CHK( mpi_copy( &R->X, &X ) );
970 MPI_CHK( mpi_copy( &R->Y, &Y ) );
971 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100972
973cleanup:
974
975 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
976 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
977
978 return( ret );
979}
980
981/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100982 * Addition or subtraction: R = P + Q or R = P + Q,
983 * mixed affine-Jacobian coordinates (GECC 3.22)
984 *
985 * The coordinates of Q must be normalized (= affine),
986 * but those of P don't need to. R is not normalized.
987 *
988 * If sign >= 0, perform addition, otherwise perform subtraction,
989 * taking advantage of the fact that, for Q != 0, we have
990 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100991 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100992static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100993 const ecp_point *P, const ecp_point *Q,
994 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100995{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100996 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100997 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100998
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100999#if defined(POLARSSL_SELF_TEST)
1000 add_count++;
1001#endif
1002
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001003 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001004 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001005 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001006 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001007 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1008 return( ecp_copy( R, P ) );
1009
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001010 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1011 {
1012 ret = ecp_copy( R, Q );
1013
1014 /*
1015 * -R.Y mod P = P - R.Y unless R.Y == 0
1016 */
1017 if( ret == 0 && sign < 0)
1018 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1019 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1020
1021 return( ret );
1022 }
1023
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001024 /*
1025 * Make sure Q coordinates are normalized
1026 */
1027 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001028 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001029
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001030 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1031 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001032
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001033 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1034 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1035 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1036 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001037
1038 /*
1039 * For subtraction, -Q.Y should have been used instead of Q.Y,
1040 * so we replace T2 by -T2, which is P - T2 mod P
1041 */
1042 if( sign < 0 )
1043 {
1044 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1045 MOD_SUB( T2 );
1046 }
1047
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001048 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1049 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001050
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001051 if( mpi_cmp_int( &T1, 0 ) == 0 )
1052 {
1053 if( mpi_cmp_int( &T2, 0 ) == 0 )
1054 {
1055 ret = ecp_double_jac( grp, R, P );
1056 goto cleanup;
1057 }
1058 else
1059 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001060 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001061 goto cleanup;
1062 }
1063 }
1064
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001065 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1066 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1067 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1068 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1069 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1070 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1071 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1072 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1073 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1074 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1075 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1076 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001077
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001078 MPI_CHK( mpi_copy( &R->X, &X ) );
1079 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1080 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001081
1082cleanup:
1083
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001084 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1085 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001086
1087 return( ret );
1088}
1089
1090/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001091 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001092 */
1093int ecp_add( const ecp_group *grp, ecp_point *R,
1094 const ecp_point *P, const ecp_point *Q )
1095{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001096 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001097
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001098 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1099 MPI_CHK( ecp_normalize( grp, R ) );
1100
1101cleanup:
1102 return( ret );
1103}
1104
1105/*
1106 * Subtraction: R = P - Q, result's coordinates normalized
1107 */
1108int ecp_sub( const ecp_group *grp, ecp_point *R,
1109 const ecp_point *P, const ecp_point *Q )
1110{
1111 int ret;
1112
1113 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001114 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001115
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001116cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001117 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001118}
1119
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001120/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001121 * Compute a modified width-w non-adjacent form (NAF) of a number,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001122 * with a fixed pattern for resistance to simple timing attacks (even SPA),
1123 * see [1]. (The resulting multiplication algorithm can also been seen as a
1124 * modification of 2^w-ary multiplication, with signed coefficients, all of
1125 * them odd.)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001126 *
1127 * Input:
1128 * m must be an odd positive mpi less than w * k bits long
1129 * x must be an array of k elements
1130 * w must be less than a certain maximum (currently 8)
1131 *
1132 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1133 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1134 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1135 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1136 *
1137 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1138 * p. 335 of the cited reference, here we return only u, not d_w since
1139 * it is known that the other d_w[j] will be 0. Moreover, the returned
1140 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1141 * that u_i is odd. Also, since we always select a positive value for d
1142 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1143 * does. Finally, there is an off-by-one error in the reference: the
1144 * last index should be k-1, not k.
1145 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001146static int ecp_w_naf_fixed( signed char x[], size_t k,
1147 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001148{
1149 int ret;
1150 unsigned int i, u, mask, carry;
1151 mpi M;
1152
1153 mpi_init( &M );
1154
1155 MPI_CHK( mpi_copy( &M, m ) );
1156 mask = ( 1 << w ) - 1;
1157 carry = 1 << ( w - 1 );
1158
1159 for( i = 0; i < k; i++ )
1160 {
1161 u = M.p[0] & mask;
1162
1163 if( ( u & 1 ) == 0 && i > 0 )
1164 x[i - 1] -= carry;
1165
1166 x[i] = u >> 1;
1167 mpi_shift_r( &M, w );
1168 }
1169
1170 /*
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001171 * We should have consumed all bits, unless the input value was too big
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001172 */
1173 if( mpi_cmp_int( &M, 0 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001174 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001175
1176cleanup:
1177
1178 mpi_free( &M );
1179
1180 return( ret );
1181}
1182
1183/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001184 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1185 * The table is filled with T[i] = (2 * i + 1) P.
1186 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001187static int ecp_precompute( const ecp_group *grp,
1188 ecp_point T[], size_t t_len,
1189 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001190{
1191 int ret;
1192 size_t i;
1193 ecp_point PP;
1194
1195 ecp_point_init( &PP );
1196
1197 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1198
1199 MPI_CHK( ecp_copy( &T[0], P ) );
1200
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001201 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001202 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1203
1204 /*
1205 * T[0] = P already has normalized coordinates
1206 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001207 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001208
1209cleanup:
1210
1211 ecp_point_free( &PP );
1212
1213 return( ret );
1214}
1215
1216/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001217 * Randomize jacobian coordinates:
1218 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1219 * This is sort of the reverse operation of ecp_normalize().
1220 */
1221static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1222 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1223{
1224 int ret;
1225 mpi l, ll;
1226 size_t p_size = (grp->pbits + 7) / 8;
1227 int count = 0;
1228
1229 mpi_init( &l ); mpi_init( &ll );
1230
1231 /* Generate l such that 1 < l < p */
1232 do
1233 {
1234 mpi_fill_random( &l, p_size, f_rng, p_rng );
1235
1236 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1237 mpi_shift_r( &l, 1 );
1238
1239 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001240 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001241 }
1242 while( mpi_cmp_int( &l, 1 ) <= 0 );
1243
1244 /* Z = l * Z */
1245 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1246
1247 /* X = l^2 * X */
1248 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1249 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1250
1251 /* Y = l^3 * Y */
1252 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1253 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1254
1255cleanup:
1256 mpi_free( &l ); mpi_free( &ll );
1257
1258 return( ret );
1259}
1260
1261/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001262 * Maximum length of the precomputed table
1263 */
1264#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1265
1266/*
1267 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1268 * (that is: grp->nbits / w + 1)
1269 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1270 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001271#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001272
1273/*
1274 * Integer multiplication: R = m * P
1275 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001276 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed().
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001277 *
1278 * This function executes a fixed number of operations for
1279 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001280 *
1281 * As an additional countermeasure against potential elaborate timing attacks,
1282 * we randomize coordinates after each addition. This was suggested as a
1283 * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that
1284 * we use jacobian coordinates, not standard projective coordinates).
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001285 */
1286int ecp_mul( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001287 const mpi *m, const ecp_point *P,
1288 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001289{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001290 int ret;
1291 unsigned char w, m_is_odd;
1292 size_t pre_len, naf_len, i, j;
1293 signed char naf[ MAX_NAF_LEN ];
1294 ecp_point Q, T[ MAX_PRE_LEN ];
1295 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001296
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001297 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001298 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001299
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001300 w = grp->nbits >= 521 ? 6 :
1301 grp->nbits >= 224 ? 5 :
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001302 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001303
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001304 /*
1305 * Make sure w is within the limits.
1306 * The last test ensures that none of the precomputed points is zero,
1307 * which wouldn't be handled correctly by ecp_normalize_many().
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001308 * It is only useful for very small curves, as used in the test suite.
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001309 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001310 if( w > POLARSSL_ECP_WINDOW_SIZE )
1311 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001312 if( w < 2 || w >= grp->nbits )
1313 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001314
1315 pre_len = 1 << ( w - 1 );
1316 naf_len = grp->nbits / w + 1;
1317
1318 mpi_init( &M );
1319 ecp_point_init( &Q );
1320 for( i = 0; i < pre_len; i++ )
1321 ecp_point_init( &T[i] );
1322
1323 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1324
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001325 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001326 * Make sure M is odd:
1327 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001328 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001329 MPI_CHK( mpi_copy( &M, m ) );
1330 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001331
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001332 /*
1333 * Compute the fixed-pattern NAF and precompute odd multiples
1334 */
1335 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1336 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001337
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001338 /*
1339 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1340 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1341 *
1342 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1343 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1344 * == T[ - naf[i] - 1 ]
1345 */
1346 MPI_CHK( ecp_set_zero( &Q ) );
1347 i = naf_len - 1;
1348 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001349 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001350 if( naf[i] < 0 )
1351 {
1352 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1353 }
1354 else
1355 {
1356 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1357 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001358
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001359 /* Countermeasure (see comments above) */
1360 if( f_rng != NULL )
1361 ecp_randomize_coordinates( grp, &Q, f_rng, p_rng );
1362
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001363 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001364 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001365 i--;
1366
1367 for( j = 0; j < w; j++ )
1368 {
1369 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1370 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001371 }
1372
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001373 /*
1374 * Now get m * P from M * P.
1375 * Since we don't need T[] any more, we can recycle it:
1376 * we already have T[0] = P, now set T[1] = 2 * P.
1377 */
1378 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1379 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001380
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001381
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001382cleanup:
1383
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001384 mpi_free( &M );
1385 ecp_point_free( &Q );
1386 for( i = 0; i < pre_len; i++ )
1387 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001388
1389 return( ret );
1390}
1391
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001392/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001393 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1394 */
1395int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1396{
1397 int ret;
1398 mpi YY, RHS;
1399
1400 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001401 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001402
1403 /*
1404 * pt coordinates must be normalized for our checks
1405 */
1406 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001407 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001408
1409 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1410 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1411 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1412 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001413 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001414
1415 mpi_init( &YY ); mpi_init( &RHS );
1416
1417 /*
1418 * YY = Y^2
1419 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
1420 */
1421 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1422 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
1423 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1424 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1425 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
1426
1427 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001428 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001429
1430cleanup:
1431
1432 mpi_free( &YY ); mpi_free( &RHS );
1433
1434 return( ret );
1435}
1436
1437/*
1438 * Check that an mpi is valid as a private key (SEC1 3.2)
1439 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001440int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001441{
1442 /* We want 1 <= d <= N-1 */
1443 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001444 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001445
1446 return( 0 );
1447}
1448
1449/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001450 * Generate a keypair (SEC1 3.2.1)
1451 */
1452int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1453 int (*f_rng)(void *, unsigned char *, size_t),
1454 void *p_rng )
1455{
1456 int count = 0;
1457 size_t n_size = (grp->nbits + 7) / 8;
1458
1459 /*
1460 * Generate d such that 1 <= n < N
1461 */
1462 do
1463 {
1464 mpi_fill_random( d, n_size, f_rng, p_rng );
1465
1466 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1467 mpi_shift_r( d, 1 );
1468
1469 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001470 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001471 }
1472 while( mpi_cmp_int( d, 1 ) < 0 );
1473
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001474 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001475}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001476
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001477#if defined(POLARSSL_SELF_TEST)
1478
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001479/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001480 * Checkup routine
1481 */
1482int ecp_self_test( int verbose )
1483{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001484 int ret;
1485 size_t i;
1486 ecp_group grp;
1487 ecp_point R;
1488 mpi m;
1489 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001490 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001491 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001492 "000000000000000000000000000000000000000000000000", /* zero */
1493 "000000000000000000000000000000000000000000000001", /* one */
1494 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1495 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001496 "400000000000000000000000000000000000000000000000",
1497 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1498 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001499 };
1500
1501 ecp_group_init( &grp );
1502 ecp_point_init( &R );
1503 mpi_init( &m );
1504
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001505#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001506 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001507#else
1508#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1509 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1510#else
1511#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1512 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1513#else
1514#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1515 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1516#else
1517#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1518 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1519#else
1520#error No curves defines
1521#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1522#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1523#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1524#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1525#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001526
1527 if( verbose != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001528 printf( " ECP test #1 (resistance to simple timing attacks): " );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001529
1530 add_count = 0;
1531 dbl_count = 0;
1532 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001533 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001534
1535 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1536 {
1537 add_c_prev = add_count;
1538 dbl_c_prev = dbl_count;
1539 add_count = 0;
1540 dbl_count = 0;
1541
1542 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001543 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001544
1545 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1546 {
1547 if( verbose != 0 )
1548 printf( "failed (%zu)\n", i );
1549
1550 ret = 1;
1551 goto cleanup;
1552 }
1553 }
1554
1555 if( verbose != 0 )
1556 printf( "passed\n" );
1557
1558cleanup:
1559
1560 if( ret < 0 && verbose != 0 )
1561 printf( "Unexpected error, return code = %08X\n", ret );
1562
1563 ecp_group_free( &grp );
1564 ecp_point_free( &R );
1565 mpi_free( &m );
1566
1567 if( verbose != 0 )
1568 printf( "\n" );
1569
1570 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001571}
1572
1573#endif
1574
1575#endif