blob: d9a3ac2f7e836cbc5760583ef139947ab6251ba3 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010020 */
21
22/*
23 * References:
24 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010025 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010026 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010027 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010028 * RFC 4492 for the related TLS structures and constants
Nicholas Wilson08f3ef12015-11-10 13:10:01 +000029 * RFC 7748 for the Curve448 and Curve25519 curve definitions
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020030 *
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020031 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010032 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020033 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020034 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
35 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
36 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010037 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020038 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010039 * render ECC resistant against Side Channel Attacks. IACR Cryptology
40 * ePrint Archive, 2004, vol. 2004, p. 342.
41 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010042 */
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020046#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020048#endif
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010049
Janos Follath683c5822018-12-07 12:09:25 +000050/**
51 * \brief Function level alternative implementation.
52 *
53 * The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
54 * replace certain functions in this module. The alternative implementations are
55 * typically hardware accelerators and need to activate the hardware before the
56 * computation starts and deactivate it after it finishes. The
57 * mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
58 * this purpose.
59 *
60 * To preserve the correct functionality the following conditions must hold:
61 *
62 * - The alternative implementation must be activated by
63 * mbedtls_internal_ecp_init() before any of the replaceable functions is
64 * called.
65 * - mbedtls_internal_ecp_free() must \b only be called when the alternative
66 * implementation is activated.
67 * - mbedtls_internal_ecp_init() must \b not be called when the alternative
68 * implementation is activated.
69 * - Public functions must not return while the alternative implementation is
70 * activated.
71 * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
72 * before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
73 * \endcode ensures that the alternative implementation supports the current
74 * group.
75 */
76#if defined(MBEDTLS_ECP_INTERNAL_ALT)
77#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010080
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/ecp.h"
Janos Follath430d3372016-11-03 14:25:37 +000082#include "mbedtls/threading.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050083#include "mbedtls/platform_util.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020084
Rich Evans00ab4702015-02-06 13:43:58 +000085#include <string.h>
86
Janos Follathb0697532016-08-18 12:38:46 +010087#if !defined(MBEDTLS_ECP_ALT)
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000090#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020091#else
Rich Evans00ab4702015-02-06 13:43:58 +000092#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000093#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020095#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020097#endif
98
Janos Follath47d28f02016-11-01 13:22:05 +000099#include "mbedtls/ecp_internal.h"
Janos Follathb0697532016-08-18 12:38:46 +0100100
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +0100101#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
102 !defined(inline) && !defined(__cplusplus)
Paul Bakker6a6087e2013-10-28 18:53:08 +0100103#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +0200104#endif
Paul Bakker6a6087e2013-10-28 18:53:08 +0100105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100107/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100108 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200109 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100110 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100111static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100112#endif
113
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200114#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100115/*
116 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200117 *
118 * Default value 0 means that ECC operations will not yield.
119 * Note that regardless of the value of ecp_max_ops, always at
120 * least one step is performed before yielding.
121 *
122 * Setting ecp_max_ops=1 can be suitable for testing purposes
123 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100124 */
125static unsigned ecp_max_ops = 0;
126
127/*
128 * Set ecp_max_ops
129 */
130void mbedtls_ecp_set_max_ops( unsigned max_ops )
131{
132 ecp_max_ops = max_ops;
133}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100134
135/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200136 * Check if restart is enabled
137 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200138int mbedtls_ecp_restart_is_enabled( void )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200139{
140 return( ecp_max_ops != 0 );
141}
142
143/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200144 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100145 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200146struct mbedtls_ecp_restart_mul
147{
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100148 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100149 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100150 mbedtls_ecp_point *T; /* table for precomputed points */
151 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200152 enum { /* what were we doing last time we returned? */
153 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
154 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100155 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
156 ecp_rsm_pre_add, /* precompute remaining points by adding */
157 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200158 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100159 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100160 } state;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100161};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100162
163/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200164 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100165 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200166static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100167{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200168 mbedtls_ecp_point_init( &ctx->R );
169 ctx->i = 0;
170 ctx->T = NULL;
171 ctx->T_size = 0;
172 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100173}
174
175/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200176 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100177 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200178static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100179{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100180 unsigned char i;
181
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100182 if( ctx == NULL )
183 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100184
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100185 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100186
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200187 if( ctx->T != NULL )
188 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100189 for( i = 0; i < ctx->T_size; i++ )
190 mbedtls_ecp_point_free( ctx->T + i );
191 mbedtls_free( ctx->T );
192 }
193
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200194 ecp_restart_rsm_init( ctx );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100195}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100196
197/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200198 * Restart context for ecp_muladd()
199 */
200struct mbedtls_ecp_restart_muladd
201{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200202 mbedtls_ecp_point mP; /* mP value */
203 mbedtls_ecp_point R; /* R intermediate result */
204 enum { /* what should we do next? */
205 ecp_rsma_mul1 = 0, /* first multiplication */
206 ecp_rsma_mul2, /* second multiplication */
207 ecp_rsma_add, /* addition */
208 ecp_rsma_norm, /* normalization */
209 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200210};
211
212/*
213 * Init restart_muladd sub-context
214 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200215static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200216{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200217 mbedtls_ecp_point_init( &ctx->mP );
218 mbedtls_ecp_point_init( &ctx->R );
219 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200220}
221
222/*
223 * Free the components of a restart_muladd sub-context
224 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200225static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200226{
227 if( ctx == NULL )
228 return;
229
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200230 mbedtls_ecp_point_free( &ctx->mP );
231 mbedtls_ecp_point_free( &ctx->R );
232
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200233 ecp_restart_ma_init( ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200234}
235
236/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200237 * Initialize a restart context
238 */
239void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
240{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200241 ctx->ops_done = 0;
242 ctx->depth = 0;
243 ctx->rsm = NULL;
244 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200245}
246
247/*
248 * Free the components of a restart context
249 */
250void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
251{
252 if( ctx == NULL )
253 return;
254
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200255 ecp_restart_rsm_free( ctx->rsm );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200256 mbedtls_free( ctx->rsm );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200257
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200258 ecp_restart_ma_free( ctx->ma );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200259 mbedtls_free( ctx->ma );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200260
261 mbedtls_ecp_restart_init( ctx );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200262}
263
264/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100265 * Check if we can do the next step
266 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200267int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
268 mbedtls_ecp_restart_ctx *rs_ctx,
269 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100270{
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200271 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100272 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100273 /* scale depending on curve size: the chosen reference is 256-bit,
274 * and multiplication is quadratic. Round to the closest integer. */
275 if( grp->pbits >= 512 )
276 ops *= 4;
277 else if( grp->pbits >= 384 )
278 ops *= 2;
279
Hanno Beckerb10c6602018-10-26 13:50:13 +0100280 /* Avoid infinite loops: always allow first step.
281 * Because of that, however, it's not generally true
282 * that ops_done <= ecp_max_ops, so the check
283 * ops_done > ecp_max_ops below is mandatory. */
284 if( ( rs_ctx->ops_done != 0 ) &&
285 ( rs_ctx->ops_done > ecp_max_ops ||
286 ops > ecp_max_ops - rs_ctx->ops_done ) )
287 {
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100288 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
Hanno Beckerb10c6602018-10-26 13:50:13 +0100289 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100290
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100291 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200292 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100293 }
294
295 return( 0 );
296}
297
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200298/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200299#define ECP_RS_ENTER( SUB ) do { \
300 /* reset ops count for this call if top-level */ \
301 if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
302 rs_ctx->ops_done = 0; \
303 \
304 /* set up our own sub-context if needed */ \
305 if( mbedtls_ecp_restart_is_enabled() && \
306 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
307 { \
308 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
309 if( rs_ctx->SUB == NULL ) \
310 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
311 \
312 ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
313 } \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200314} while( 0 )
315
316/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200317#define ECP_RS_LEAVE( SUB ) do { \
318 /* clear our sub-context when not in progress (done or error) */ \
319 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
320 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
321 { \
322 ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
323 mbedtls_free( rs_ctx->SUB ); \
324 rs_ctx->SUB = NULL; \
325 } \
326 \
327 if( rs_ctx != NULL ) \
328 rs_ctx->depth--; \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200329} while( 0 )
330
331#else /* MBEDTLS_ECP_RESTARTABLE */
332
333#define ECP_RS_ENTER( sub ) (void) rs_ctx;
334#define ECP_RS_LEAVE( sub ) (void) rs_ctx;
335
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200336#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
339 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
340 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
341 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
342 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
343 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
344 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
345 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
346 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
347 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
348 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200349#define ECP_SHORTWEIERSTRASS
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100350#endif
351
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000352#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
353 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200354#define ECP_MONTGOMERY
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100355#endif
356
357/*
358 * Curve types: internal for now, might be exposed later
359 */
360typedef enum
361{
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200362 ECP_TYPE_NONE = 0,
363 ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
364 ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100365} ecp_curve_type;
366
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100367/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200368 * List of supported curves:
369 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200370 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200371 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200372 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100373 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100374 * Curves are listed in order: largest curves first, and for a given size,
375 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200376 *
377 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
382 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
385 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
388 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
391 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
394 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
397 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
400 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100401#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
403 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
406 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
409 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
412 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200415};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100416
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200417#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
418 sizeof( ecp_supported_curves[0] )
419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200421
422/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200423 * List of supported curves and associated info
424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200426{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200427 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200428}
429
430/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100431 * List of supported curves, group ID only
432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100434{
435 static int init_done = 0;
436
437 if( ! init_done )
438 {
439 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 for( curve_info = mbedtls_ecp_curve_list();
443 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100444 curve_info++ )
445 {
446 ecp_supported_grp_id[i++] = curve_info->grp_id;
447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100449
450 init_done = 1;
451 }
452
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200453 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100454}
455
456/*
457 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 for( curve_info = mbedtls_ecp_curve_list();
464 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200465 curve_info++ )
466 {
467 if( curve_info->grp_id == grp_id )
468 return( curve_info );
469 }
470
471 return( NULL );
472}
473
474/*
475 * Get the curve info from the TLS identifier
476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 for( curve_info = mbedtls_ecp_curve_list();
482 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200483 curve_info++ )
484 {
485 if( curve_info->tls_id == tls_id )
486 return( curve_info );
487 }
488
489 return( NULL );
490}
491
492/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100493 * Get the curve info from the name
494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 for( curve_info = mbedtls_ecp_curve_list();
500 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100501 curve_info++ )
502 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200503 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100504 return( curve_info );
505 }
506
507 return( NULL );
508}
509
510/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100511 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100514{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100515 if( grp->G.X.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200516 return( ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100517
518 if( grp->G.Y.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200519 return( ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100520 else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200521 return( ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100522}
523
524/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100525 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100528{
529 if( pt == NULL )
530 return;
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_mpi_init( &pt->X );
533 mbedtls_mpi_init( &pt->Y );
534 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100535}
536
537/*
538 * Initialize (the components of) a group
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100541{
542 if( grp == NULL )
543 return;
544
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200545 grp->id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200546 mbedtls_mpi_init( &grp->P );
547 mbedtls_mpi_init( &grp->A );
548 mbedtls_mpi_init( &grp->B );
549 mbedtls_ecp_point_init( &grp->G );
550 mbedtls_mpi_init( &grp->N );
551 grp->pbits = 0;
552 grp->nbits = 0;
553 grp->h = 0;
554 grp->modp = NULL;
555 grp->t_pre = NULL;
556 grp->t_post = NULL;
557 grp->t_data = NULL;
558 grp->T = NULL;
559 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100560}
561
562/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200563 * Initialize (the components of) a key pair
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200566{
Paul Bakker66d5d072014-06-17 16:39:18 +0200567 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200568 return;
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_ecp_group_init( &key->grp );
571 mbedtls_mpi_init( &key->d );
572 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200573}
574
575/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100576 * Unallocate (the components of) a point
577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100579{
580 if( pt == NULL )
581 return;
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_mpi_free( &( pt->X ) );
584 mbedtls_mpi_free( &( pt->Y ) );
585 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100586}
587
588/*
589 * Unallocate (the components of) a group
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100592{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200593 size_t i;
594
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100595 if( grp == NULL )
596 return;
597
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100598 if( grp->h != 1 )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_mpi_free( &grp->P );
601 mbedtls_mpi_free( &grp->A );
602 mbedtls_mpi_free( &grp->B );
603 mbedtls_ecp_point_free( &grp->G );
604 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100605 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200606
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200607 if( grp->T != NULL )
608 {
609 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_ecp_point_free( &grp->T[i] );
611 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200612 }
613
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500614 mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100615}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100616
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100617/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200618 * Unallocate (the components of) a key pair
619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200621{
Paul Bakker66d5d072014-06-17 16:39:18 +0200622 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200623 return;
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_ecp_group_free( &key->grp );
626 mbedtls_mpi_free( &key->d );
627 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200628}
629
630/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200631 * Copy the contents of a point
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200634{
635 int ret;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
638 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
639 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200640
641cleanup:
642 return( ret );
643}
644
645/*
646 * Copy the contents of a group object
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200649{
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200650 return mbedtls_ecp_group_load( dst, src->id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200651}
652
653/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100654 * Set point to zero
655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100657{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100658 int ret;
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
661 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
662 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100663
664cleanup:
665 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100666}
667
668/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100669 * Tell if a point is zero
670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100672{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100674}
675
676/*
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200677 * Compare two points lazyly
678 */
679int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
680 const mbedtls_ecp_point *Q )
681{
682 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
683 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
684 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
685 {
686 return( 0 );
687 }
688
689 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
690}
691
692/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100693 * Import a non-zero point from ASCII strings
694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100696 const char *x, const char *y )
697{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100698 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
701 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
702 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100703
704cleanup:
705 return( ret );
706}
707
708/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100709 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100712 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100713 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100714{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200715 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100716 size_t plen;
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
719 format != MBEDTLS_ECP_PF_COMPRESSED )
720 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100721
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100722 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100723 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100726 {
727 if( buflen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100729
730 buf[0] = 0x00;
731 *olen = 1;
732
733 return( 0 );
734 }
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100739 {
740 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100741
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100742 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100744
745 buf[0] = 0x04;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
747 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100748 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100750 {
751 *olen = plen + 1;
752
753 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
757 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100758 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100759
760cleanup:
761 return( ret );
762}
763
764/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100765 * Import a point from unsigned binary data (SEC1 2.3.4)
766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100768 const unsigned char *buf, size_t ilen )
769{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100770 int ret;
771 size_t plen;
772
Paul Bakker82788fb2014-10-20 13:59:19 +0200773 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200775
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100776 if( buf[0] == 0x00 )
777 {
778 if( ilen == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( mbedtls_ecp_set_zero( pt ) );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100780 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100782 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100785
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100786 if( buf[0] != 0x04 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100788
789 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
793 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
794 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100795
796cleanup:
797 return( ret );
798}
799
800/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100801 * Import a point from a TLS ECPoint record (RFC 4492)
802 * struct {
803 * opaque point <1..2^8-1>;
804 * } ECPoint;
805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100807 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100808{
809 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100810 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100811
812 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200813 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100814 */
815 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100817
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100818 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100819 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100821
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100822 /*
823 * Save buffer start for read_binary and update buf
824 */
825 buf_start = *buf;
826 *buf += data_len;
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100829}
830
831/*
832 * Export a point as a TLS ECPoint record (RFC 4492)
833 * struct {
834 * opaque point <1..2^8-1>;
835 * } ECPoint;
836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100838 int format, size_t *olen,
839 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100840{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100841 int ret;
842
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100843 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100844 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100845 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100846 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100850 olen, buf + 1, blen - 1) ) != 0 )
851 return( ret );
852
853 /*
854 * write length to the first byte and update total length
855 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200856 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100857 ++*olen;
858
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200859 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100860}
861
862/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100863 * Set a group from an ECParameters record (RFC 4492)
864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100866{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200867 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100869
870 /*
871 * We expect at least three bytes (see below)
872 */
873 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100875
876 /*
877 * First byte is curve_type; only named_curve is handled
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
880 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100881
882 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100883 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100884 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200885 tls_id = *(*buf)++;
886 tls_id <<= 8;
887 tls_id |= *(*buf)++;
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
890 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200891
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200892 return mbedtls_ecp_group_load( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100893}
894
895/*
896 * Write the ECParameters record corresponding to a group (RFC 4492)
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100899 unsigned char *buf, size_t blen )
900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
904 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200905
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100906 /*
907 * We are going to write 3 bytes (see below)
908 */
909 *olen = 3;
910 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100912
913 /*
914 * First byte is curve_type, always named_curve
915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100917
918 /*
919 * Next two bytes are the namedcurve value
920 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200921 buf[0] = curve_info->tls_id >> 8;
922 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100923
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200924 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100925}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100926
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200927/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
929 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200930 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200934{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200935 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200936
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200937 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200939
940 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200942 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200945 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200948
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200949 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
951 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200954 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200956
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200957cleanup:
958 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200959}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200960
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100961/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100962 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100963 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100964 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100966 * bring the result back to this range.
967 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100968 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100969 */
970
971/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100975#define INC_MUL_COUNT mul_count++;
976#else
977#define INC_MUL_COUNT
978#endif
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100981 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100982
983/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200985 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100986 */
987#define MOD_SUB( N ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
989 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100990
991/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200993 * We known P, N and the result are positive, so sub_abs is correct, and
994 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100995 */
996#define MOD_ADD( N ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
998 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100999
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001000#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001001/*
1002 * For curves in short Weierstrass form, we do all the internal operations in
1003 * Jacobian coordinates.
1004 *
1005 * For multiplication, we'll use a comb method with coutermeasueres against
1006 * SPA, hence timing attacks.
1007 */
1008
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001009/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001010 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001011 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001014{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001015 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001019 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001020
Janos Follathb0697532016-08-18 12:38:46 +01001021#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001022 if( mbedtls_internal_ecp_grp_capable( grp ) )
1023 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +01001024#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001027
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001028 /*
1029 * X = X / Z^2 mod p
1030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
1032 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001034
1035 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001036 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
1039 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001040
1041 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001042 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001045
1046cleanup:
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001049
1050 return( ret );
1051}
1052
1053/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001054 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001055 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001056 * (See for example Cohen's "A Course in Computational Algebraic Number
1057 * Theory", Algorithm 10.3.4.)
1058 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001059 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001060 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001061 *
1062 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001065 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001066{
1067 int ret;
1068 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_mpi *c, u, Zi, ZZi;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001070
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001071 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001072 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001073
Janos Follathb0697532016-08-18 12:38:46 +01001074#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001075 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001076 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +01001077#endif
1078
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001079 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001080 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001081
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001082 for( i = 0; i < T_size; i++ )
1083 mbedtls_mpi_init( &c[i] );
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001086
1087 /*
1088 * c[i] = Z_0 * ... * Z_i
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001091 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001094 MOD_MUL( c[i] );
1095 }
1096
1097 /*
1098 * u = 1 / (Z_0 * ... * Z_n) mod P
1099 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001100 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001101
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001102 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001103 {
1104 /*
1105 * Zi = 1 / Z_i mod p
1106 * u = 1 / (Z_0 * ... * Z_i) mod P
1107 */
1108 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001110 }
1111 else
1112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
1114 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001115 }
1116
1117 /*
1118 * proceed as in normalize()
1119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1121 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
1122 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
1123 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001124
1125 /*
1126 * Post-precessing: reclaim some memory by shrinking coordinates
1127 * - not storing Z (always 1)
1128 * - shrinking other coordinates, but still keeping the same number of
1129 * limbs as P, as otherwise it will too likely be regrown too fast.
1130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1132 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1133 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001134
1135 if( i == 0 )
1136 break;
1137 }
1138
1139cleanup:
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001142 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_mpi_free( &c[i] );
1144 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001145
1146 return( ret );
1147}
1148
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001149/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001150 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1151 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1154 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001155 unsigned char inv )
1156{
1157 int ret;
1158 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001162
1163 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1165 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1166 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001167
1168cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001170
1171 return( ret );
1172}
1173
1174/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001175 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001176 *
Peter Dettmance661b22015-02-07 14:43:51 +07001177 * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001178 *
Peter Dettmance661b22015-02-07 14:43:51 +07001179 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1180 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1181 *
1182 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1183 *
1184 * Cost: 1D := 3M + 4S (A == 0)
1185 * 4M + 4S (A == -3)
1186 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1189 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001190{
1191 int ret;
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001192 mbedtls_mpi M, S, T, U;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001195 dbl_count++;
1196#endif
1197
Janos Follathb0697532016-08-18 12:38:46 +01001198#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001199 if( mbedtls_internal_ecp_grp_capable( grp ) )
1200 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001201#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001202
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001203 mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001204
1205 /* Special case for A = -3 */
1206 if( grp->A.p == NULL )
1207 {
Peter Dettmance661b22015-02-07 14:43:51 +07001208 /* M = 3(X + Z^2)(X - Z^2) */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001209 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1210 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
1211 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
1212 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
1213 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001214 }
1215 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001216 {
Peter Dettmance661b22015-02-07 14:43:51 +07001217 /* M = 3.X^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001218 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
1219 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001220
1221 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001222 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001223 {
1224 /* M += A.Z^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001225 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1226 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
1227 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
1228 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001229 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001230 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001231
Peter Dettmance661b22015-02-07 14:43:51 +07001232 /* S = 4.X.Y^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001233 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
1234 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
1235 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
1236 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001237
Peter Dettmance661b22015-02-07 14:43:51 +07001238 /* U = 8.Y^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001239 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
1240 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001241
1242 /* T = M^2 - 2.S */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001243 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
1244 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
1245 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
Peter Dettmance661b22015-02-07 14:43:51 +07001246
1247 /* S = M(S - T) - U */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001248 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
1249 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
1250 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
Peter Dettmance661b22015-02-07 14:43:51 +07001251
1252 /* U = 2.Y.Z */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001253 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
1254 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001255
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001256 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1257 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1258 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001259
1260cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001261 mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001262
1263 return( ret );
1264}
1265
1266/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001267 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001268 *
1269 * The coordinates of Q must be normalized (= affine),
1270 * but those of P don't need to. R is not normalized.
1271 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001272 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001273 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001274 * - at each step, P, Q and R are multiples of the base point, the factor
1275 * being less than its order, so none of them is zero;
1276 * - Q is an odd multiple of the base point, P an even multiple,
1277 * due to the choice of precomputed points in the modified comb method.
1278 * So branches for these cases do not leak secret information.
1279 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001280 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1281 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001282 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1285 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001286{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001287 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001291 add_count++;
1292#endif
1293
Janos Follathb0697532016-08-18 12:38:46 +01001294#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001295 if( mbedtls_internal_ecp_grp_capable( grp ) )
1296 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001297#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001298
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001299 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001300 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
1303 return( mbedtls_ecp_copy( R, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
1306 return( mbedtls_ecp_copy( R, P ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001307
1308 /*
1309 * Make sure Q coordinates are normalized
1310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
1312 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
1315 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1318 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1319 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1320 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1321 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1322 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001323
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001324 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001328 {
1329 ret = ecp_double_jac( grp, R, P );
1330 goto cleanup;
1331 }
1332 else
1333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 ret = mbedtls_ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001335 goto cleanup;
1336 }
1337 }
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1340 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1341 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1342 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1344 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1345 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1346 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1347 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1348 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1349 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1350 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1353 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1354 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001355
1356cleanup:
1357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1359 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001360
1361 return( ret );
1362}
1363
1364/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001365 * Randomize jacobian coordinates:
1366 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001367 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001368 *
1369 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001372 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1373{
1374 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_mpi l, ll;
Janos Follathb0697532016-08-18 12:38:46 +01001376 size_t p_size;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001377 int count = 0;
1378
Janos Follathb0697532016-08-18 12:38:46 +01001379#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001380 if( mbedtls_internal_ecp_grp_capable( grp ) )
1381 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001382#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001383
1384 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001386
1387 /* Generate l such that 1 < l < p */
1388 do
1389 {
Ron Eldorca6ff582017-01-12 14:50:50 +02001390 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1393 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001394
1395 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001399
1400 /* Z = l * Z */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001402
1403 /* X = l^2 * X */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1405 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001406
1407 /* Y = l^3 * Y */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1409 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001410
1411cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001413
1414 return( ret );
1415}
1416
1417/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001418 * Check and define parameters used by the comb method (see below for details)
1419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1421#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001422#endif
1423
1424/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001426
1427/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001429
1430/*
1431 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001432 *
1433 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001434 * modified version that provides resistance to SPA by avoiding zero
1435 * digits in the representation as in [3]. We modify the method further by
1436 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001437 * representation uses one more K_i, due to carries, but saves on the size of
1438 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001439 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001440 * Summary of the comb method and its modifications:
1441 *
1442 * - The goal is to compute m*P for some w*d-bit integer m.
1443 *
1444 * - The basic comb method splits m into the w-bit integers
1445 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1446 * index has residue i modulo d, and computes m * P as
1447 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1448 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1449 *
1450 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1451 * .. + 2^{i-1} S[x[i-1]] - 2^i S[x[i]] + 2^{i+1} S[x[i]] + 2^{i+2} S[x[i+2]] ..,
1452 * thereby successively converting it into a form where all summands
1453 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1454 *
1455 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1456 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1457 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1458 * Performing and iterating this procedure for those x[i] that are even
1459 * (keeping track of carry), we can transform the original sum into one of the form
1460 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1461 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1462 * which is why we are only computing half of it in the first place in
1463 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1464 *
1465 * - For the sake of compactness, only the seven low-order bits of x[i]
1466 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001467 * of x[i] encodes the sign (s_i in the paper): it is set if and only if
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001468 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001469 *
1470 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001471 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001472 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001474 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1475 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001476 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001477static void ecp_comb_recode_core( unsigned char x[], size_t d,
1478 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001479{
1480 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001481 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001482
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001483 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001484
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001485 /* First get the classical comb values (except for x_d = 0) */
1486 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001487 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001489
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001490 /* Now make sure x_1 .. x_d are odd */
1491 c = 0;
1492 for( i = 1; i <= d; i++ )
1493 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001494 /* Add carry and update it */
1495 cc = x[i] & c;
1496 x[i] = x[i] ^ c;
1497 c = cc;
1498
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001499 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001500 adjust = 1 - ( x[i] & 0x01 );
1501 c |= x[i] & ( x[i-1] * adjust );
1502 x[i] = x[i] ^ ( x[i-1] * adjust );
1503 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001504 }
1505}
1506
1507/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001508 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001509 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001510 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001511 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001512 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1513 * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001514 *
1515 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001516 *
1517 * Note: Even comb values (those where P would be omitted from the
1518 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001519 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001520 *
1521 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001522 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001523 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1524 * (3) [add] Computation of all T[i]
1525 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001526 *
1527 * Step 1 can be interrupted but not the others; together with the final
1528 * coordinate normalization they are the largest steps done at once, depending
1529 * on the window size. Here are operation counts for P-256:
1530 *
1531 * step (2) (3) (4)
1532 * w = 5 142 165 208
1533 * w = 4 136 77 160
1534 * w = 3 130 33 136
1535 * w = 2 124 11 124
1536 *
1537 * So if ECC operations are blocking for too long even with a low max_ops
1538 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1539 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1542 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001543 unsigned char w, size_t d,
1544 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001545{
1546 int ret;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001547 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001548 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001549 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001551
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001552#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001553 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001554 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001555 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
1556 goto dbl;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001557 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001558 goto norm_dbl;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001559 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
1560 goto add;
1561 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
1562 goto norm_add;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001563 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001564#else
1565 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001566#endif
1567
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001568#if defined(MBEDTLS_ECP_RESTARTABLE)
1569 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1570 {
1571 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1572
1573 /* initial state for the loop */
1574 rs_ctx->rsm->i = 0;
1575 }
1576
1577dbl:
1578#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001579 /*
1580 * Set T[0] = P and
1581 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001584
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001585#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001586 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
1587 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001588 else
1589#endif
1590 j = 0;
1591
1592 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001593 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001594 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001595
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001596 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001597 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001598
1599 if( j % d == 0 )
1600 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1601
1602 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001603 }
1604
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001605#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001606 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1607 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1608
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001609norm_dbl:
1610#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001611 /*
1612 * Normalize current elements in T. As T has holes,
1613 * use an auxiliary array of pointers to elements in T.
1614 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001615 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001616 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001617 TT[j++] = T + i;
1618
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001619 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001620
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001621 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001622
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001623#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001624 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1625 rs_ctx->rsm->state = ecp_rsm_pre_add;
1626
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001627add:
1628#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001629 /*
1630 * Compute the remaining ones using the minimal number of additions
1631 * Be careful to update T[2^l] only after using it!
1632 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001633 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001634
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001635 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001636 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001637 j = i;
1638 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001640 }
1641
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001642#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001643 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1644 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1645
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001646norm_add:
1647#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001648 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001649 * Normalize final elements in T. Even though there are no holes now, we
1650 * still need the auxiliary array for homogeneity with the previous
1651 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001652 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001653 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001654 TT[j] = T + j + 1;
1655
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001656 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001657
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001658 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001659
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001660cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001661#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001662 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1663 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001664 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001665 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001666 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001667 }
1668#endif
Janos Follathb0697532016-08-18 12:38:46 +01001669
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001670 return( ret );
1671}
1672
1673/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001674 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001675 *
1676 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001679 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001680 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001681{
1682 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001683 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001684
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001685 /* Ignore the "sign" bit and scale down */
1686 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001687
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001688 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001689 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1692 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001693 }
1694
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001695 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001697
1698cleanup:
1699 return( ret );
1700}
1701
1702/*
1703 * Core multiplication algorithm for the (modified) comb method.
1704 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001705 *
1706 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001709 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001710 const unsigned char x[], size_t d,
1711 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001712 void *p_rng,
1713 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001714{
1715 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001717 size_t i;
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001720
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001721#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001722 (void) rs_ctx;
1723#endif
1724
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001725#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001726 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1727 rs_ctx->rsm->state != ecp_rsm_comb_core )
1728 {
1729 rs_ctx->rsm->i = 0;
1730 rs_ctx->rsm->state = ecp_rsm_comb_core;
1731 }
1732
1733 /* new 'if' instead of nested for the sake of the 'else' branch */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001734 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001735 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001736 /* restore current index (R already pointing to rs_ctx->rsm->R) */
1737 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001738 }
1739 else
1740#endif
1741 {
1742 /* Start with a non-zero point and randomize its coordinates */
1743 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001744 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001745 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
1746 if( f_rng != 0 )
1747 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
1748 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001749
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001750 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001751 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001752 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001753 --i;
1754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001756 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001758 }
1759
1760cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01001761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001763
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001764#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001765 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1766 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001767 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001768 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001769 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001770 }
1771#endif
1772
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001773 return( ret );
1774}
1775
1776/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001777 * Recode the scalar to get constant-time comb multiplication
1778 *
1779 * As the actual scalar recoding needs an odd scalar as a starting point,
1780 * this wrapper ensures that by replacing m by N - m if necessary, and
1781 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001782 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001783 * This works because we only support large prime order for Short Weierstrass
1784 * curves, so N is always odd hence either m or N - m is.
1785 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001786 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001787 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001788static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
1789 const mbedtls_mpi *m,
1790 unsigned char k[COMB_MAX_D + 1],
1791 size_t d,
1792 unsigned char w,
1793 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001794{
1795 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001796 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001797
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001798 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001799 mbedtls_mpi_init( &mm );
1800
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001801 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001802 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
1803 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
1804
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001805 /* do we need the parity trick? */
1806 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
1807
1808 /* execute parity fix in constant time */
1809 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001810 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001811 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
1812
1813 /* actual scalar recoding */
1814 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001815
1816cleanup:
1817 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001818 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001819
1820 return( ret );
1821}
1822
1823/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001824 * Perform comb multiplication (for short Weierstrass curves)
1825 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001826 *
1827 * Scalar recoding may use a parity trick that makes us compute -m * P,
1828 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001829 */
1830static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
1831 mbedtls_ecp_point *R,
1832 const mbedtls_mpi *m,
1833 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001834 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001835 unsigned char w,
1836 size_t d,
1837 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001838 void *p_rng,
1839 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001840{
1841 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001842 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001843 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001844 mbedtls_ecp_point *RR = R;
1845
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001846#if defined(MBEDTLS_ECP_RESTARTABLE)
1847 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1848 {
1849 RR = &rs_ctx->rsm->R;
1850
1851 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
1852 goto final_norm;
1853 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001854#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001855
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001856 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
1857 &parity_trick ) );
1858 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
1859 f_rng, p_rng, rs_ctx ) );
1860 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
1861
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001862#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001863 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001864 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001865
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001866final_norm:
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01001867#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001868 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001869 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
1870
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001871#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02001872 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1873 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001874#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001875
1876cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001877 return( ret );
1878}
1879
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001880/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01001881 * Pick window size based on curve size and whether we optimize for base point
1882 */
1883static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
1884 unsigned char p_eq_g )
1885{
1886 unsigned char w;
1887
1888 /*
1889 * Minimize the number of multiplications, that is minimize
1890 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
1891 * (see costs of the various parts, with 1S = 1M)
1892 */
1893 w = grp->nbits >= 384 ? 5 : 4;
1894
1895 /*
1896 * If P == G, pre-compute a bit more, since this may be re-used later.
1897 * Just adding one avoids upping the cost of the first mul too much,
1898 * and the memory cost too.
1899 */
1900 if( p_eq_g )
1901 w++;
1902
1903 /*
1904 * Make sure w is within bounds.
1905 * (The last test is useful only for very small curves in the test suite.)
1906 */
1907 if( w > MBEDTLS_ECP_WINDOW_SIZE )
1908 w = MBEDTLS_ECP_WINDOW_SIZE;
1909 if( w >= grp->nbits )
1910 w = 2;
1911
1912 return( w );
1913}
1914
1915/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01001916 * Multiplication using the comb method - for curves in short Weierstrass form
1917 *
1918 * This function is mainly responsible for administrative work:
1919 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001920 * - managing the table of precomputed points (passed between the below two
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01001921 * functions): allocation, computation, ownership tranfer, freeing.
1922 *
1923 * It delegates the actual arithmetic work to:
1924 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
1925 *
1926 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1929 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001930 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001931 void *p_rng,
1932 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001933{
1934 int ret;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001935 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001936 size_t d;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001937 unsigned char T_size, T_ok;
1938 mbedtls_ecp_point *T;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001939
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02001940 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01001941
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01001942 /* Is P the base point ? */
1943#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
1944 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1945 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02001946#else
1947 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01001948#endif
1949
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001950 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01001951 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001952 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001953 d = ( grp->nbits + w - 1 ) / w;
1954
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001955 /* Pre-computed table: do we have it already for the base point? */
1956 if( p_eq_g && grp->T != NULL )
1957 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001958 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001959 T = grp->T;
1960 T_ok = 1;
1961 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001962 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001963#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001964 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001965 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001966 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01001967 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001968 T = rs_ctx->rsm->T;
1969 rs_ctx->rsm->T = NULL;
1970 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001971
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02001972 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001973 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001974 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001975 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001976#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001977 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001978 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001979 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001980 if( T == NULL )
1981 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001982 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001983 goto cleanup;
1984 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001985
1986 for( i = 0; i < T_size; i++ )
1987 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02001988
1989 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001990 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001991
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001992 /* Compute table (or finish computing it) if not done already */
1993 if( !T_ok )
1994 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001995 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001996
1997 if( p_eq_g )
1998 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001999 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002000 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002001 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002002 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002003 }
2004 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002005
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002006 /* Actual comb multiplication using precomputed points */
2007 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002008 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002009 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002010
2011cleanup:
2012
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002013 /* does T belong to the group? */
2014 if( T == grp->T )
2015 T = NULL;
2016
2017 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002018#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002019 if( rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002020 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002021 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002022 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002023 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002024 T = NULL;
2025 }
2026#endif
2027
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002028 /* did T belong to us? then let's destroy it! */
2029 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002030 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002031 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 mbedtls_ecp_point_free( &T[i] );
2033 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002034 }
2035
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002036 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002037#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002038 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2039#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002040 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01002041 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002043
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002044 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002045
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002046 return( ret );
2047}
2048
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002049#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002050
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002051#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002052/*
2053 * For Montgomery curves, we do all the internal arithmetic in projective
2054 * coordinates. Import/export of points uses only the x coordinates, which is
2055 * internaly represented as X / Z.
2056 *
2057 * For scalar multiplication, we'll use a Montgomery ladder.
2058 */
2059
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002060/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002061 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2062 * Cost: 1M + 1I
2063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002065{
2066 int ret;
2067
Janos Follathb0697532016-08-18 12:38:46 +01002068#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002069 if( mbedtls_internal_ecp_grp_capable( grp ) )
2070 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002071#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
2074 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
2075 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002076
2077cleanup:
2078 return( ret );
2079}
2080
2081/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002082 * Randomize projective x/z coordinates:
2083 * (X, Z) -> (l X, l Z) for random l
2084 * This is sort of the reverse operation of ecp_normalize_mxz().
2085 *
2086 * This countermeasure was first suggested in [2].
2087 * Cost: 2M
2088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002090 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2091{
2092 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 mbedtls_mpi l;
Janos Follathb0697532016-08-18 12:38:46 +01002094 size_t p_size;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002095 int count = 0;
2096
Janos Follathb0697532016-08-18 12:38:46 +01002097#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002098 if( mbedtls_internal_ecp_grp_capable( grp ) )
2099 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Janos Follath372697b2016-10-28 16:53:11 +01002100#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002101
2102 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002104
2105 /* Generate l such that 1 < l < p */
2106 do
2107 {
Ron Eldorca6ff582017-01-12 14:50:50 +02002108 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
2111 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002112
2113 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
2119 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002120
2121cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002123
2124 return( ret );
2125}
2126
2127/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002128 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2129 * for Montgomery curves in x/z coordinates.
2130 *
2131 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2132 * with
2133 * d = X1
2134 * P = (X2, Z2)
2135 * Q = (X3, Z3)
2136 * R = (X4, Z4)
2137 * S = (X5, Z5)
2138 * and eliminating temporary variables tO, ..., t4.
2139 *
2140 * Cost: 5M + 4S
2141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2143 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2144 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2145 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002146{
2147 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002149
Janos Follathb0697532016-08-18 12:38:46 +01002150#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002151 if( mbedtls_internal_ecp_grp_capable( grp ) )
2152 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002153#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2156 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2157 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
2160 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
2161 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
2162 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
2163 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
2164 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
2165 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
2166 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
2167 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
2168 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
2169 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
2170 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
2171 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
2172 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
2173 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
2174 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
2175 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
2176 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002177
2178cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2180 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2181 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002182
2183 return( ret );
2184}
2185
2186/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002187 * Multiplication with Montgomery ladder in x/z coordinates,
2188 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2191 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002192 int (*f_rng)(void *, unsigned char *, size_t),
2193 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002194{
2195 int ret;
2196 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002197 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 mbedtls_ecp_point RP;
2199 mbedtls_mpi PX;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002202
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002203 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2205 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002206
2207 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2209 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2210 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002211
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002212 /* RP.X might be sligtly larger than P, so reduce it */
2213 MOD_ADD( RP.X );
2214
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002215 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002216 if( f_rng != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002218
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002219 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002220 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002221 while( i-- > 0 )
2222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002224 /*
2225 * if (b) R = 2R + P else R = 2R,
2226 * which is:
2227 * if (b) double_add( RP, R, RP, R )
2228 * else double_add( R, RP, R, RP )
2229 * but using safe conditional swaps to avoid leaks
2230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2232 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2233 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2234 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2235 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002236 }
2237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002239
2240cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002242
2243 return( ret );
2244}
2245
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002246#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002247
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002248/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002249 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002250 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002251int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002253 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2254 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002255{
Janos Follathb0697532016-08-18 12:38:46 +01002256 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002257#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2258 char is_grp_capable = 0;
2259#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002260
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002261#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002262 /* reset ops count for this call if top-level */
2263 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2264 rs_ctx->ops_done = 0;
2265#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002266
Janos Follathc44ab972016-11-18 16:38:23 +00002267#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002268 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002269 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002270#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002271
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002272#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002273 /* skip argument check when restarting */
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002274 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002275#endif
2276 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002277 /* check_privkey is free */
2278 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2279
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002280 /* Common sanity checks */
2281 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2282 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002283 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002284
2285 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002286#if defined(ECP_MONTGOMERY)
2287 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002288 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002289#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002290#if defined(ECP_SHORTWEIERSTRASS)
2291 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002292 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002293#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002294
Janos Follath6c8ccd52016-11-29 15:37:09 +00002295cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002296
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002297#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002298 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002299 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002300#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002301
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002302#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002303 if( rs_ctx != NULL )
2304 rs_ctx->depth--;
2305#endif
2306
Janos Follathb0697532016-08-18 12:38:46 +01002307 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002308}
2309
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002310/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002311 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002312 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002313int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002314 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002315 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002316{
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002317 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002318}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002319
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002320#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002321/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002322 * Check that an affine point is valid as a public key,
2323 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002326{
2327 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002329
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002330 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2332 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2333 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2334 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2335 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002338
2339 /*
2340 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002341 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
2344 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002345
2346 /* Special case for A = -3 */
2347 if( grp->A.p == NULL )
2348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002350 }
2351 else
2352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002354 }
2355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
2357 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2360 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002361
2362cleanup:
2363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002365
2366 return( ret );
2367}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002368#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002369
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002370/*
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002371 * R = m * P with shortcuts for m == 1 and m == -1
2372 * NOT constant-time - ONLY for short Weierstrass!
2373 */
2374static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2375 mbedtls_ecp_point *R,
2376 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002377 const mbedtls_ecp_point *P,
2378 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002379{
2380 int ret;
2381
2382 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
2383 {
2384 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2385 }
2386 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2387 {
2388 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2389 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2390 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2391 }
2392 else
2393 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002394 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2395 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002396 }
2397
2398cleanup:
2399 return( ret );
2400}
2401
2402/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002403 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002404 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002405 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002406int mbedtls_ecp_muladd_restartable(
2407 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002408 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002409 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2410 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002411{
2412 int ret;
2413 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002414 mbedtls_ecp_point *pmP = &mP;
2415 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002416#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2417 char is_grp_capable = 0;
2418#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002419
2420 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
2421 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2422
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002423 mbedtls_ecp_point_init( &mP );
2424
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002425 ECP_RS_ENTER( ma );
2426
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002427#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002428 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2429 {
2430 /* redirect intermediate results to restart context */
2431 pmP = &rs_ctx->ma->mP;
2432 pR = &rs_ctx->ma->R;
2433
2434 /* jump to next operation */
2435 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2436 goto mul2;
2437 if( rs_ctx->ma->state == ecp_rsma_add )
2438 goto add;
2439 if( rs_ctx->ma->state == ecp_rsma_norm )
2440 goto norm;
2441 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002442#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002443
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002444 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002445#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002446 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002447 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002448
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002449mul2:
2450#endif
2451 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Janos Follathaf6f2692018-12-07 09:55:24 +00002452
2453#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2454 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2455 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2456#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2457
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002458#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002459 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002460 rs_ctx->ma->state = ecp_rsma_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002461
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002462add:
2463#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002464 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002465 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002466#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002467 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002468 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002469
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002470norm:
2471#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002472 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002473 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2474
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002475#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002476 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2477 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2478#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002479
2480cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002481#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002482 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002483 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002484#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002485
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002486 mbedtls_ecp_point_free( &mP );
2487
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002488 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002489
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002490 return( ret );
2491}
2492
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002493/*
2494 * Linear combination
2495 * NOT constant-time
2496 */
2497int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2498 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2499 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2500{
2501 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2502}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002503
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002504#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002505/*
2506 * Check validity of a public key for Montgomery curves with x-only schemes
2507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002509{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002510 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002511 /* Allow any public value, if it's too big then we'll just reduce it mod p
2512 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2514 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002515
2516 return( 0 );
2517}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002518#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002519
2520/*
2521 * Check that a point is valid as a public key
2522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002524{
2525 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2527 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002528
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002529#if defined(ECP_MONTGOMERY)
2530 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002531 return( ecp_check_pubkey_mx( grp, pt ) );
2532#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002533#if defined(ECP_SHORTWEIERSTRASS)
2534 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002535 return( ecp_check_pubkey_sw( grp, pt ) );
2536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002538}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002539
2540/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002544{
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002545#if defined(ECP_MONTGOMERY)
2546 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002547 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002548 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2550 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002551 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002553
2554 /* see [Curve25519] page 5 */
2555 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
2556 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2557
2558 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002559 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002560#endif /* ECP_MONTGOMERY */
2561#if defined(ECP_SHORTWEIERSTRASS)
2562 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002563 {
2564 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2566 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2567 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002568 else
2569 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002570 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002571#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002574}
2575
2576/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002577 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002578 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002579int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
2580 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002581 int (*f_rng)(void *, unsigned char *, size_t),
2582 void *p_rng )
2583{
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002584 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakker66d5d072014-06-17 16:39:18 +02002585 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002586
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002587#if defined(ECP_MONTGOMERY)
2588 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002589 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002590 /* [M225] page 5 */
2591 size_t b;
2592
Janos Follath98e28a72016-05-31 14:03:54 +01002593 do {
2594 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
2595 } while( mbedtls_mpi_bitlen( d ) == 0);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002596
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002597 /* Make sure the most significant bit is nbits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002598 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002599 if( b > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002603
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002604 /* Make sure the last two bits are unset for Curve448, three bits for
2605 Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
2607 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002608 if( grp->nbits == 254 )
2609 {
2610 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
2611 }
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002612 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002613#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002614
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002615#if defined(ECP_SHORTWEIERSTRASS)
2616 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002617 {
2618 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002619 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01002620
2621 /*
2622 * Match the procedure given in RFC 6979 (deterministic ECDSA):
2623 * - use the same byte ordering;
2624 * - keep the leftmost nbits bits of the generated octet string;
2625 * - try until result is in the desired range.
2626 * This also avoids any biais, which is especially important for ECDSA.
2627 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002628 do
2629 {
Hanno Becker7c8cb9c2017-10-17 15:19:38 +01002630 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002632
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01002633 /*
2634 * Each try has at worst a probability 1/2 of failing (the msb has
2635 * a probability 1/2 of being 0, and then the result will be < N),
2636 * so after 30 tries failure probability is a most 2**(-30).
2637 *
2638 * For most curves, 1 try is enough with overwhelming probability,
2639 * since N starts with a lot of 1s in binary, but some curves
2640 * such as secp224k1 are actually very close to the worst case.
2641 */
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02002642 if( ++count > 30 )
2643 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2646 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002647 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002648#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002649
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002650cleanup:
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002651 return( ret );
2652}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002653
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002654/*
2655 * Generate a keypair with configurable base point
2656 */
2657int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
2658 const mbedtls_ecp_point *G,
2659 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2660 int (*f_rng)(void *, unsigned char *, size_t),
2661 void *p_rng )
2662{
2663 int ret;
2664
2665 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
2666 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
2667
2668cleanup:
2669 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02002670}
2671
2672/*
2673 * Generate key pair, wrapper for conventional base point
2674 */
2675int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
2676 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2677 int (*f_rng)(void *, unsigned char *, size_t),
2678 void *p_rng )
2679{
2680 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002681}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01002682
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002683/*
2684 * Generate a keypair, prettier wrapper
2685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002687 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2688{
2689 int ret;
2690
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02002691 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002692 return( ret );
2693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002695}
2696
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002697/*
2698 * Check a public-private key pair
2699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002701{
2702 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 mbedtls_ecp_point Q;
2704 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002707 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
2709 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
2710 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002713 }
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_ecp_point_init( &Q );
2716 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 /* mbedtls_ecp_mul() needs a non-const group... */
2719 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002720
2721 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
2725 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
2726 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002729 goto cleanup;
2730 }
2731
2732cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 mbedtls_ecp_point_free( &Q );
2734 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002735
2736 return( ret );
2737}
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002740
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002741/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002742 * Checkup routine
2743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002745{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002746 int ret;
2747 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 mbedtls_ecp_group grp;
2749 mbedtls_ecp_point R, P;
2750 mbedtls_mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002751 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002752 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002753 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002754 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002755 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002756 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002757 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002758 "400000000000000000000000000000000000000000000000", /* one and zeros */
2759 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2760 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002761 };
2762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 mbedtls_ecp_group_init( &grp );
2764 mbedtls_ecp_point_init( &R );
2765 mbedtls_ecp_point_init( &P );
2766 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002767
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002768 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02002770 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002771#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02002772 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002773#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002774
2775 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002777
2778 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
2780 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002781
2782 add_count = 0;
2783 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002784 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
2786 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002787
2788 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2789 {
2790 add_c_prev = add_count;
2791 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002792 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002793 add_count = 0;
2794 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002795 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
2798 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002799
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002800 if( add_count != add_c_prev ||
2801 dbl_count != dbl_c_prev ||
2802 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002803 {
2804 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002806
2807 ret = 1;
2808 goto cleanup;
2809 }
2810 }
2811
2812 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002814
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002815 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002817 /* We computed P = 2G last time, use it */
2818
2819 add_count = 0;
2820 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002821 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
2823 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002824
2825 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2826 {
2827 add_c_prev = add_count;
2828 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002829 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002830 add_count = 0;
2831 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002832 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
2835 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002836
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002837 if( add_count != add_c_prev ||
2838 dbl_count != dbl_c_prev ||
2839 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002840 {
2841 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002843
2844 ret = 1;
2845 goto cleanup;
2846 }
2847 }
2848
2849 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002851
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002852cleanup:
2853
2854 if( ret < 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 mbedtls_ecp_group_free( &grp );
2858 mbedtls_ecp_point_free( &R );
2859 mbedtls_ecp_point_free( &P );
2860 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002861
2862 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002864
2865 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002866}
2867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002869
Janos Follathb0697532016-08-18 12:38:46 +01002870#endif /* !MBEDTLS_ECP_ALT */
2871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872#endif /* MBEDTLS_ECP_C */