blob: b344c81a37a8696e36c5eda5c39fd00a9847991a [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
Paul Bakkercf4365f2013-01-16 17:00:43 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 *
34 * [1] OKEYA, Katsuyuki and TAKAGI, Tsuyoshi. The width-w NAF method provides
35 * small memory and fast elliptic scalar multiplications secure against
36 * side channel attacks. In : Topics in Cryptology—CT-RSA 2003. Springer
37 * Berlin Heidelberg, 2003. p. 328-343.
38 * <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
39 *
40 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
41 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
42 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
43 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010044 */
45
46#include "polarssl/config.h"
47
48#if defined(POLARSSL_ECP_C)
49
50#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051
52#if defined(POLARSSL_MEMORY_C)
53#include "polarssl/memory.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010059#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010060#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010062#if defined(POLARSSL_SELF_TEST)
63/*
64 * Counts of point addition and doubling operations.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020065 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010066 */
67unsigned long add_count, dbl_count;
68#endif
69
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010070/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020071 * List of supported curves:
72 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020073 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020074 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020075 * - readable name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020076 */
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +020077const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020078{
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020079#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
80 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpool512r1" },
81#endif
82#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
83 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpool384r1" },
84#endif
85#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
86 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpool256r1" },
87#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020088#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020089 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020090#endif
91#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020092 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020093#endif
94#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020095 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020096#endif
97#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020098 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020099#endif
100#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200101 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200102#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200103 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200104};
105
106/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200107 * List of supported curves and associated info
108 */
109const ecp_curve_info *ecp_curve_list( void )
110{
111 return ecp_supported_curves;
112}
113
114/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100115 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100116 */
117void ecp_point_init( ecp_point *pt )
118{
119 if( pt == NULL )
120 return;
121
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100122 mpi_init( &pt->X );
123 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100124 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100125}
126
127/*
128 * Initialize (the components of) a group
129 */
130void ecp_group_init( ecp_group *grp )
131{
132 if( grp == NULL )
133 return;
134
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200135 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100136}
137
138/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200139 * Initialize (the components of) a key pair
140 */
141void ecp_keypair_init( ecp_keypair *key )
142{
143 if ( key == NULL )
144 return;
145
146 ecp_group_init( &key->grp );
147 mpi_init( &key->d );
148 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200149}
150
151/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100152 * Unallocate (the components of) a point
153 */
154void ecp_point_free( ecp_point *pt )
155{
156 if( pt == NULL )
157 return;
158
159 mpi_free( &( pt->X ) );
160 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100161 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100162}
163
164/*
165 * Unallocate (the components of) a group
166 */
167void ecp_group_free( ecp_group *grp )
168{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200169 size_t i;
170
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100171 if( grp == NULL )
172 return;
173
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100174 mpi_free( &grp->P );
175 mpi_free( &grp->B );
176 ecp_point_free( &grp->G );
177 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200178
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200179 if( grp->T != NULL )
180 {
181 for( i = 0; i < grp->T_size; i++ )
182 ecp_point_free( &grp->T[i] );
183 polarssl_free( grp->T );
184 }
185
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200186 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100187}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100188
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100189/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200190 * Unallocate (the components of) a key pair
191 */
192void ecp_keypair_free( ecp_keypair *key )
193{
194 if ( key == NULL )
195 return;
196
197 ecp_group_free( &key->grp );
198 mpi_free( &key->d );
199 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200200}
201
202/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100203 * Set point to zero
204 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100205int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100206{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100207 int ret;
208
209 MPI_CHK( mpi_lset( &pt->X , 1 ) );
210 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
211 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
212
213cleanup:
214 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100215}
216
217/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100218 * Tell if a point is zero
219 */
220int ecp_is_zero( ecp_point *pt )
221{
222 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
223}
224
225/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100226 * Copy the contents of Q into P
227 */
228int ecp_copy( ecp_point *P, const ecp_point *Q )
229{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100230 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100231
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100232 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
233 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100234 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100235
236cleanup:
237 return( ret );
238}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100239
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100240/*
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200241 * Copy the contents of a group object
242 */
243int ecp_group_copy( ecp_group *dst, const ecp_group *src )
244{
245 return ecp_use_known_dp( dst, src->id );
246}
247
248/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100249 * Import a non-zero point from ASCII strings
250 */
251int ecp_point_read_string( ecp_point *P, int radix,
252 const char *x, const char *y )
253{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100254 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100255
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100256 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
257 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100258 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100259
260cleanup:
261 return( ret );
262}
263
264/*
265 * Import an ECP group from ASCII strings
266 */
267int ecp_group_read_string( ecp_group *grp, int radix,
268 const char *p, const char *b,
269 const char *gx, const char *gy, const char *n)
270{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100271 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100272
273 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
274 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
275 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
276 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
277
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100278 grp->pbits = mpi_msb( &grp->P );
279 grp->nbits = mpi_msb( &grp->N );
280
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100281cleanup:
282 return( ret );
283}
284
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100285/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100286 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100287 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100288int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100289 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100290 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100291{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200292 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100293 size_t plen;
294
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100295 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
296 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100297 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100298
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100299 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100300 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100301 */
302 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
303 {
304 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100305 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100306
307 buf[0] = 0x00;
308 *olen = 1;
309
310 return( 0 );
311 }
312
313 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100314
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100315 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
316 {
317 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100318
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100319 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100320 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100321
322 buf[0] = 0x04;
323 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
324 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
325 }
326 else if( format == POLARSSL_ECP_PF_COMPRESSED )
327 {
328 *olen = plen + 1;
329
330 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100331 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100332
333 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
334 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
335 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100336
337cleanup:
338 return( ret );
339}
340
341/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100342 * Import a point from unsigned binary data (SEC1 2.3.4)
343 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100344int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
345 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100346 int ret;
347 size_t plen;
348
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100349 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100350 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100351
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100352 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100353
354 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100355 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100356
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100357 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
358 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
359 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100360
361cleanup:
362 return( ret );
363}
364
365/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100366 * Import a point from a TLS ECPoint record (RFC 4492)
367 * struct {
368 * opaque point <1..2^8-1>;
369 * } ECPoint;
370 */
371int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100372 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100373{
374 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100375 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100376
377 /*
378 * We must have at least two bytes (1 for length, at least of for data)
379 */
380 if( buf_len < 2 )
381 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
382
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100383 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100384 if( data_len < 1 || data_len > buf_len - 1 )
385 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
386
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100387 /*
388 * Save buffer start for read_binary and update buf
389 */
390 buf_start = *buf;
391 *buf += data_len;
392
393 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100394}
395
396/*
397 * Export a point as a TLS ECPoint record (RFC 4492)
398 * struct {
399 * opaque point <1..2^8-1>;
400 * } ECPoint;
401 */
402int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100403 int format, size_t *olen,
404 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100405{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100406 int ret;
407
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100408 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100409 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100410 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100411 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100412 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
413
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100414 if( ( ret = ecp_point_write_binary( grp, pt, format,
415 olen, buf + 1, blen - 1) ) != 0 )
416 return( ret );
417
418 /*
419 * write length to the first byte and update total length
420 */
421 buf[0] = *olen;
422 ++*olen;
423
424 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100425}
426
427/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100428 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
429 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100430 */
431static int ecp_modp( mpi *N, const ecp_group *grp )
432{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100433 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100434
435 if( grp->modp == NULL )
436 return( mpi_mod_mpi( N, N, &grp->P ) );
437
438 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200439 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100440
441 MPI_CHK( grp->modp( N ) );
442
443 while( mpi_cmp_int( N, 0 ) < 0 )
444 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
445
446 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
447 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
448
449cleanup:
450 return( ret );
451}
452
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200453#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100454/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100455 * 192 bits in terms of t_uint
456 */
457#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
458
459/*
460 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
461 * -1 means let this chunk be 0
462 * a positive value i means A_i.
463 */
464#define P192_CHUNKS 3
465#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
466#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
467
468const signed char p192_tbl[][P192_CHUNKS] = {
469 { -1, 3, 3 }, /* S1 */
470 { 4, 4, -1 }, /* S2 */
471 { 5, 5, 5 }, /* S3 */
472};
473
474/*
475 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
476 */
477static int ecp_mod_p192( mpi *N )
478{
479 int ret;
480 unsigned char i, j, offset;
481 signed char chunk;
482 mpi tmp, acc;
483 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
484
485 tmp.s = 1;
486 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
487 tmp.p = tmp_p;
488
489 acc.s = 1;
490 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
491 acc.p = acc_p;
492
493 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
494
495 /*
496 * acc = T
497 */
498 memset( acc_p, 0, sizeof( acc_p ) );
499 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
500
501 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
502 {
503 /*
504 * tmp = S_i
505 */
506 memset( tmp_p, 0, sizeof( tmp_p ) );
507 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
508 {
509 chunk = p192_tbl[i][j];
510 if( chunk >= 0 )
511 memcpy( tmp_p + offset * P192_CHUNK_INT,
512 N->p + chunk * P192_CHUNK_INT,
513 P192_CHUNK_CHAR );
514 }
515
516 /*
517 * acc += tmp
518 */
519 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
520 }
521
522 MPI_CHK( mpi_copy( N, &acc ) );
523
524cleanup:
525 return( ret );
526}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200527#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100528
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200529#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100530/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100531 * Size of p521 in terms of t_uint
532 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100533#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100534
535/*
536 * Bits to keep in the most significant t_uint
537 */
538#if defined(POLARSS_HAVE_INT8)
539#define P521_MASK 0x01
540#else
541#define P521_MASK 0x01FF
542#endif
543
544/*
545 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100546 */
547static int ecp_mod_p521( mpi *N )
548{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100549 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100550 t_uint Mp[P521_SIZE_INT];
551 mpi M;
552
553 if( N->n < P521_SIZE_INT )
554 return( 0 );
555
556 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
557 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
558 Mp[P521_SIZE_INT - 1] &= P521_MASK;
559
560 M.s = 1;
561 M.n = P521_SIZE_INT;
562 M.p = Mp;
563
564 MPI_CHK( mpi_shift_r( N, 521 ) );
565
566 MPI_CHK( mpi_add_abs( N, N, &M ) );
567
568cleanup:
569 return( ret );
570}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200571#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100572
573/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100574 * Domain parameters for secp192r1
575 */
576#define SECP192R1_P \
577 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
578#define SECP192R1_B \
579 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
580#define SECP192R1_GX \
581 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
582#define SECP192R1_GY \
583 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
584#define SECP192R1_N \
585 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
586
587/*
588 * Domain parameters for secp224r1
589 */
590#define SECP224R1_P \
591 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
592#define SECP224R1_B \
593 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
594#define SECP224R1_GX \
595 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
596#define SECP224R1_GY \
597 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
598#define SECP224R1_N \
599 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
600
601/*
602 * Domain parameters for secp256r1
603 */
604#define SECP256R1_P \
605 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
606#define SECP256R1_B \
607 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
608#define SECP256R1_GX \
609 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
610#define SECP256R1_GY \
611 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
612#define SECP256R1_N \
613 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
614
615/*
616 * Domain parameters for secp384r1
617 */
618#define SECP384R1_P \
619 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
620 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
621#define SECP384R1_B \
622 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
623 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
624#define SECP384R1_GX \
625 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
626 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
627#define SECP384R1_GY \
628 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
629 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
630#define SECP384R1_N \
631 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
632 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
633
634/*
635 * Domain parameters for secp521r1
636 */
637#define SECP521R1_P \
638 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
639 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
640 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
641#define SECP521R1_B \
642 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
643 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
644 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
645#define SECP521R1_GX \
646 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
647 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
648 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
649#define SECP521R1_GY \
650 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
651 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
652 "3FAD0761353C7086A272C24088BE94769FD16650"
653#define SECP521R1_N \
654 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
655 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
656 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
657
658/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100659 * Set a group using well-known domain parameters
660 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100661int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100662{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100663 grp->id = id;
664
665 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100666 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200667#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100668 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100669 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100670 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100671 SECP192R1_P, SECP192R1_B,
672 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200673#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100674
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200675#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100676 case POLARSSL_ECP_DP_SECP224R1:
677 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100678 SECP224R1_P, SECP224R1_B,
679 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200680#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100681
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200682#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100683 case POLARSSL_ECP_DP_SECP256R1:
684 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100685 SECP256R1_P, SECP256R1_B,
686 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200687#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100688
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200689#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100690 case POLARSSL_ECP_DP_SECP384R1:
691 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100692 SECP384R1_P, SECP384R1_B,
693 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200694#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100695
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200696#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100697 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100698 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100699 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100700 SECP521R1_P, SECP521R1_B,
701 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200702#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100703
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200704 default:
705 grp->id = POLARSSL_ECP_DP_NONE;
706 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
707 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100708}
709
710/*
711 * Set a group from an ECParameters record (RFC 4492)
712 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100713int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100714{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200715 uint16_t tls_id;
716 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100717
718 /*
719 * We expect at least three bytes (see below)
720 */
721 if( len < 3 )
722 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
723
724 /*
725 * First byte is curve_type; only named_curve is handled
726 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100727 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100728 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
729
730 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100731 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100732 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200733 tls_id = *(*buf)++;
734 tls_id <<= 8;
735 tls_id |= *(*buf)++;
736
737 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
738 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
739
740 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100741}
742
743/*
744 * Write the ECParameters record corresponding to a group (RFC 4492)
745 */
746int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
747 unsigned char *buf, size_t blen )
748{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200749 const ecp_curve_info *curve_info;
750
751 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
752 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200753
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100754 /*
755 * We are going to write 3 bytes (see below)
756 */
757 *olen = 3;
758 if( blen < *olen )
759 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
760
761 /*
762 * First byte is curve_type, always named_curve
763 */
764 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
765
766 /*
767 * Next two bytes are the namedcurve value
768 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200769 buf[0] = curve_info->tls_id >> 8;
770 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100771
772 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100773}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100774
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200775/*
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200776 * Get the curve info from the TLS identifier
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200777 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200778const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200779{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200780 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200781
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200782 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200783 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
784 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200785 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200786 if( curve_info->tls_id == tls_id )
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200787 return( curve_info );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200788 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200789
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200790 return( NULL );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200791}
792
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200793/*
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200794 * Get the curve info for the internal identifer
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200795 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200796const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200797{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200798 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200799
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200800 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200801 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
802 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200803 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200804 if( curve_info->grp_id == grp_id )
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200805 return( curve_info );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200806 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200807
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200808 return( NULL );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200809}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200810
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100811/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100812 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100813 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100814 * In order to guarantee that, we need to ensure that operands of
815 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100816 * bring the result back to this range.
817 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100818 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100819 */
820
821/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100822 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
823 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100824#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100825
826/*
827 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
828 */
829#define MOD_SUB( N ) \
830 while( mpi_cmp_int( &N, 0 ) < 0 ) \
831 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
832
833/*
834 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
835 */
836#define MOD_ADD( N ) \
837 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
838 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
839
840/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100841 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100842 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100843static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100844{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100845 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100846 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100847
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100848 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100849 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100850
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100851 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100852
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100853 /*
854 * X = X / Z^2 mod p
855 */
856 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
857 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
858 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100859
860 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100861 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100862 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100863 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
864 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100865
866 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100867 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100868 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100869 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100870
871cleanup:
872
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100873 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100874
875 return( ret );
876}
877
878/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100879 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100880 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100881 * (See for example Cohen's "A Course in Computational Algebraic Number
882 * Theory", Algorithm 10.3.4.)
883 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200884 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100885 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100886 */
887static int ecp_normalize_many( const ecp_group *grp,
888 ecp_point T[], size_t t_len )
889{
890 int ret;
891 size_t i;
892 mpi *c, u, Zi, ZZi;
893
894 if( t_len < 2 )
895 return( ecp_normalize( grp, T ) );
896
Paul Bakker6e339b52013-07-03 13:37:05 +0200897 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200898 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100899
900 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
901 for( i = 0; i < t_len; i++ )
902 mpi_init( &c[i] );
903
904 /*
905 * c[i] = Z_0 * ... * Z_i
906 */
907 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
908 for( i = 1; i < t_len; i++ )
909 {
910 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
911 MOD_MUL( c[i] );
912 }
913
914 /*
915 * u = 1 / (Z_0 * ... * Z_n) mod P
916 */
917 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
918
919 for( i = t_len - 1; ; i-- )
920 {
921 /*
922 * Zi = 1 / Z_i mod p
923 * u = 1 / (Z_0 * ... * Z_i) mod P
924 */
925 if( i == 0 ) {
926 MPI_CHK( mpi_copy( &Zi, &u ) );
927 }
928 else
929 {
930 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
931 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
932 }
933
934 /*
935 * proceed as in normalize()
936 */
937 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
938 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
939 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
940 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
941 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
942
943 if( i == 0 )
944 break;
945 }
946
947cleanup:
948
949 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
950 for( i = 0; i < t_len; i++ )
951 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200952 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100953
954 return( ret );
955}
956
957
958/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100959 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
960 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100961static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
962 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100963{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100964 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100965 mpi T1, T2, T3, X, Y, Z;
966
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100967#if defined(POLARSSL_SELF_TEST)
968 dbl_count++;
969#endif
970
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100971 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100972 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100973
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100974 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
975 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
976
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100977 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
978 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
979 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
980 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
981 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
982 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
983 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
984 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
985 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
986 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100987
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100988 /*
989 * For Y = Y / 2 mod p, we must make sure that Y is even before
990 * using right-shift. No need to reduce mod p afterwards.
991 */
992 if( mpi_get_bit( &Y, 0 ) == 1 )
993 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
994 MPI_CHK( mpi_shift_r( &Y, 1 ) );
995
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100996 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
997 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
998 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
999 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
1000 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
1001 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001002
1003 MPI_CHK( mpi_copy( &R->X, &X ) );
1004 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1005 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001006
1007cleanup:
1008
1009 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
1010 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
1011
1012 return( ret );
1013}
1014
1015/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001016 * Addition or subtraction: R = P + Q or R = P + Q,
1017 * mixed affine-Jacobian coordinates (GECC 3.22)
1018 *
1019 * The coordinates of Q must be normalized (= affine),
1020 * but those of P don't need to. R is not normalized.
1021 *
1022 * If sign >= 0, perform addition, otherwise perform subtraction,
1023 * taking advantage of the fact that, for Q != 0, we have
1024 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001025 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001026static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001027 const ecp_point *P, const ecp_point *Q,
1028 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001029{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001030 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001031 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001032
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001033#if defined(POLARSSL_SELF_TEST)
1034 add_count++;
1035#endif
1036
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001037 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001038 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001039 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001040 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001041 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1042 return( ecp_copy( R, P ) );
1043
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001044 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1045 {
1046 ret = ecp_copy( R, Q );
1047
1048 /*
1049 * -R.Y mod P = P - R.Y unless R.Y == 0
1050 */
1051 if( ret == 0 && sign < 0)
1052 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1053 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1054
1055 return( ret );
1056 }
1057
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001058 /*
1059 * Make sure Q coordinates are normalized
1060 */
1061 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001062 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001063
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001064 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1065 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001066
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001067 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1068 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1069 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1070 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001071
1072 /*
1073 * For subtraction, -Q.Y should have been used instead of Q.Y,
1074 * so we replace T2 by -T2, which is P - T2 mod P
1075 */
1076 if( sign < 0 )
1077 {
1078 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1079 MOD_SUB( T2 );
1080 }
1081
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001082 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1083 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001084
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001085 if( mpi_cmp_int( &T1, 0 ) == 0 )
1086 {
1087 if( mpi_cmp_int( &T2, 0 ) == 0 )
1088 {
1089 ret = ecp_double_jac( grp, R, P );
1090 goto cleanup;
1091 }
1092 else
1093 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001094 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001095 goto cleanup;
1096 }
1097 }
1098
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001099 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1100 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1101 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1102 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1103 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1104 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1105 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1106 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1107 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1108 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1109 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1110 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001111
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001112 MPI_CHK( mpi_copy( &R->X, &X ) );
1113 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1114 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001115
1116cleanup:
1117
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001118 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1119 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001120
1121 return( ret );
1122}
1123
1124/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001125 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001126 */
1127int ecp_add( const ecp_group *grp, ecp_point *R,
1128 const ecp_point *P, const ecp_point *Q )
1129{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001130 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001131
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001132 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1133 MPI_CHK( ecp_normalize( grp, R ) );
1134
1135cleanup:
1136 return( ret );
1137}
1138
1139/*
1140 * Subtraction: R = P - Q, result's coordinates normalized
1141 */
1142int ecp_sub( const ecp_group *grp, ecp_point *R,
1143 const ecp_point *P, const ecp_point *Q )
1144{
1145 int ret;
1146
1147 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001148 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001149
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001150cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001151 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001152}
1153
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001154/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001155 * Compute a modified width-w non-adjacent form (NAF) of a number,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001156 * with a fixed pattern for resistance to simple timing attacks (even SPA),
1157 * see [1]. (The resulting multiplication algorithm can also been seen as a
1158 * modification of 2^w-ary multiplication, with signed coefficients, all of
1159 * them odd.)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001160 *
1161 * Input:
1162 * m must be an odd positive mpi less than w * k bits long
1163 * x must be an array of k elements
1164 * w must be less than a certain maximum (currently 8)
1165 *
1166 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1167 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1168 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1169 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1170 *
1171 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1172 * p. 335 of the cited reference, here we return only u, not d_w since
1173 * it is known that the other d_w[j] will be 0. Moreover, the returned
1174 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1175 * that u_i is odd. Also, since we always select a positive value for d
1176 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1177 * does. Finally, there is an off-by-one error in the reference: the
1178 * last index should be k-1, not k.
1179 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001180static int ecp_w_naf_fixed( signed char x[], size_t k,
1181 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001182{
1183 int ret;
1184 unsigned int i, u, mask, carry;
1185 mpi M;
1186
1187 mpi_init( &M );
1188
1189 MPI_CHK( mpi_copy( &M, m ) );
1190 mask = ( 1 << w ) - 1;
1191 carry = 1 << ( w - 1 );
1192
1193 for( i = 0; i < k; i++ )
1194 {
1195 u = M.p[0] & mask;
1196
1197 if( ( u & 1 ) == 0 && i > 0 )
1198 x[i - 1] -= carry;
1199
1200 x[i] = u >> 1;
1201 mpi_shift_r( &M, w );
1202 }
1203
1204 /*
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001205 * We should have consumed all bits, unless the input value was too big
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001206 */
1207 if( mpi_cmp_int( &M, 0 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001208 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001209
1210cleanup:
1211
1212 mpi_free( &M );
1213
1214 return( ret );
1215}
1216
1217/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001218 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1219 * The table is filled with T[i] = (2 * i + 1) P.
1220 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001221static int ecp_precompute( const ecp_group *grp,
1222 ecp_point T[], size_t t_len,
1223 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001224{
1225 int ret;
1226 size_t i;
1227 ecp_point PP;
1228
1229 ecp_point_init( &PP );
1230
1231 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1232
1233 MPI_CHK( ecp_copy( &T[0], P ) );
1234
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001235 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001236 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1237
1238 /*
1239 * T[0] = P already has normalized coordinates
1240 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001241 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001242
1243cleanup:
1244
1245 ecp_point_free( &PP );
1246
1247 return( ret );
1248}
1249
1250/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001251 * Randomize jacobian coordinates:
1252 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1253 * This is sort of the reverse operation of ecp_normalize().
1254 */
1255static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1256 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1257{
1258 int ret;
1259 mpi l, ll;
1260 size_t p_size = (grp->pbits + 7) / 8;
1261 int count = 0;
1262
1263 mpi_init( &l ); mpi_init( &ll );
1264
1265 /* Generate l such that 1 < l < p */
1266 do
1267 {
1268 mpi_fill_random( &l, p_size, f_rng, p_rng );
1269
1270 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1271 mpi_shift_r( &l, 1 );
1272
1273 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001274 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001275 }
1276 while( mpi_cmp_int( &l, 1 ) <= 0 );
1277
1278 /* Z = l * Z */
1279 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1280
1281 /* X = l^2 * X */
1282 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1283 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1284
1285 /* Y = l^3 * Y */
1286 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1287 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1288
1289cleanup:
1290 mpi_free( &l ); mpi_free( &ll );
1291
1292 return( ret );
1293}
1294
1295/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001296 * Maximum length of the precomputed table
1297 */
1298#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1299
1300/*
1301 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1302 * (that is: grp->nbits / w + 1)
1303 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1304 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001305#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001306
1307/*
1308 * Integer multiplication: R = m * P
1309 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001310 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed().
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001311 *
1312 * This function executes a fixed number of operations for
1313 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001314 *
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001315 * As an additional countermeasure against potential timing attacks,
1316 * we randomize coordinates before each addition. This was suggested as a
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001317 * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that
1318 * we use jacobian coordinates, not standard projective coordinates).
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001319 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001320int ecp_mul( ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001321 const mpi *m, const ecp_point *P,
1322 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001323{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001324 int ret;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001325 unsigned char w, m_is_odd, p_eq_g;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001326 size_t pre_len, naf_len, i, j;
1327 signed char naf[ MAX_NAF_LEN ];
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001328 ecp_point Q, *T = NULL, S[2];
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001329 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001330
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001331 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001332 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001333
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001334 mpi_init( &M );
1335 ecp_point_init( &Q );
1336 ecp_point_init( &S[0] );
1337 ecp_point_init( &S[1] );
1338
1339 /*
1340 * Check if P == G
1341 */
1342 p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 &&
1343 mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1344 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
1345
1346 /*
1347 * If P == G, pre-compute a lot of points: this will be re-used later,
1348 * otherwise, choose window size depending on curve size
1349 */
1350 if( p_eq_g )
1351 w = POLARSSL_ECP_WINDOW_SIZE;
1352 else
1353 w = grp->nbits >= 512 ? 6 :
1354 grp->nbits >= 224 ? 5 :
1355 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001356
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001357 /*
1358 * Make sure w is within the limits.
1359 * The last test ensures that none of the precomputed points is zero,
1360 * which wouldn't be handled correctly by ecp_normalize_many().
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001361 * It is only useful for very small curves as used in the test suite.
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001362 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001363 if( w > POLARSSL_ECP_WINDOW_SIZE )
1364 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001365 if( w < 2 || w >= grp->nbits )
1366 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001367
1368 pre_len = 1 << ( w - 1 );
1369 naf_len = grp->nbits / w + 1;
1370
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001371 /*
1372 * Prepare precomputed points: if P == G we want to
1373 * use grp->T if already initialized, or initiliaze it.
1374 */
1375 if( ! p_eq_g || grp->T == NULL )
1376 {
1377 if( ( T = polarssl_malloc( pre_len * sizeof( ecp_point ) ) ) == NULL )
1378 {
1379 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1380 goto cleanup;
1381 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001382
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001383 for( i = 0; i < pre_len; i++ )
1384 ecp_point_init( &T[i] );
1385
1386 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
1387
1388 if( p_eq_g )
1389 {
1390 grp->T = T;
1391 grp->T_size = pre_len;
1392 }
1393 }
1394 else
1395 {
1396 T = grp->T;
1397
1398 /* Should never happen, but we want to be extra sure */
1399 if( pre_len != grp->T_size )
1400 {
1401 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
1402 goto cleanup;
1403 }
1404 }
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001405
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001406 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001407 * Make sure M is odd (M = m + 1 or M = m + 2)
1408 * later we'll get m * P by subtracting P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001409 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001410 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1411
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001412 MPI_CHK( mpi_copy( &M, m ) );
1413 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001414
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001415 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001416 * Compute the fixed-pattern NAF of M
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001417 */
1418 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001419
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001420 /*
1421 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1422 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1423 *
1424 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1425 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1426 * == T[ - naf[i] - 1 ]
1427 */
1428 MPI_CHK( ecp_set_zero( &Q ) );
1429 i = naf_len - 1;
1430 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001431 {
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001432 /* Countermeasure (see comments above) */
1433 if( f_rng != NULL )
1434 ecp_randomize_coordinates( grp, &Q, f_rng, p_rng );
1435
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001436 if( naf[i] < 0 )
1437 {
1438 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1439 }
1440 else
1441 {
1442 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1443 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001444
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001445 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001446 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001447 i--;
1448
1449 for( j = 0; j < w; j++ )
1450 {
1451 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1452 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001453 }
1454
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001455 /*
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001456 * Now get m * P from M * P
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001457 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001458 MPI_CHK( ecp_copy( &S[0], P ) );
1459 MPI_CHK( ecp_add( grp, &S[1], P, P ) );
1460 MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001461
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001462
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001463cleanup:
1464
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001465 if( T != NULL && ! p_eq_g )
1466 {
1467 for( i = 0; i < pre_len; i++ )
1468 ecp_point_free( &T[i] );
1469 polarssl_free( T );
1470 }
1471
1472 ecp_point_free( &S[1] );
1473 ecp_point_free( &S[0] );
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001474 ecp_point_free( &Q );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001475 mpi_free( &M );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001476
1477 return( ret );
1478}
1479
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001480/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001481 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1482 */
1483int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1484{
1485 int ret;
1486 mpi YY, RHS;
1487
1488 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001489 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001490
1491 /*
1492 * pt coordinates must be normalized for our checks
1493 */
1494 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001495 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001496
1497 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1498 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1499 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1500 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001501 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001502
1503 mpi_init( &YY ); mpi_init( &RHS );
1504
1505 /*
1506 * YY = Y^2
1507 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
1508 */
1509 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1510 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
1511 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1512 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1513 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
1514
1515 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001516 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001517
1518cleanup:
1519
1520 mpi_free( &YY ); mpi_free( &RHS );
1521
1522 return( ret );
1523}
1524
1525/*
1526 * Check that an mpi is valid as a private key (SEC1 3.2)
1527 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001528int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001529{
1530 /* We want 1 <= d <= N-1 */
1531 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001532 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001533
1534 return( 0 );
1535}
1536
1537/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001538 * Generate a keypair (SEC1 3.2.1)
1539 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001540int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001541 int (*f_rng)(void *, unsigned char *, size_t),
1542 void *p_rng )
1543{
1544 int count = 0;
1545 size_t n_size = (grp->nbits + 7) / 8;
1546
1547 /*
1548 * Generate d such that 1 <= n < N
1549 */
1550 do
1551 {
1552 mpi_fill_random( d, n_size, f_rng, p_rng );
1553
1554 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1555 mpi_shift_r( d, 1 );
1556
1557 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001558 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001559 }
1560 while( mpi_cmp_int( d, 1 ) < 0 );
1561
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001562 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001563}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001564
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001565#if defined(POLARSSL_SELF_TEST)
1566
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001567/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001568 * Checkup routine
1569 */
1570int ecp_self_test( int verbose )
1571{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001572 int ret;
1573 size_t i;
1574 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001575 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001576 mpi m;
1577 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001578 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001579 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001580 "000000000000000000000000000000000000000000000000", /* zero */
1581 "000000000000000000000000000000000000000000000001", /* one */
1582 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1583 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001584 "400000000000000000000000000000000000000000000000",
1585 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1586 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001587 };
1588
1589 ecp_group_init( &grp );
1590 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001591 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001592 mpi_init( &m );
1593
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001594#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001595 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001596#else
1597#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1598 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1599#else
1600#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1601 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1602#else
1603#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1604 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1605#else
1606#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1607 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1608#else
1609#error No curves defines
1610#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1611#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1612#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1613#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1614#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001615
1616 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001617 printf( " ECP test #1 (constant op_count, base point G): " );
1618
1619 /* Do a dummy multiplication first to trigger precomputation */
1620 MPI_CHK( mpi_lset( &m, 2 ) );
1621 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001622
1623 add_count = 0;
1624 dbl_count = 0;
1625 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001626 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001627
1628 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1629 {
1630 add_c_prev = add_count;
1631 dbl_c_prev = dbl_count;
1632 add_count = 0;
1633 dbl_count = 0;
1634
1635 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001636 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001637
1638 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1639 {
1640 if( verbose != 0 )
1641 printf( "failed (%zu)\n", i );
1642
1643 ret = 1;
1644 goto cleanup;
1645 }
1646 }
1647
1648 if( verbose != 0 )
1649 printf( "passed\n" );
1650
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001651 if( verbose != 0 )
1652 printf( " ECP test #2 (constant op_count, other point): " );
1653 /* We computed P = 2G last time, use it */
1654
1655 add_count = 0;
1656 dbl_count = 0;
1657 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1658 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1659
1660 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1661 {
1662 add_c_prev = add_count;
1663 dbl_c_prev = dbl_count;
1664 add_count = 0;
1665 dbl_count = 0;
1666
1667 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1668 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1669
1670 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1671 {
1672 if( verbose != 0 )
1673 printf( "failed (%zu)\n", i );
1674
1675 ret = 1;
1676 goto cleanup;
1677 }
1678 }
1679
1680 if( verbose != 0 )
1681 printf( "passed\n" );
1682
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001683cleanup:
1684
1685 if( ret < 0 && verbose != 0 )
1686 printf( "Unexpected error, return code = %08X\n", ret );
1687
1688 ecp_group_free( &grp );
1689 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001690 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001691 mpi_free( &m );
1692
1693 if( verbose != 0 )
1694 printf( "\n" );
1695
1696 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001697}
1698
1699#endif
1700
1701#endif