blob: 48ce5d37450d3a48dbfe4b121653ecf6c292d314 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
Paul Bakkercf4365f2013-01-16 17:00:43 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 *
34 * [1] OKEYA, Katsuyuki and TAKAGI, Tsuyoshi. The width-w NAF method provides
35 * small memory and fast elliptic scalar multiplications secure against
36 * side channel attacks. In : Topics in Cryptology—CT-RSA 2003. Springer
37 * Berlin Heidelberg, 2003. p. 328-343.
38 * <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>.
39 *
40 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
41 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
42 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
43 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010044 */
45
46#include "polarssl/config.h"
47
48#if defined(POLARSSL_ECP_C)
49
50#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051
52#if defined(POLARSSL_MEMORY_C)
53#include "polarssl/memory.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +010059#include <limits.h>
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010060#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010062#if defined(POLARSSL_SELF_TEST)
63/*
64 * Counts of point addition and doubling operations.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020065 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010066 */
67unsigned long add_count, dbl_count;
68#endif
69
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010070/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020071 * List of supported curves:
72 * - internal ID
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020073 * - TLS NamedCurve ID (RFC 4492 section 5.1.1)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020074 * - size in bits
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020075 * - readeble name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020076 */
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +020077const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020078{
79#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020080 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020081#endif
82#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020083 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020084#endif
85#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020086 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020087#endif
88#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020089 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020090#endif
91#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020092 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020093#endif
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020094 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020095};
96
97/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010098 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010099 */
100void ecp_point_init( ecp_point *pt )
101{
102 if( pt == NULL )
103 return;
104
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100105 mpi_init( &pt->X );
106 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100107 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100108}
109
110/*
111 * Initialize (the components of) a group
112 */
113void ecp_group_init( ecp_group *grp )
114{
115 if( grp == NULL )
116 return;
117
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200118 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100119}
120
121/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200122 * Initialize (the components of) a key pair
123 */
124void ecp_keypair_init( ecp_keypair *key )
125{
126 if ( key == NULL )
127 return;
128
129 ecp_group_init( &key->grp );
130 mpi_init( &key->d );
131 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200132}
133
134/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100135 * Unallocate (the components of) a point
136 */
137void ecp_point_free( ecp_point *pt )
138{
139 if( pt == NULL )
140 return;
141
142 mpi_free( &( pt->X ) );
143 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100144 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100145}
146
147/*
148 * Unallocate (the components of) a group
149 */
150void ecp_group_free( ecp_group *grp )
151{
152 if( grp == NULL )
153 return;
154
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100155 mpi_free( &grp->P );
156 mpi_free( &grp->B );
157 ecp_point_free( &grp->G );
158 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200159
160 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100161}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100162
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100163/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200164 * Unallocate (the components of) a key pair
165 */
166void ecp_keypair_free( ecp_keypair *key )
167{
168 if ( key == NULL )
169 return;
170
171 ecp_group_free( &key->grp );
172 mpi_free( &key->d );
173 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200174}
175
176/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100177 * Set point to zero
178 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100179int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100180{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100181 int ret;
182
183 MPI_CHK( mpi_lset( &pt->X , 1 ) );
184 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
185 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
186
187cleanup:
188 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100189}
190
191/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100192 * Tell if a point is zero
193 */
194int ecp_is_zero( ecp_point *pt )
195{
196 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
197}
198
199/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100200 * Copy the contents of Q into P
201 */
202int ecp_copy( ecp_point *P, const ecp_point *Q )
203{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100204 int ret;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100205
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100206 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
207 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100208 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100209
210cleanup:
211 return( ret );
212}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100213
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100214/*
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200215 * Copy the contents of a group object
216 */
217int ecp_group_copy( ecp_group *dst, const ecp_group *src )
218{
219 return ecp_use_known_dp( dst, src->id );
220}
221
222/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100223 * Import a non-zero point from ASCII strings
224 */
225int ecp_point_read_string( ecp_point *P, int radix,
226 const char *x, const char *y )
227{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100228 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100229
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100230 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
231 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100232 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100233
234cleanup:
235 return( ret );
236}
237
238/*
239 * Import an ECP group from ASCII strings
240 */
241int ecp_group_read_string( ecp_group *grp, int radix,
242 const char *p, const char *b,
243 const char *gx, const char *gy, const char *n)
244{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100245 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100246
247 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
248 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
249 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
250 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
251
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100252 grp->pbits = mpi_msb( &grp->P );
253 grp->nbits = mpi_msb( &grp->N );
254
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100255cleanup:
256 return( ret );
257}
258
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100259/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100260 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100261 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100262int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100263 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100264 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100265{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200266 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100267 size_t plen;
268
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100269 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
270 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100271 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100272
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100273 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100274 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100275 */
276 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
277 {
278 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100279 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100280
281 buf[0] = 0x00;
282 *olen = 1;
283
284 return( 0 );
285 }
286
287 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100288
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100289 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
290 {
291 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100292
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100293 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100294 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100295
296 buf[0] = 0x04;
297 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
298 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
299 }
300 else if( format == POLARSSL_ECP_PF_COMPRESSED )
301 {
302 *olen = plen + 1;
303
304 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100305 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100306
307 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
308 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
309 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100310
311cleanup:
312 return( ret );
313}
314
315/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100316 * Import a point from unsigned binary data (SEC1 2.3.4)
317 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100318int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
319 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100320 int ret;
321 size_t plen;
322
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100323 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100324 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100325
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100326 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100327
328 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100329 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100330
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100331 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
332 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
333 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100334
335cleanup:
336 return( ret );
337}
338
339/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100340 * Import a point from a TLS ECPoint record (RFC 4492)
341 * struct {
342 * opaque point <1..2^8-1>;
343 * } ECPoint;
344 */
345int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100346 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100347{
348 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100349 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100350
351 /*
352 * We must have at least two bytes (1 for length, at least of for data)
353 */
354 if( buf_len < 2 )
355 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
356
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100357 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100358 if( data_len < 1 || data_len > buf_len - 1 )
359 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
360
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100361 /*
362 * Save buffer start for read_binary and update buf
363 */
364 buf_start = *buf;
365 *buf += data_len;
366
367 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100368}
369
370/*
371 * Export a point as a TLS ECPoint record (RFC 4492)
372 * struct {
373 * opaque point <1..2^8-1>;
374 * } ECPoint;
375 */
376int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100377 int format, size_t *olen,
378 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100379{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100380 int ret;
381
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100382 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100383 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100384 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100385 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100386 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
387
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100388 if( ( ret = ecp_point_write_binary( grp, pt, format,
389 olen, buf + 1, blen - 1) ) != 0 )
390 return( ret );
391
392 /*
393 * write length to the first byte and update total length
394 */
395 buf[0] = *olen;
396 ++*olen;
397
398 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100399}
400
401/*
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100402 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
403 * See the documentation of struct ecp_group.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100404 */
405static int ecp_modp( mpi *N, const ecp_group *grp )
406{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100407 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100408
409 if( grp->modp == NULL )
410 return( mpi_mod_mpi( N, N, &grp->P ) );
411
412 if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200413 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100414
415 MPI_CHK( grp->modp( N ) );
416
417 while( mpi_cmp_int( N, 0 ) < 0 )
418 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
419
420 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
421 MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) );
422
423cleanup:
424 return( ret );
425}
426
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200427#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100428/*
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100429 * 192 bits in terms of t_uint
430 */
431#define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) )
432
433/*
434 * Table to get S1, S2, S3 of FIPS 186-3 D.2.1:
435 * -1 means let this chunk be 0
436 * a positive value i means A_i.
437 */
438#define P192_CHUNKS 3
439#define P192_CHUNK_CHAR ( 64 / CHAR_BIT )
440#define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) )
441
442const signed char p192_tbl[][P192_CHUNKS] = {
443 { -1, 3, 3 }, /* S1 */
444 { 4, 4, -1 }, /* S2 */
445 { 5, 5, 5 }, /* S3 */
446};
447
448/*
449 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
450 */
451static int ecp_mod_p192( mpi *N )
452{
453 int ret;
454 unsigned char i, j, offset;
455 signed char chunk;
456 mpi tmp, acc;
457 t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1];
458
459 tmp.s = 1;
460 tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] );
461 tmp.p = tmp_p;
462
463 acc.s = 1;
464 acc.n = sizeof( acc_p ) / sizeof( acc_p[0] );
465 acc.p = acc_p;
466
467 MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) );
468
469 /*
470 * acc = T
471 */
472 memset( acc_p, 0, sizeof( acc_p ) );
473 memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS );
474
475 for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++)
476 {
477 /*
478 * tmp = S_i
479 */
480 memset( tmp_p, 0, sizeof( tmp_p ) );
481 for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- )
482 {
483 chunk = p192_tbl[i][j];
484 if( chunk >= 0 )
485 memcpy( tmp_p + offset * P192_CHUNK_INT,
486 N->p + chunk * P192_CHUNK_INT,
487 P192_CHUNK_CHAR );
488 }
489
490 /*
491 * acc += tmp
492 */
493 MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) );
494 }
495
496 MPI_CHK( mpi_copy( N, &acc ) );
497
498cleanup:
499 return( ret );
500}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200501#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100502
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200503#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100504/*
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100505 * Size of p521 in terms of t_uint
506 */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100507#define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 )
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100508
509/*
510 * Bits to keep in the most significant t_uint
511 */
512#if defined(POLARSS_HAVE_INT8)
513#define P521_MASK 0x01
514#else
515#define P521_MASK 0x01FF
516#endif
517
518/*
519 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100520 */
521static int ecp_mod_p521( mpi *N )
522{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100523 int ret;
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100524 t_uint Mp[P521_SIZE_INT];
525 mpi M;
526
527 if( N->n < P521_SIZE_INT )
528 return( 0 );
529
530 memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) );
531 memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) );
532 Mp[P521_SIZE_INT - 1] &= P521_MASK;
533
534 M.s = 1;
535 M.n = P521_SIZE_INT;
536 M.p = Mp;
537
538 MPI_CHK( mpi_shift_r( N, 521 ) );
539
540 MPI_CHK( mpi_add_abs( N, N, &M ) );
541
542cleanup:
543 return( ret );
544}
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200545#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100546
547/*
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100548 * Domain parameters for secp192r1
549 */
550#define SECP192R1_P \
551 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
552#define SECP192R1_B \
553 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
554#define SECP192R1_GX \
555 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
556#define SECP192R1_GY \
557 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
558#define SECP192R1_N \
559 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
560
561/*
562 * Domain parameters for secp224r1
563 */
564#define SECP224R1_P \
565 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
566#define SECP224R1_B \
567 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
568#define SECP224R1_GX \
569 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
570#define SECP224R1_GY \
571 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
572#define SECP224R1_N \
573 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
574
575/*
576 * Domain parameters for secp256r1
577 */
578#define SECP256R1_P \
579 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
580#define SECP256R1_B \
581 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
582#define SECP256R1_GX \
583 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
584#define SECP256R1_GY \
585 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
586#define SECP256R1_N \
587 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
588
589/*
590 * Domain parameters for secp384r1
591 */
592#define SECP384R1_P \
593 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
594 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
595#define SECP384R1_B \
596 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
597 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
598#define SECP384R1_GX \
599 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
600 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
601#define SECP384R1_GY \
602 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
603 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
604#define SECP384R1_N \
605 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
606 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
607
608/*
609 * Domain parameters for secp521r1
610 */
611#define SECP521R1_P \
612 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
613 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
614 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
615#define SECP521R1_B \
616 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
617 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
618 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
619#define SECP521R1_GX \
620 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
621 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
622 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
623#define SECP521R1_GY \
624 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
625 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
626 "3FAD0761353C7086A272C24088BE94769FD16650"
627#define SECP521R1_N \
628 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
629 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
630 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
631
632/*
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100633 * Set a group using well-known domain parameters
634 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100635int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100636{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100637 grp->id = id;
638
639 switch( id )
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100640 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200641#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100642 case POLARSSL_ECP_DP_SECP192R1:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100643 grp->modp = ecp_mod_p192;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100644 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100645 SECP192R1_P, SECP192R1_B,
646 SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200647#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100648
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200649#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100650 case POLARSSL_ECP_DP_SECP224R1:
651 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100652 SECP224R1_P, SECP224R1_B,
653 SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200654#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100655
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200656#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100657 case POLARSSL_ECP_DP_SECP256R1:
658 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100659 SECP256R1_P, SECP256R1_B,
660 SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200661#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100662
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200663#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100664 case POLARSSL_ECP_DP_SECP384R1:
665 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100666 SECP384R1_P, SECP384R1_B,
667 SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200668#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100669
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200670#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100671 case POLARSSL_ECP_DP_SECP521R1:
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100672 grp->modp = ecp_mod_p521;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100673 return( ecp_group_read_string( grp, 16,
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100674 SECP521R1_P, SECP521R1_B,
675 SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200676#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100677
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200678 default:
679 grp->id = POLARSSL_ECP_DP_NONE;
680 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
681 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100682}
683
684/*
685 * Set a group from an ECParameters record (RFC 4492)
686 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100687int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100688{
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200689 unsigned int named_curve;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100690
691 /*
692 * We expect at least three bytes (see below)
693 */
694 if( len < 3 )
695 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
696
697 /*
698 * First byte is curve_type; only named_curve is handled
699 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100700 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100701 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
702
703 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100704 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100705 */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200706 named_curve = *(*buf)++;
707 named_curve <<= 8;
708 named_curve |= *(*buf)++;
709 return ecp_use_known_dp( grp, ecp_grp_id_from_named_curve( named_curve ) );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100710}
711
712/*
713 * Write the ECParameters record corresponding to a group (RFC 4492)
714 */
715int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
716 unsigned char *buf, size_t blen )
717{
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200718 unsigned int named_curve;
719
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100720 /*
721 * We are going to write 3 bytes (see below)
722 */
723 *olen = 3;
724 if( blen < *olen )
725 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
726
727 /*
728 * First byte is curve_type, always named_curve
729 */
730 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
731
732 /*
733 * Next two bytes are the namedcurve value
734 */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200735 named_curve = ecp_named_curve_from_grp_id( grp->id );
736 buf[0] = named_curve >> 8;
737 buf[1] = named_curve & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100738
739 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100740}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100741
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200742/*
743 * Get the internal identifer from the TLS name
744 */
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200745ecp_group_id ecp_grp_id_from_named_curve( uint16_t tls_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200746{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200747 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200748
749 for( curve_info = ecp_supported_curves;
750 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
751 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200752 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200753 if( curve_info->tls_id == tls_id )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200754 return( curve_info->grp_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200755 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200756
757 return( POLARSSL_ECP_DP_NONE );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200758}
759
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200760/*
761 * Get the TLS name for the internal identifer
762 */
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200763uint16_t ecp_named_curve_from_grp_id( ecp_group_id grp_id )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200764{
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200765 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200766
767 for( curve_info = ecp_supported_curves;
768 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
769 curve_info++ )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200770 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200771 if( curve_info->grp_id == grp_id )
772 return( curve_info->tls_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200773 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200774
775 return( 0 );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200776}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200777
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100778/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100779 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100780 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100781 * In order to guarantee that, we need to ensure that operands of
782 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100783 * bring the result back to this range.
784 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100785 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100786 */
787
788/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100789 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
790 */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100791#define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100792
793/*
794 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
795 */
796#define MOD_SUB( N ) \
797 while( mpi_cmp_int( &N, 0 ) < 0 ) \
798 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
799
800/*
801 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int
802 */
803#define MOD_ADD( N ) \
804 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
805 MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) )
806
807/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100808 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100809 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100810static int ecp_normalize( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100811{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100812 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100813 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100814
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100815 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100816 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100817
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100818 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100819
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100820 /*
821 * X = X / Z^2 mod p
822 */
823 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
824 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
825 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100826
827 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100828 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100829 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100830 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
831 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100832
833 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100834 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100835 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100836 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100837
838cleanup:
839
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100840 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100841
842 return( ret );
843}
844
845/*
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100846 * Normalize jacobian coordinates of an array of points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100847 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100848 * (See for example Cohen's "A Course in Computational Algebraic Number
849 * Theory", Algorithm 10.3.4.)
850 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200851 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100852 * This should never happen, see choice of w in ecp_mul().
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100853 */
854static int ecp_normalize_many( const ecp_group *grp,
855 ecp_point T[], size_t t_len )
856{
857 int ret;
858 size_t i;
859 mpi *c, u, Zi, ZZi;
860
861 if( t_len < 2 )
862 return( ecp_normalize( grp, T ) );
863
Paul Bakker6e339b52013-07-03 13:37:05 +0200864 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200865 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100866
867 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
868 for( i = 0; i < t_len; i++ )
869 mpi_init( &c[i] );
870
871 /*
872 * c[i] = Z_0 * ... * Z_i
873 */
874 MPI_CHK( mpi_copy( &c[0], &T[0].Z ) );
875 for( i = 1; i < t_len; i++ )
876 {
877 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) );
878 MOD_MUL( c[i] );
879 }
880
881 /*
882 * u = 1 / (Z_0 * ... * Z_n) mod P
883 */
884 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
885
886 for( i = t_len - 1; ; i-- )
887 {
888 /*
889 * Zi = 1 / Z_i mod p
890 * u = 1 / (Z_0 * ... * Z_i) mod P
891 */
892 if( i == 0 ) {
893 MPI_CHK( mpi_copy( &Zi, &u ) );
894 }
895 else
896 {
897 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
898 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u );
899 }
900
901 /*
902 * proceed as in normalize()
903 */
904 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
905 MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X );
906 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y );
907 MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y );
908 MPI_CHK( mpi_lset( &T[i].Z, 1 ) );
909
910 if( i == 0 )
911 break;
912 }
913
914cleanup:
915
916 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
917 for( i = 0; i < t_len; i++ )
918 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200919 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100920
921 return( ret );
922}
923
924
925/*
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100926 * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21)
927 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100928static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
929 const ecp_point *P )
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100930{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100931 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100932 mpi T1, T2, T3, X, Y, Z;
933
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100934#if defined(POLARSSL_SELF_TEST)
935 dbl_count++;
936#endif
937
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100938 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100939 return( ecp_set_zero( R ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100940
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100941 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
942 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
943
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100944 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
945 MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 );
946 MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 );
947 MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 );
948 MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 );
949 MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y );
950 MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z );
951 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
952 MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 );
953 MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100954
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100955 /*
956 * For Y = Y / 2 mod p, we must make sure that Y is even before
957 * using right-shift. No need to reduce mod p afterwards.
958 */
959 if( mpi_get_bit( &Y, 0 ) == 1 )
960 MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) );
961 MPI_CHK( mpi_shift_r( &Y, 1 ) );
962
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100963 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
964 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
965 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
966 MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 );
967 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 );
968 MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100969
970 MPI_CHK( mpi_copy( &R->X, &X ) );
971 MPI_CHK( mpi_copy( &R->Y, &Y ) );
972 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100973
974cleanup:
975
976 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
977 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
978
979 return( ret );
980}
981
982/*
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100983 * Addition or subtraction: R = P + Q or R = P + Q,
984 * mixed affine-Jacobian coordinates (GECC 3.22)
985 *
986 * The coordinates of Q must be normalized (= affine),
987 * but those of P don't need to. R is not normalized.
988 *
989 * If sign >= 0, perform addition, otherwise perform subtraction,
990 * taking advantage of the fact that, for Q != 0, we have
991 * -Q = (Q.X, -Q.Y, Q.Z)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100992 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100993static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100994 const ecp_point *P, const ecp_point *Q,
995 signed char sign )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100996{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100997 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100998 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100999
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001000#if defined(POLARSSL_SELF_TEST)
1001 add_count++;
1002#endif
1003
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001004 /*
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001005 * Trivial cases: P == 0 or Q == 0
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001006 * (Check Q first, so that we know Q != 0 when we compute -Q.)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001007 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001008 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
1009 return( ecp_copy( R, P ) );
1010
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001011 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1012 {
1013 ret = ecp_copy( R, Q );
1014
1015 /*
1016 * -R.Y mod P = P - R.Y unless R.Y == 0
1017 */
1018 if( ret == 0 && sign < 0)
1019 if( mpi_cmp_int( &R->Y, 0 ) != 0 )
1020 ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y );
1021
1022 return( ret );
1023 }
1024
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001025 /*
1026 * Make sure Q coordinates are normalized
1027 */
1028 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001029 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001030
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001031 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1032 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001033
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001034 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1035 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1036 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1037 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001038
1039 /*
1040 * For subtraction, -Q.Y should have been used instead of Q.Y,
1041 * so we replace T2 by -T2, which is P - T2 mod P
1042 */
1043 if( sign < 0 )
1044 {
1045 MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) );
1046 MOD_SUB( T2 );
1047 }
1048
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001049 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1050 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001051
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001052 if( mpi_cmp_int( &T1, 0 ) == 0 )
1053 {
1054 if( mpi_cmp_int( &T2, 0 ) == 0 )
1055 {
1056 ret = ecp_double_jac( grp, R, P );
1057 goto cleanup;
1058 }
1059 else
1060 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001061 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001062 goto cleanup;
1063 }
1064 }
1065
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001066 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1067 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1068 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1069 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1070 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1071 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1072 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1073 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1074 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1075 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1076 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1077 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001078
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001079 MPI_CHK( mpi_copy( &R->X, &X ) );
1080 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1081 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001082
1083cleanup:
1084
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001085 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1086 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001087
1088 return( ret );
1089}
1090
1091/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001092 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001093 */
1094int ecp_add( const ecp_group *grp, ecp_point *R,
1095 const ecp_point *P, const ecp_point *Q )
1096{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001097 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001098
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001099 MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) );
1100 MPI_CHK( ecp_normalize( grp, R ) );
1101
1102cleanup:
1103 return( ret );
1104}
1105
1106/*
1107 * Subtraction: R = P - Q, result's coordinates normalized
1108 */
1109int ecp_sub( const ecp_group *grp, ecp_point *R,
1110 const ecp_point *P, const ecp_point *Q )
1111{
1112 int ret;
1113
1114 MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001115 MPI_CHK( ecp_normalize( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001116
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001117cleanup:
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001118 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001119}
1120
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001121/*
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001122 * Compute a modified width-w non-adjacent form (NAF) of a number,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001123 * with a fixed pattern for resistance to simple timing attacks (even SPA),
1124 * see [1]. (The resulting multiplication algorithm can also been seen as a
1125 * modification of 2^w-ary multiplication, with signed coefficients, all of
1126 * them odd.)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001127 *
1128 * Input:
1129 * m must be an odd positive mpi less than w * k bits long
1130 * x must be an array of k elements
1131 * w must be less than a certain maximum (currently 8)
1132 *
1133 * The result is a sequence x[0], ..., x[k-1] with x[i] in the range
1134 * - 2^(width - 1) .. 2^(width - 1) - 1 such that
1135 * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ...
1136 * + 2^((k-1) * width) * (2 * x[k-1] + 1)
1137 *
1138 * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar"
1139 * p. 335 of the cited reference, here we return only u, not d_w since
1140 * it is known that the other d_w[j] will be 0. Moreover, the returned
1141 * string doesn't actually store u_i but x_i = u_i / 2 since it is known
1142 * that u_i is odd. Also, since we always select a positive value for d
1143 * mod 2^w, we don't need to check the sign of u[i-1] when the reference
1144 * does. Finally, there is an off-by-one error in the reference: the
1145 * last index should be k-1, not k.
1146 */
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001147static int ecp_w_naf_fixed( signed char x[], size_t k,
1148 unsigned char w, const mpi *m )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001149{
1150 int ret;
1151 unsigned int i, u, mask, carry;
1152 mpi M;
1153
1154 mpi_init( &M );
1155
1156 MPI_CHK( mpi_copy( &M, m ) );
1157 mask = ( 1 << w ) - 1;
1158 carry = 1 << ( w - 1 );
1159
1160 for( i = 0; i < k; i++ )
1161 {
1162 u = M.p[0] & mask;
1163
1164 if( ( u & 1 ) == 0 && i > 0 )
1165 x[i - 1] -= carry;
1166
1167 x[i] = u >> 1;
1168 mpi_shift_r( &M, w );
1169 }
1170
1171 /*
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001172 * We should have consumed all bits, unless the input value was too big
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001173 */
1174 if( mpi_cmp_int( &M, 0 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001175 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +01001176
1177cleanup:
1178
1179 mpi_free( &M );
1180
1181 return( ret );
1182}
1183
1184/*
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001185 * Precompute odd multiples of P up to (2 * t_len - 1) P.
1186 * The table is filled with T[i] = (2 * i + 1) P.
1187 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001188static int ecp_precompute( const ecp_group *grp,
1189 ecp_point T[], size_t t_len,
1190 const ecp_point *P )
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001191{
1192 int ret;
1193 size_t i;
1194 ecp_point PP;
1195
1196 ecp_point_init( &PP );
1197
1198 MPI_CHK( ecp_add( grp, &PP, P, P ) );
1199
1200 MPI_CHK( ecp_copy( &T[0], P ) );
1201
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001202 for( i = 1; i < t_len; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001203 MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) );
1204
1205 /*
1206 * T[0] = P already has normalized coordinates
1207 */
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001208 MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) );
Manuel Pégourié-Gonnard7652a592012-11-21 10:00:45 +01001209
1210cleanup:
1211
1212 ecp_point_free( &PP );
1213
1214 return( ret );
1215}
1216
1217/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001218 * Randomize jacobian coordinates:
1219 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1220 * This is sort of the reverse operation of ecp_normalize().
1221 */
1222static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt,
1223 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1224{
1225 int ret;
1226 mpi l, ll;
1227 size_t p_size = (grp->pbits + 7) / 8;
1228 int count = 0;
1229
1230 mpi_init( &l ); mpi_init( &ll );
1231
1232 /* Generate l such that 1 < l < p */
1233 do
1234 {
1235 mpi_fill_random( &l, p_size, f_rng, p_rng );
1236
1237 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1238 mpi_shift_r( &l, 1 );
1239
1240 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001241 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001242 }
1243 while( mpi_cmp_int( &l, 1 ) <= 0 );
1244
1245 /* Z = l * Z */
1246 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1247
1248 /* X = l^2 * X */
1249 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1250 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1251
1252 /* Y = l^3 * Y */
1253 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1254 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1255
1256cleanup:
1257 mpi_free( &l ); mpi_free( &ll );
1258
1259 return( ret );
1260}
1261
1262/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001263 * Maximum length of the precomputed table
1264 */
1265#define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) )
1266
1267/*
1268 * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w
1269 * (that is: grp->nbits / w + 1)
1270 * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
1271 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +02001272#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001273
1274/*
1275 * Integer multiplication: R = m * P
1276 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001277 * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed().
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001278 *
1279 * This function executes a fixed number of operations for
1280 * random m in the range 0 .. 2^nbits - 1.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001281 *
1282 * As an additional countermeasure against potential elaborate timing attacks,
1283 * we randomize coordinates after each addition. This was suggested as a
1284 * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that
1285 * we use jacobian coordinates, not standard projective coordinates).
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001286 */
1287int ecp_mul( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001288 const mpi *m, const ecp_point *P,
1289 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001290{
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001291 int ret;
1292 unsigned char w, m_is_odd;
1293 size_t pre_len, naf_len, i, j;
1294 signed char naf[ MAX_NAF_LEN ];
1295 ecp_point Q, T[ MAX_PRE_LEN ];
1296 mpi M;
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001297
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001298 if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001299 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +01001300
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001301 w = grp->nbits >= 521 ? 6 :
1302 grp->nbits >= 224 ? 5 :
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001303 4;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001304
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001305 /*
1306 * Make sure w is within the limits.
1307 * The last test ensures that none of the precomputed points is zero,
1308 * which wouldn't be handled correctly by ecp_normalize_many().
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001309 * It is only useful for very small curves, as used in the test suite.
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001310 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001311 if( w > POLARSSL_ECP_WINDOW_SIZE )
1312 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001313 if( w < 2 || w >= grp->nbits )
1314 w = 2;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001315
1316 pre_len = 1 << ( w - 1 );
1317 naf_len = grp->nbits / w + 1;
1318
1319 mpi_init( &M );
1320 ecp_point_init( &Q );
1321 for( i = 0; i < pre_len; i++ )
1322 ecp_point_init( &T[i] );
1323
1324 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
1325
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001326 /*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001327 * Make sure M is odd:
1328 * later we'll get m * P by subtracting * P or 2 * P to M * P.
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001329 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001330 MPI_CHK( mpi_copy( &M, m ) );
1331 MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001332
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001333 /*
1334 * Compute the fixed-pattern NAF and precompute odd multiples
1335 */
1336 MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) );
1337 MPI_CHK( ecp_precompute( grp, T, pre_len, P ) );
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001338
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001339 /*
1340 * Compute M * P, using a variant of left-to-right 2^w-ary multiplication:
1341 * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w.
1342 *
1343 * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ]
1344 * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P
1345 * == T[ - naf[i] - 1 ]
1346 */
1347 MPI_CHK( ecp_set_zero( &Q ) );
1348 i = naf_len - 1;
1349 while( 1 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001350 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001351 if( naf[i] < 0 )
1352 {
1353 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) );
1354 }
1355 else
1356 {
1357 MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) );
1358 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001359
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001360 /* Countermeasure (see comments above) */
1361 if( f_rng != NULL )
1362 ecp_randomize_coordinates( grp, &Q, f_rng, p_rng );
1363
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001364 if( i == 0 )
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001365 break;
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001366 i--;
1367
1368 for( j = 0; j < w; j++ )
1369 {
1370 MPI_CHK( ecp_double_jac( grp, &Q, &Q ) );
1371 }
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001372 }
1373
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001374 /*
1375 * Now get m * P from M * P.
1376 * Since we don't need T[] any more, we can recycle it:
1377 * we already have T[0] = P, now set T[1] = 2 * P.
1378 */
1379 MPI_CHK( ecp_add( grp, &T[1], P, P ) );
1380 MPI_CHK( ecp_sub( grp, R, &Q, &T[m_is_odd] ) );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001381
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001382
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001383cleanup:
1384
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001385 mpi_free( &M );
1386 ecp_point_free( &Q );
1387 for( i = 0; i < pre_len; i++ )
1388 ecp_point_free( &T[i] );
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001389
1390 return( ret );
1391}
1392
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001393/*
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001394 * Check that a point is valid as a public key (SEC1 3.2.3.1)
1395 */
1396int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1397{
1398 int ret;
1399 mpi YY, RHS;
1400
1401 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001402 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001403
1404 /*
1405 * pt coordinates must be normalized for our checks
1406 */
1407 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001408 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001409
1410 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1411 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1412 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1413 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001414 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001415
1416 mpi_init( &YY ); mpi_init( &RHS );
1417
1418 /*
1419 * YY = Y^2
1420 * RHS = X (X^2 - 3) + B = X^3 - 3X + B
1421 */
1422 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1423 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
1424 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1425 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1426 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
1427
1428 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001429 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001430
1431cleanup:
1432
1433 mpi_free( &YY ); mpi_free( &RHS );
1434
1435 return( ret );
1436}
1437
1438/*
1439 * Check that an mpi is valid as a private key (SEC1 3.2)
1440 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001441int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001442{
1443 /* We want 1 <= d <= N-1 */
1444 if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001445 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001446
1447 return( 0 );
1448}
1449
1450/*
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001451 * Generate a keypair (SEC1 3.2.1)
1452 */
1453int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
1454 int (*f_rng)(void *, unsigned char *, size_t),
1455 void *p_rng )
1456{
1457 int count = 0;
1458 size_t n_size = (grp->nbits + 7) / 8;
1459
1460 /*
1461 * Generate d such that 1 <= n < N
1462 */
1463 do
1464 {
1465 mpi_fill_random( d, n_size, f_rng, p_rng );
1466
1467 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1468 mpi_shift_r( d, 1 );
1469
1470 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001471 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001472 }
1473 while( mpi_cmp_int( d, 1 ) < 0 );
1474
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001475 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001476}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001477
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001478#if defined(POLARSSL_SELF_TEST)
1479
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001480/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001481 * Checkup routine
1482 */
1483int ecp_self_test( int verbose )
1484{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001485 int ret;
1486 size_t i;
1487 ecp_group grp;
1488 ecp_point R;
1489 mpi m;
1490 unsigned long add_c_prev, dbl_c_prev;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001491 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001492 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001493 "000000000000000000000000000000000000000000000000", /* zero */
1494 "000000000000000000000000000000000000000000000001", /* one */
1495 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */
1496 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001497 "400000000000000000000000000000000000000000000000",
1498 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
1499 "555555555555555555555555555555555555555555555555",
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001500 };
1501
1502 ecp_group_init( &grp );
1503 ecp_point_init( &R );
1504 mpi_init( &m );
1505
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001506#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001507 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001508#else
1509#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1510 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) );
1511#else
1512#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1513 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) );
1514#else
1515#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
1516 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) );
1517#else
1518#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
1519 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) );
1520#else
1521#error No curves defines
1522#endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */
1523#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
1524#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
1525#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
1526#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001527
1528 if( verbose != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001529 printf( " ECP test #1 (resistance to simple timing attacks): " );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001530
1531 add_count = 0;
1532 dbl_count = 0;
1533 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001534 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001535
1536 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1537 {
1538 add_c_prev = add_count;
1539 dbl_c_prev = dbl_count;
1540 add_count = 0;
1541 dbl_count = 0;
1542
1543 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001544 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001545
1546 if( add_count != add_c_prev || dbl_count != dbl_c_prev )
1547 {
1548 if( verbose != 0 )
1549 printf( "failed (%zu)\n", i );
1550
1551 ret = 1;
1552 goto cleanup;
1553 }
1554 }
1555
1556 if( verbose != 0 )
1557 printf( "passed\n" );
1558
1559cleanup:
1560
1561 if( ret < 0 && verbose != 0 )
1562 printf( "Unexpected error, return code = %08X\n", ret );
1563
1564 ecp_group_free( &grp );
1565 ecp_point_free( &R );
1566 mpi_free( &m );
1567
1568 if( verbose != 0 )
1569 printf( "\n" );
1570
1571 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001572}
1573
1574#endif
1575
1576#endif