blob: b120adf74bbfce45969d548e763998331ed4ebd5 [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
Hanno Becker4f8e8e52018-12-14 15:08:03 +000089/* Parameter validation macros based on platform_util.h */
90#define ECP_VALIDATE_RET( cond ) \
91 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
92#define ECP_VALIDATE( cond ) \
93 MBEDTLS_INTERNAL_VALIDATE( cond )
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000096#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020097#else
Rich Evans00ab4702015-02-06 13:43:58 +000098#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000099#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200101#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +0200103#endif
104
Janos Follath47d28f02016-11-01 13:22:05 +0000105#include "mbedtls/ecp_internal.h"
Janos Follathb0697532016-08-18 12:38:46 +0100106
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200107#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
108#if defined(MBEDTLS_CTR_DRBG_C)
109#include "mbedtls/ctr_drbg.h"
110#elif defined(MBEDTLS_HMAC_DRBG_C)
111#include "mbedtls/hmac_drbg.h"
112#else
113#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
114#endif
115#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
116
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +0100117#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
118 !defined(inline) && !defined(__cplusplus)
Paul Bakker6a6087e2013-10-28 18:53:08 +0100119#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +0200120#endif
Paul Bakker6a6087e2013-10-28 18:53:08 +0100121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100123/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100124 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200125 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100126 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100127static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100128#endif
129
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200130#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
131/*
132 * Currently ecp_mul() takes a RNG function as an argument, used for
133 * side-channel protection, but it can be NULL. The initial reasonning was
134 * that people will pass non-NULL RNG when they care about side-channels, but
135 * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with
136 * no opportunity for the user to do anything about it.
137 *
138 * The obvious strategies for addressing that include:
139 * - change those APIs so that they take RNG arguments;
140 * - require a global RNG to be available to all crypto modules.
141 *
142 * Unfortunately those would break compatibility. So what we do instead is
143 * have our own internal DRBG instance, seeded from the secret scalar.
144 *
145 * The following is a light-weight abstraction layer for doing that with
146 * CTR_DRBG or HMAC_DRBG.
147 */
148
149#if defined(MBEDTLS_CTR_DRBG_C)
150/* DRBG context type */
151typedef mbedtls_ctr_drbg_context ecp_drbg_context;
152
153/* DRBG context init */
154static inline void ecp_drbg_init( ecp_drbg_context *ctx )
155{
156 mbedtls_ctr_drbg_init( ctx );
157}
158
159/* DRBG context free */
160static inline void ecp_drbg_free( ecp_drbg_context *ctx )
161{
162 mbedtls_ctr_drbg_free( ctx );
163}
164
165/* DRBG function */
166static inline int ecp_drbg_random( void *p_rng,
167 unsigned char *output, size_t output_len )
168{
169 return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
170}
171
172/*
173 * Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does,
174 * we need to pass an entropy function when seeding. So we use a dummy
175 * function for that, and pass the actual entropy as customisation string.
176 * (During seeding of CTR_DRBG the entropy input and customisation string are
177 * concatenated before being used to update the secret state.)
178 */
179static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
180{
181 (void) ctx;
182 memset( out, 0, len );
183 return( 0 );
184}
185
186/* DRBG context seeding */
187static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret )
188{
189 const unsigned char *secret_p = (const unsigned char *) secret->p;
190 const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint );
191
192 return( mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL,
193 secret_p, secret_size ) );
194}
195
196#elif defined(MBEDTLS_HMAC_DRBG_C)
197/* DRBG context type */
198typedef mbedtls_hmac_drbg_context ecp_drbg_context;
199
200/* DRBG context init */
201static inline void ecp_drbg_init( ecp_drbg_context *ctx )
202{
203 mbedtls_hmac_drbg_init( ctx );
204}
205
206/* DRBG context free */
207static inline void ecp_drbg_free( ecp_drbg_context *ctx )
208{
209 mbedtls_hmac_drbg_free( ctx );
210}
211
212/* DRBG function */
213static inline int ecp_drbg_random( void *p_rng,
214 unsigned char *output, size_t output_len )
215{
216 return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
217}
218
219/* DRBG context seeding */
220static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret )
221{
222 const unsigned char *secret_p = (const unsigned char *) secret->p;
223 const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint );
224
225 /* The list starts with strong hashes */
226 const mbedtls_md_type_t md_type = mbedtls_md_list()[0];
227 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type );
228
229 return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_size ) );
230}
231
232#else
233#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
234#endif /* DRBG modules */
235#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
236
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200237#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100238/*
239 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200240 *
241 * Default value 0 means that ECC operations will not yield.
242 * Note that regardless of the value of ecp_max_ops, always at
243 * least one step is performed before yielding.
244 *
245 * Setting ecp_max_ops=1 can be suitable for testing purposes
246 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100247 */
248static unsigned ecp_max_ops = 0;
249
250/*
251 * Set ecp_max_ops
252 */
253void mbedtls_ecp_set_max_ops( unsigned max_ops )
254{
255 ecp_max_ops = max_ops;
256}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100257
258/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200259 * Check if restart is enabled
260 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200261int mbedtls_ecp_restart_is_enabled( void )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200262{
263 return( ecp_max_ops != 0 );
264}
265
266/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200267 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100268 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200269struct mbedtls_ecp_restart_mul
270{
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100271 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100272 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100273 mbedtls_ecp_point *T; /* table for precomputed points */
274 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200275 enum { /* what were we doing last time we returned? */
276 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
277 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100278 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
279 ecp_rsm_pre_add, /* precompute remaining points by adding */
280 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200281 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100282 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100283 } state;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100284};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100285
286/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200287 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100288 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200289static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100290{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200291 mbedtls_ecp_point_init( &ctx->R );
292 ctx->i = 0;
293 ctx->T = NULL;
294 ctx->T_size = 0;
295 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100296}
297
298/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200299 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100300 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200301static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100302{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100303 unsigned char i;
304
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100305 if( ctx == NULL )
306 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100307
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100308 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100309
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200310 if( ctx->T != NULL )
311 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100312 for( i = 0; i < ctx->T_size; i++ )
313 mbedtls_ecp_point_free( ctx->T + i );
314 mbedtls_free( ctx->T );
315 }
316
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200317 ecp_restart_rsm_init( ctx );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100318}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100319
320/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200321 * Restart context for ecp_muladd()
322 */
323struct mbedtls_ecp_restart_muladd
324{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200325 mbedtls_ecp_point mP; /* mP value */
326 mbedtls_ecp_point R; /* R intermediate result */
327 enum { /* what should we do next? */
328 ecp_rsma_mul1 = 0, /* first multiplication */
329 ecp_rsma_mul2, /* second multiplication */
330 ecp_rsma_add, /* addition */
331 ecp_rsma_norm, /* normalization */
332 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200333};
334
335/*
336 * Init restart_muladd sub-context
337 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200338static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200339{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200340 mbedtls_ecp_point_init( &ctx->mP );
341 mbedtls_ecp_point_init( &ctx->R );
342 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200343}
344
345/*
346 * Free the components of a restart_muladd sub-context
347 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200348static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200349{
350 if( ctx == NULL )
351 return;
352
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200353 mbedtls_ecp_point_free( &ctx->mP );
354 mbedtls_ecp_point_free( &ctx->R );
355
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200356 ecp_restart_ma_init( ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200357}
358
359/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200360 * Initialize a restart context
361 */
362void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
363{
Hanno Becker80f71682018-12-18 23:44:43 +0000364 ECP_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200365 ctx->ops_done = 0;
366 ctx->depth = 0;
367 ctx->rsm = NULL;
368 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200369}
370
371/*
372 * Free the components of a restart context
373 */
374void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
375{
376 if( ctx == NULL )
377 return;
378
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200379 ecp_restart_rsm_free( ctx->rsm );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200380 mbedtls_free( ctx->rsm );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200381
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200382 ecp_restart_ma_free( ctx->ma );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200383 mbedtls_free( ctx->ma );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200384
385 mbedtls_ecp_restart_init( ctx );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200386}
387
388/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100389 * Check if we can do the next step
390 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200391int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
392 mbedtls_ecp_restart_ctx *rs_ctx,
393 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100394{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000395 ECP_VALIDATE_RET( grp != NULL );
396
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200397 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100398 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100399 /* scale depending on curve size: the chosen reference is 256-bit,
400 * and multiplication is quadratic. Round to the closest integer. */
401 if( grp->pbits >= 512 )
402 ops *= 4;
403 else if( grp->pbits >= 384 )
404 ops *= 2;
405
Hanno Beckerb10c6602018-10-26 13:50:13 +0100406 /* Avoid infinite loops: always allow first step.
407 * Because of that, however, it's not generally true
408 * that ops_done <= ecp_max_ops, so the check
409 * ops_done > ecp_max_ops below is mandatory. */
410 if( ( rs_ctx->ops_done != 0 ) &&
411 ( rs_ctx->ops_done > ecp_max_ops ||
412 ops > ecp_max_ops - rs_ctx->ops_done ) )
413 {
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100414 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
Hanno Beckerb10c6602018-10-26 13:50:13 +0100415 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100416
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100417 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200418 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100419 }
420
421 return( 0 );
422}
423
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200424/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200425#define ECP_RS_ENTER( SUB ) do { \
426 /* reset ops count for this call if top-level */ \
427 if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
428 rs_ctx->ops_done = 0; \
429 \
430 /* set up our own sub-context if needed */ \
431 if( mbedtls_ecp_restart_is_enabled() && \
432 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
433 { \
434 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
435 if( rs_ctx->SUB == NULL ) \
436 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
437 \
438 ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
439 } \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200440} while( 0 )
441
442/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200443#define ECP_RS_LEAVE( SUB ) do { \
444 /* clear our sub-context when not in progress (done or error) */ \
445 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
446 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
447 { \
448 ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
449 mbedtls_free( rs_ctx->SUB ); \
450 rs_ctx->SUB = NULL; \
451 } \
452 \
453 if( rs_ctx != NULL ) \
454 rs_ctx->depth--; \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200455} while( 0 )
456
457#else /* MBEDTLS_ECP_RESTARTABLE */
458
459#define ECP_RS_ENTER( sub ) (void) rs_ctx;
460#define ECP_RS_LEAVE( sub ) (void) rs_ctx;
461
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200462#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
465 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
466 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
467 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
468 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
469 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
470 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
471 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
472 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
473 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
474 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200475#define ECP_SHORTWEIERSTRASS
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100476#endif
477
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000478#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
479 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200480#define ECP_MONTGOMERY
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100481#endif
482
483/*
484 * Curve types: internal for now, might be exposed later
485 */
486typedef enum
487{
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200488 ECP_TYPE_NONE = 0,
489 ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
490 ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100491} ecp_curve_type;
492
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100493/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200494 * List of supported curves:
495 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200496 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200497 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200498 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100499 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100500 * Curves are listed in order: largest curves first, and for a given size,
501 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200502 *
503 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
508 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
511 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100512#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
514 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
517 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
520 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
523 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
526 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
529 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
532 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
535 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
538 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200541};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100542
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200543#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
544 sizeof( ecp_supported_curves[0] )
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200547
548/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200549 * List of supported curves and associated info
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200552{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200553 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200554}
555
556/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100557 * List of supported curves, group ID only
558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100560{
561 static int init_done = 0;
562
563 if( ! init_done )
564 {
565 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 for( curve_info = mbedtls_ecp_curve_list();
569 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100570 curve_info++ )
571 {
572 ecp_supported_grp_id[i++] = curve_info->grp_id;
573 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100575
576 init_done = 1;
577 }
578
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100580}
581
582/*
583 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585const 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 +0200586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 for( curve_info = mbedtls_ecp_curve_list();
590 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200591 curve_info++ )
592 {
593 if( curve_info->grp_id == grp_id )
594 return( curve_info );
595 }
596
597 return( NULL );
598}
599
600/*
601 * Get the curve info from the TLS identifier
602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 for( curve_info = mbedtls_ecp_curve_list();
608 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200609 curve_info++ )
610 {
611 if( curve_info->tls_id == tls_id )
612 return( curve_info );
613 }
614
615 return( NULL );
616}
617
618/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100619 * Get the curve info from the name
620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100624
Hanno Beckerb7a04a72018-12-18 23:50:21 +0000625 if( name == NULL )
626 return( NULL );
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 for( curve_info = mbedtls_ecp_curve_list();
629 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100630 curve_info++ )
631 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200632 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100633 return( curve_info );
634 }
635
636 return( NULL );
637}
638
639/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100640 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100643{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100644 if( grp->G.X.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200645 return( ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100646
647 if( grp->G.Y.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200648 return( ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100649 else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200650 return( ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100651}
652
653/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100654 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100657{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000658 ECP_VALIDATE( pt != NULL );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_mpi_init( &pt->X );
661 mbedtls_mpi_init( &pt->Y );
662 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100663}
664
665/*
666 * Initialize (the components of) a group
667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100669{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000670 ECP_VALIDATE( grp != NULL );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100671
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200672 grp->id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200673 mbedtls_mpi_init( &grp->P );
674 mbedtls_mpi_init( &grp->A );
675 mbedtls_mpi_init( &grp->B );
676 mbedtls_ecp_point_init( &grp->G );
677 mbedtls_mpi_init( &grp->N );
678 grp->pbits = 0;
679 grp->nbits = 0;
680 grp->h = 0;
681 grp->modp = NULL;
682 grp->t_pre = NULL;
683 grp->t_post = NULL;
684 grp->t_data = NULL;
685 grp->T = NULL;
686 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100687}
688
689/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200690 * Initialize (the components of) a key pair
691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200693{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000694 ECP_VALIDATE( key != NULL );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_ecp_group_init( &key->grp );
697 mbedtls_mpi_init( &key->d );
698 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200699}
700
701/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100702 * Unallocate (the components of) a point
703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100705{
706 if( pt == NULL )
707 return;
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_mpi_free( &( pt->X ) );
710 mbedtls_mpi_free( &( pt->Y ) );
711 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100712}
713
714/*
715 * Unallocate (the components of) a group
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100718{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200719 size_t i;
720
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100721 if( grp == NULL )
722 return;
723
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100724 if( grp->h != 1 )
725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_mpi_free( &grp->P );
727 mbedtls_mpi_free( &grp->A );
728 mbedtls_mpi_free( &grp->B );
729 mbedtls_ecp_point_free( &grp->G );
730 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100731 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200732
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200733 if( grp->T != NULL )
734 {
735 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_ecp_point_free( &grp->T[i] );
737 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200738 }
739
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500740 mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100741}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100742
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100743/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200744 * Unallocate (the components of) a key pair
745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200747{
Paul Bakker66d5d072014-06-17 16:39:18 +0200748 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200749 return;
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_ecp_group_free( &key->grp );
752 mbedtls_mpi_free( &key->d );
753 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200754}
755
756/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200757 * Copy the contents of a point
758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200760{
761 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000762 ECP_VALIDATE_RET( P != NULL );
763 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
766 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
767 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200768
769cleanup:
770 return( ret );
771}
772
773/*
774 * Copy the contents of a group object
775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200777{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000778 ECP_VALIDATE_RET( dst != NULL );
779 ECP_VALIDATE_RET( src != NULL );
780
781 return( mbedtls_ecp_group_load( dst, src->id ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200782}
783
784/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100785 * Set point to zero
786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100788{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100789 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000790 ECP_VALIDATE_RET( pt != NULL );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
793 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
794 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100795
796cleanup:
797 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100798}
799
800/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100801 * Tell if a point is zero
802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100804{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000805 ECP_VALIDATE_RET( pt != NULL );
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100808}
809
810/*
Brian J Murrayf343de12018-10-22 16:40:49 -0700811 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200812 */
813int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
814 const mbedtls_ecp_point *Q )
815{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000816 ECP_VALIDATE_RET( P != NULL );
817 ECP_VALIDATE_RET( Q != NULL );
818
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200819 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
820 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
821 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
822 {
823 return( 0 );
824 }
825
826 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
827}
828
829/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100830 * Import a non-zero point from ASCII strings
831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100833 const char *x, const char *y )
834{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100835 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000836 ECP_VALIDATE_RET( P != NULL );
837 ECP_VALIDATE_RET( x != NULL );
838 ECP_VALIDATE_RET( y != NULL );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
841 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
842 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100843
844cleanup:
845 return( ret );
846}
847
848/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100849 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100850 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000851int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp,
852 const mbedtls_ecp_point *P,
853 int format, size_t *olen,
854 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100855{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200856 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100857 size_t plen;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000858 ECP_VALIDATE_RET( grp != NULL );
859 ECP_VALIDATE_RET( P != NULL );
860 ECP_VALIDATE_RET( olen != NULL );
861 ECP_VALIDATE_RET( buf != NULL );
862 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
863 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100864
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100865 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100866 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100869 {
870 if( buflen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100872
873 buf[0] = 0x00;
874 *olen = 1;
875
876 return( 0 );
877 }
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100882 {
883 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100884
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100885 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100887
888 buf[0] = 0x04;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
890 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100891 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100893 {
894 *olen = plen + 1;
895
896 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
900 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100901 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100902
903cleanup:
904 return( ret );
905}
906
907/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100908 * Import a point from unsigned binary data (SEC1 2.3.4)
909 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000910int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
911 mbedtls_ecp_point *pt,
912 const unsigned char *buf, size_t ilen )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100913{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100914 int ret;
915 size_t plen;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000916 ECP_VALIDATE_RET( grp != NULL );
917 ECP_VALIDATE_RET( pt != NULL );
918 ECP_VALIDATE_RET( buf != NULL );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100919
Paul Bakker82788fb2014-10-20 13:59:19 +0200920 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200922
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100923 if( buf[0] == 0x00 )
924 {
925 if( ilen == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 return( mbedtls_ecp_set_zero( pt ) );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100927 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100929 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100932
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100933 if( buf[0] != 0x04 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100935
936 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
940 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
941 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100942
943cleanup:
944 return( ret );
945}
946
947/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100948 * Import a point from a TLS ECPoint record (RFC 4492)
949 * struct {
950 * opaque point <1..2^8-1>;
951 * } ECPoint;
952 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000953int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
954 mbedtls_ecp_point *pt,
955 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100956{
957 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100958 const unsigned char *buf_start;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000959 ECP_VALIDATE_RET( grp != NULL );
960 ECP_VALIDATE_RET( pt != NULL );
961 ECP_VALIDATE_RET( buf != NULL );
962 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100963
964 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200965 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100966 */
967 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100969
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100970 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100971 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100973
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100974 /*
975 * Save buffer start for read_binary and update buf
976 */
977 buf_start = *buf;
978 *buf += data_len;
979
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000980 return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100981}
982
983/*
984 * Export a point as a TLS ECPoint record (RFC 4492)
985 * struct {
986 * opaque point <1..2^8-1>;
987 * } ECPoint;
988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100990 int format, size_t *olen,
991 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100992{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100993 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000994 ECP_VALIDATE_RET( grp != NULL );
995 ECP_VALIDATE_RET( pt != NULL );
996 ECP_VALIDATE_RET( olen != NULL );
997 ECP_VALIDATE_RET( buf != NULL );
998 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
999 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001000
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001001 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001002 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001003 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001004 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001008 olen, buf + 1, blen - 1) ) != 0 )
1009 return( ret );
1010
1011 /*
1012 * write length to the first byte and update total length
1013 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001014 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001015 ++*olen;
1016
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001017 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001018}
1019
1020/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001021 * Set a group from an ECParameters record (RFC 4492)
1022 */
Janos Follath89ac8c92018-10-30 11:24:05 +00001023int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
1024 const unsigned char **buf, size_t len )
1025{
1026 int ret;
1027 mbedtls_ecp_group_id grp_id;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001028 ECP_VALIDATE_RET( grp != NULL );
1029 ECP_VALIDATE_RET( buf != NULL );
1030 ECP_VALIDATE_RET( *buf != NULL );
Janos Follath89ac8c92018-10-30 11:24:05 +00001031
1032 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 )
1033 return( ret );
1034
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001035 return( mbedtls_ecp_group_load( grp, grp_id ) );
Janos Follath89ac8c92018-10-30 11:24:05 +00001036}
1037
1038/*
1039 * Read a group id from an ECParameters record (RFC 4492) and convert it to
1040 * mbedtls_ecp_group_id.
1041 */
1042int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
1043 const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001044{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001045 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 const mbedtls_ecp_curve_info *curve_info;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001047 ECP_VALIDATE_RET( grp != NULL );
1048 ECP_VALIDATE_RET( buf != NULL );
1049 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001050
1051 /*
1052 * We expect at least three bytes (see below)
1053 */
1054 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001056
1057 /*
1058 * First byte is curve_type; only named_curve is handled
1059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
1061 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001062
1063 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001064 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001065 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001066 tls_id = *(*buf)++;
1067 tls_id <<= 8;
1068 tls_id |= *(*buf)++;
1069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
1071 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001072
Janos Follath89ac8c92018-10-30 11:24:05 +00001073 *grp = curve_info->grp_id;
1074
1075 return( 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001076}
1077
1078/*
1079 * Write the ECParameters record corresponding to a group (RFC 4492)
1080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001082 unsigned char *buf, size_t blen )
1083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 const mbedtls_ecp_curve_info *curve_info;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001085 ECP_VALIDATE_RET( grp != NULL );
1086 ECP_VALIDATE_RET( buf != NULL );
1087 ECP_VALIDATE_RET( olen != NULL );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
1090 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001091
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001092 /*
1093 * We are going to write 3 bytes (see below)
1094 */
1095 *olen = 3;
1096 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001098
1099 /*
1100 * First byte is curve_type, always named_curve
1101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001103
1104 /*
1105 * Next two bytes are the namedcurve value
1106 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001107 buf[0] = curve_info->tls_id >> 8;
1108 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001109
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001110 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001111}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001112
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001113/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1115 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001116 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 * 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 +02001118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001120{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001121 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001122
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001123 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001125
1126 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001128 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001131 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001134
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001135 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
1137 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001140 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001142
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001143cleanup:
1144 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001145}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001146
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001147/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001148 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001149 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001150 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 * 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 +01001152 * bring the result back to this range.
1153 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001154 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001155 */
1156
1157/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 * 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 +01001159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001161#define INC_MUL_COUNT mul_count++;
1162#else
1163#define INC_MUL_COUNT
1164#endif
1165
Hanno Beckerd6028a12018-10-15 12:01:35 +01001166#define MOD_MUL( N ) \
1167 do \
1168 { \
1169 MBEDTLS_MPI_CHK( ecp_modp( &(N), grp ) ); \
1170 INC_MUL_COUNT \
1171 } while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001172
1173/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001175 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001176 */
Hanno Beckerd6028a12018-10-15 12:01:35 +01001177#define MOD_SUB( N ) \
1178 while( (N).s < 0 && mbedtls_mpi_cmp_int( &(N), 0 ) != 0 ) \
1179 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001180
1181/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 * 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 +02001183 * We known P, N and the result are positive, so sub_abs is correct, and
1184 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001185 */
Hanno Beckerd6028a12018-10-15 12:01:35 +01001186#define MOD_ADD( N ) \
1187 while( mbedtls_mpi_cmp_mpi( &(N), &grp->P ) >= 0 ) \
1188 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001189
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001190#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001191/*
1192 * For curves in short Weierstrass form, we do all the internal operations in
1193 * Jacobian coordinates.
1194 *
1195 * For multiplication, we'll use a comb method with coutermeasueres against
1196 * SPA, hence timing attacks.
1197 */
1198
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001199/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001200 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001201 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001204{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001205 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001209 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001210
Janos Follathb0697532016-08-18 12:38:46 +01001211#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001212 if( mbedtls_internal_ecp_grp_capable( grp ) )
1213 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +01001214#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001217
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001218 /*
1219 * X = X / Z^2 mod p
1220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
1222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1223 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 +01001224
1225 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001226 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
1229 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 +01001230
1231 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001232 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001235
1236cleanup:
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001239
1240 return( ret );
1241}
1242
1243/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001244 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001245 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001246 * (See for example Cohen's "A Course in Computational Algebraic Number
1247 * Theory", Algorithm 10.3.4.)
1248 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001249 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001250 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001251 *
1252 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001255 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001256{
1257 int ret;
1258 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_mpi *c, u, Zi, ZZi;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001260
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001261 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001262 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001263
Janos Follathb0697532016-08-18 12:38:46 +01001264#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001265 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001266 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +01001267#endif
1268
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001269 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001270 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001271
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001272 for( i = 0; i < T_size; i++ )
1273 mbedtls_mpi_init( &c[i] );
1274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001276
1277 /*
1278 * c[i] = Z_0 * ... * Z_i
1279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001281 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001284 MOD_MUL( c[i] );
1285 }
1286
1287 /*
1288 * u = 1 / (Z_0 * ... * Z_n) mod P
1289 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001290 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001291
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001292 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001293 {
1294 /*
1295 * Zi = 1 / Z_i mod p
1296 * u = 1 / (Z_0 * ... * Z_i) mod P
1297 */
1298 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001300 }
1301 else
1302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
1304 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001305 }
1306
1307 /*
1308 * proceed as in normalize()
1309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1311 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
1312 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
1313 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 +01001314
1315 /*
1316 * Post-precessing: reclaim some memory by shrinking coordinates
1317 * - not storing Z (always 1)
1318 * - shrinking other coordinates, but still keeping the same number of
1319 * limbs as P, as otherwise it will too likely be regrown too fast.
1320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1322 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1323 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001324
1325 if( i == 0 )
1326 break;
1327 }
1328
1329cleanup:
1330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001332 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 mbedtls_mpi_free( &c[i] );
1334 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001335
1336 return( ret );
1337}
1338
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001339/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001340 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1341 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1344 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001345 unsigned char inv )
1346{
1347 int ret;
1348 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001352
1353 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1355 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1356 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001357
1358cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001360
1361 return( ret );
1362}
1363
1364/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001365 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001366 *
Peter Dettmance661b22015-02-07 14:43:51 +07001367 * 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 +01001368 *
Peter Dettmance661b22015-02-07 14:43:51 +07001369 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1370 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1371 *
1372 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1373 *
1374 * Cost: 1D := 3M + 4S (A == 0)
1375 * 4M + 4S (A == -3)
1376 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1379 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001380{
1381 int ret;
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001382 mbedtls_mpi M, S, T, U;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001385 dbl_count++;
1386#endif
1387
Janos Follathb0697532016-08-18 12:38:46 +01001388#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001389 if( mbedtls_internal_ecp_grp_capable( grp ) )
1390 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001391#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001392
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001393 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 +01001394
1395 /* Special case for A = -3 */
1396 if( grp->A.p == NULL )
1397 {
Peter Dettmance661b22015-02-07 14:43:51 +07001398 /* M = 3(X + Z^2)(X - Z^2) */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001399 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1400 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
1401 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
1402 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
1403 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001404 }
1405 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001406 {
Peter Dettmance661b22015-02-07 14:43:51 +07001407 /* M = 3.X^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001408 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
1409 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001410
1411 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001412 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001413 {
1414 /* M += A.Z^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001415 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1416 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
1417 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
1418 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001419 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001420 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001421
Peter Dettmance661b22015-02-07 14:43:51 +07001422 /* S = 4.X.Y^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001423 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
1424 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
1425 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
1426 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001427
Peter Dettmance661b22015-02-07 14:43:51 +07001428 /* U = 8.Y^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
1430 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001431
1432 /* T = M^2 - 2.S */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
1434 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
1435 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
Peter Dettmance661b22015-02-07 14:43:51 +07001436
1437 /* S = M(S - T) - U */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001438 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
1439 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
1440 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
Peter Dettmance661b22015-02-07 14:43:51 +07001441
1442 /* U = 2.Y.Z */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001443 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
1444 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001445
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001446 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1447 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001449
1450cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001451 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 +02001452
1453 return( ret );
1454}
1455
1456/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001457 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001458 *
1459 * The coordinates of Q must be normalized (= affine),
1460 * but those of P don't need to. R is not normalized.
1461 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001462 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001463 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001464 * - at each step, P, Q and R are multiples of the base point, the factor
1465 * being less than its order, so none of them is zero;
1466 * - Q is an odd multiple of the base point, P an even multiple,
1467 * due to the choice of precomputed points in the modified comb method.
1468 * So branches for these cases do not leak secret information.
1469 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001470 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1471 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001472 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1475 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001476{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001477 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001481 add_count++;
1482#endif
1483
Janos Follathb0697532016-08-18 12:38:46 +01001484#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001485 if( mbedtls_internal_ecp_grp_capable( grp ) )
1486 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001487#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001488
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001489 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001490 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
1493 return( mbedtls_ecp_copy( R, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
1496 return( mbedtls_ecp_copy( R, P ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001497
1498 /*
1499 * Make sure Q coordinates are normalized
1500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
1502 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
1505 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1508 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1509 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1510 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1511 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1512 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001513
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001514 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001518 {
1519 ret = ecp_double_jac( grp, R, P );
1520 goto cleanup;
1521 }
1522 else
1523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 ret = mbedtls_ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001525 goto cleanup;
1526 }
1527 }
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1530 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1531 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1532 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1533 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1534 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1535 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1536 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1537 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1538 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1539 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1540 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1543 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1544 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001545
1546cleanup:
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1549 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001550
1551 return( ret );
1552}
1553
1554/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001555 * Randomize jacobian coordinates:
1556 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001557 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001558 *
1559 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001562 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1563{
1564 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 mbedtls_mpi l, ll;
Janos Follathb0697532016-08-18 12:38:46 +01001566 size_t p_size;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001567 int count = 0;
1568
Janos Follathb0697532016-08-18 12:38:46 +01001569#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001570 if( mbedtls_internal_ecp_grp_capable( grp ) )
1571 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001572#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001573
1574 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001576
1577 /* Generate l such that 1 < l < p */
1578 do
1579 {
Ron Eldorca6ff582017-01-12 14:50:50 +02001580 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1583 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001584
1585 if( count++ > 10 )
Jonas6645fd32020-05-08 16:57:18 +09001586 {
1587 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
1588 goto cleanup;
1589 }
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001590 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001592
1593 /* Z = l * Z */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 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 +02001595
1596 /* X = l^2 * X */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1598 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 +02001599
1600 /* Y = l^3 * Y */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1602 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 +02001603
1604cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001606
1607 return( ret );
1608}
1609
1610/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001611 * Check and define parameters used by the comb method (see below for details)
1612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1614#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001615#endif
1616
1617/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001619
1620/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001622
1623/*
1624 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001625 *
1626 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001627 * modified version that provides resistance to SPA by avoiding zero
1628 * digits in the representation as in [3]. We modify the method further by
1629 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001630 * representation uses one more K_i, due to carries, but saves on the size of
1631 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001632 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001633 * Summary of the comb method and its modifications:
1634 *
1635 * - The goal is to compute m*P for some w*d-bit integer m.
1636 *
1637 * - The basic comb method splits m into the w-bit integers
1638 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1639 * index has residue i modulo d, and computes m * P as
1640 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1641 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1642 *
1643 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1644 * .. + 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]] ..,
1645 * thereby successively converting it into a form where all summands
1646 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1647 *
1648 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1649 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1650 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1651 * Performing and iterating this procedure for those x[i] that are even
1652 * (keeping track of carry), we can transform the original sum into one of the form
1653 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1654 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1655 * which is why we are only computing half of it in the first place in
1656 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1657 *
1658 * - For the sake of compactness, only the seven low-order bits of x[i]
1659 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001660 * 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 +02001661 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001662 *
1663 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001664 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001665 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001667 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1668 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001669 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001670static void ecp_comb_recode_core( unsigned char x[], size_t d,
1671 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001672{
1673 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001674 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001675
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001676 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001677
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001678 /* First get the classical comb values (except for x_d = 0) */
1679 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001680 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001682
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001683 /* Now make sure x_1 .. x_d are odd */
1684 c = 0;
1685 for( i = 1; i <= d; i++ )
1686 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001687 /* Add carry and update it */
1688 cc = x[i] & c;
1689 x[i] = x[i] ^ c;
1690 c = cc;
1691
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001692 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001693 adjust = 1 - ( x[i] & 0x01 );
1694 c |= x[i] & ( x[i-1] * adjust );
1695 x[i] = x[i] ^ ( x[i-1] * adjust );
1696 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001697 }
1698}
1699
1700/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001701 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001702 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001703 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001704 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001705 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1706 * 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 +01001707 *
1708 * 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 +02001709 *
1710 * Note: Even comb values (those where P would be omitted from the
1711 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001712 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001713 *
1714 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001715 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001716 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1717 * (3) [add] Computation of all T[i]
1718 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001719 *
1720 * Step 1 can be interrupted but not the others; together with the final
1721 * coordinate normalization they are the largest steps done at once, depending
1722 * on the window size. Here are operation counts for P-256:
1723 *
1724 * step (2) (3) (4)
1725 * w = 5 142 165 208
1726 * w = 4 136 77 160
1727 * w = 3 130 33 136
1728 * w = 2 124 11 124
1729 *
1730 * So if ECC operations are blocking for too long even with a low max_ops
1731 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1732 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1735 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001736 unsigned char w, size_t d,
1737 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001738{
1739 int ret;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001740 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001741 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001742 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001744
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001745#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001746 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001747 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001748 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
1749 goto dbl;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001750 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001751 goto norm_dbl;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001752 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
1753 goto add;
1754 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
1755 goto norm_add;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001756 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001757#else
1758 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001759#endif
1760
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001761#if defined(MBEDTLS_ECP_RESTARTABLE)
1762 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1763 {
1764 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1765
1766 /* initial state for the loop */
1767 rs_ctx->rsm->i = 0;
1768 }
1769
1770dbl:
1771#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001772 /*
1773 * Set T[0] = P and
1774 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001777
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001778#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001779 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
1780 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001781 else
1782#endif
1783 j = 0;
1784
1785 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001786 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001787 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001788
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001789 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001790 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001791
1792 if( j % d == 0 )
1793 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1794
1795 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001796 }
1797
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001798#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001799 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1800 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1801
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001802norm_dbl:
1803#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001804 /*
1805 * Normalize current elements in T. As T has holes,
1806 * use an auxiliary array of pointers to elements in T.
1807 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001808 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001809 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001810 TT[j++] = T + i;
1811
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001812 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001813
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001814 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001815
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001816#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001817 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1818 rs_ctx->rsm->state = ecp_rsm_pre_add;
1819
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001820add:
1821#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001822 /*
1823 * Compute the remaining ones using the minimal number of additions
1824 * Be careful to update T[2^l] only after using it!
1825 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001826 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001827
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001828 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001829 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001830 j = i;
1831 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001833 }
1834
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001835#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001836 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1837 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1838
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001839norm_add:
1840#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001841 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001842 * Normalize final elements in T. Even though there are no holes now, we
1843 * still need the auxiliary array for homogeneity with the previous
1844 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001845 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001846 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001847 TT[j] = T + j + 1;
1848
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001849 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001850
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001851 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001852
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001853cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001854#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001855 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1856 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001857 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001858 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001859 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001860 }
1861#endif
Janos Follathb0697532016-08-18 12:38:46 +01001862
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001863 return( ret );
1864}
1865
1866/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001867 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001868 *
1869 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001872 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001873 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001874{
1875 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001876 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001877
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001878 /* Ignore the "sign" bit and scale down */
1879 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001880
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001881 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001882 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1885 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001886 }
1887
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001888 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001890
1891cleanup:
1892 return( ret );
1893}
1894
1895/*
1896 * Core multiplication algorithm for the (modified) comb method.
1897 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001898 *
1899 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001902 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001903 const unsigned char x[], size_t d,
1904 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001905 void *p_rng,
1906 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001907{
1908 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001910 size_t i;
1911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001913
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001914#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001915 (void) rs_ctx;
1916#endif
1917
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001918#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001919 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1920 rs_ctx->rsm->state != ecp_rsm_comb_core )
1921 {
1922 rs_ctx->rsm->i = 0;
1923 rs_ctx->rsm->state = ecp_rsm_comb_core;
1924 }
1925
1926 /* new 'if' instead of nested for the sake of the 'else' branch */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001927 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001928 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001929 /* restore current index (R already pointing to rs_ctx->rsm->R) */
1930 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001931 }
1932 else
1933#endif
1934 {
1935 /* Start with a non-zero point and randomize its coordinates */
1936 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001937 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001938 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
1939 if( f_rng != 0 )
1940 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
1941 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001942
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001943 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001944 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001945 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001946 --i;
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001949 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001951 }
1952
1953cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001956
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001957#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001958 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1959 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001960 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001961 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001962 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001963 }
1964#endif
1965
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001966 return( ret );
1967}
1968
1969/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001970 * Recode the scalar to get constant-time comb multiplication
1971 *
1972 * As the actual scalar recoding needs an odd scalar as a starting point,
1973 * this wrapper ensures that by replacing m by N - m if necessary, and
1974 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001975 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001976 * This works because we only support large prime order for Short Weierstrass
1977 * curves, so N is always odd hence either m or N - m is.
1978 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001979 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001980 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001981static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
1982 const mbedtls_mpi *m,
1983 unsigned char k[COMB_MAX_D + 1],
1984 size_t d,
1985 unsigned char w,
1986 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001987{
1988 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001989 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001990
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001991 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001992 mbedtls_mpi_init( &mm );
1993
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001994 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001995 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
1996 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
1997
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001998 /* do we need the parity trick? */
1999 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
2000
2001 /* execute parity fix in constant time */
2002 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002003 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002004 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
2005
2006 /* actual scalar recoding */
2007 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002008
2009cleanup:
2010 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002011 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002012
2013 return( ret );
2014}
2015
2016/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002017 * Perform comb multiplication (for short Weierstrass curves)
2018 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002019 *
2020 * Scalar recoding may use a parity trick that makes us compute -m * P,
2021 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002022 */
2023static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
2024 mbedtls_ecp_point *R,
2025 const mbedtls_mpi *m,
2026 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002027 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002028 unsigned char w,
2029 size_t d,
2030 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002031 void *p_rng,
2032 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002033{
2034 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002035 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002036 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002037 mbedtls_ecp_point *RR = R;
2038
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002039#if defined(MBEDTLS_ECP_RESTARTABLE)
2040 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2041 {
2042 RR = &rs_ctx->rsm->R;
2043
2044 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
2045 goto final_norm;
2046 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002047#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002048
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002049 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
2050 &parity_trick ) );
2051 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
2052 f_rng, p_rng, rs_ctx ) );
2053 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
2054
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002055#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002056 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002057 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002058
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002059final_norm:
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002060#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002061 /*
2062 * Knowledge of the jacobian coordinates may leak the last few bits of the
2063 * scalar [1], and since our MPI implementation isn't constant-flow,
2064 * inversion (used for coordinate normalization) may leak the full value
2065 * of its input via side-channels [2].
2066 *
2067 * [1] https://eprint.iacr.org/2003/191
2068 * [2] https://eprint.iacr.org/2020/055
2069 *
2070 * Avoid the leak by randomizing coordinates before we normalize them.
2071 */
2072 if( f_rng != 0 )
2073 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
2074
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002075 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002076 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
2077
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002078#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02002079 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2080 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002081#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002082
2083cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002084 return( ret );
2085}
2086
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002087/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002088 * Pick window size based on curve size and whether we optimize for base point
2089 */
2090static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
2091 unsigned char p_eq_g )
2092{
2093 unsigned char w;
2094
2095 /*
2096 * Minimize the number of multiplications, that is minimize
2097 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2098 * (see costs of the various parts, with 1S = 1M)
2099 */
2100 w = grp->nbits >= 384 ? 5 : 4;
2101
2102 /*
2103 * If P == G, pre-compute a bit more, since this may be re-used later.
2104 * Just adding one avoids upping the cost of the first mul too much,
2105 * and the memory cost too.
2106 */
2107 if( p_eq_g )
2108 w++;
2109
2110 /*
2111 * Make sure w is within bounds.
2112 * (The last test is useful only for very small curves in the test suite.)
2113 */
2114 if( w > MBEDTLS_ECP_WINDOW_SIZE )
2115 w = MBEDTLS_ECP_WINDOW_SIZE;
2116 if( w >= grp->nbits )
2117 w = 2;
2118
2119 return( w );
2120}
2121
2122/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002123 * Multiplication using the comb method - for curves in short Weierstrass form
2124 *
2125 * This function is mainly responsible for administrative work:
2126 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002127 * - managing the table of precomputed points (passed between the below two
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002128 * functions): allocation, computation, ownership tranfer, freeing.
2129 *
2130 * It delegates the actual arithmetic work to:
2131 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2132 *
2133 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2136 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002137 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002138 void *p_rng,
2139 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002140{
2141 int ret;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002142 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002143 size_t d;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002144 unsigned char T_size = 0, T_ok = 0;
2145 mbedtls_ecp_point *T = NULL;
2146#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2147 ecp_drbg_context drbg_ctx;
2148
2149 ecp_drbg_init( &drbg_ctx );
2150#endif
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002151
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002152 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002153
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002154#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2155 if( f_rng == NULL )
2156 {
2157 MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) );
2158 f_rng = &ecp_drbg_random;
2159 p_rng = &drbg_ctx;
2160 }
2161#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2162
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002163 /* Is P the base point ? */
2164#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2165 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
2166 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002167#else
2168 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002169#endif
2170
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002171 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002172 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002173 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002174 d = ( grp->nbits + w - 1 ) / w;
2175
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002176 /* Pre-computed table: do we have it already for the base point? */
2177 if( p_eq_g && grp->T != NULL )
2178 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002179 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002180 T = grp->T;
2181 T_ok = 1;
2182 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002183 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002184#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002185 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002186 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002187 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002188 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002189 T = rs_ctx->rsm->T;
2190 rs_ctx->rsm->T = NULL;
2191 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002192
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002193 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002194 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002195 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002196 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002197#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002198 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002199 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002200 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002201 if( T == NULL )
2202 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002203 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002204 goto cleanup;
2205 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002206
2207 for( i = 0; i < T_size; i++ )
2208 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002209
2210 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002211 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002212
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002213 /* Compute table (or finish computing it) if not done already */
2214 if( !T_ok )
2215 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002216 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002217
2218 if( p_eq_g )
2219 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002220 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002221 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002222 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002223 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002224 }
2225 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002226
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002227 /* Actual comb multiplication using precomputed points */
2228 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002229 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002230 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002231
2232cleanup:
2233
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002234#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2235 ecp_drbg_free( &drbg_ctx );
2236#endif
2237
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002238 /* does T belong to the group? */
2239 if( T == grp->T )
2240 T = NULL;
2241
2242 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002243#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002244 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 +01002245 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002246 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002247 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002248 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002249 T = NULL;
2250 }
2251#endif
2252
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002253 /* did T belong to us? then let's destroy it! */
2254 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002255 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002256 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_ecp_point_free( &T[i] );
2258 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002259 }
2260
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002261 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002262#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002263 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2264#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002265 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01002266 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002268
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002269 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002270
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002271 return( ret );
2272}
2273
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002274#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002275
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002276#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002277/*
2278 * For Montgomery curves, we do all the internal arithmetic in projective
2279 * coordinates. Import/export of points uses only the x coordinates, which is
2280 * internaly represented as X / Z.
2281 *
2282 * For scalar multiplication, we'll use a Montgomery ladder.
2283 */
2284
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002285/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002286 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2287 * Cost: 1M + 1I
2288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002290{
2291 int ret;
2292
Janos Follathb0697532016-08-18 12:38:46 +01002293#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002294 if( mbedtls_internal_ecp_grp_capable( grp ) )
2295 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002296#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
2299 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
2300 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002301
2302cleanup:
2303 return( ret );
2304}
2305
2306/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002307 * Randomize projective x/z coordinates:
2308 * (X, Z) -> (l X, l Z) for random l
2309 * This is sort of the reverse operation of ecp_normalize_mxz().
2310 *
2311 * This countermeasure was first suggested in [2].
2312 * Cost: 2M
2313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002315 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2316{
2317 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 mbedtls_mpi l;
Janos Follathb0697532016-08-18 12:38:46 +01002319 size_t p_size;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002320 int count = 0;
2321
Janos Follathb0697532016-08-18 12:38:46 +01002322#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002323 if( mbedtls_internal_ecp_grp_capable( grp ) )
2324 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Janos Follath372697b2016-10-28 16:53:11 +01002325#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002326
2327 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002329
2330 /* Generate l such that 1 < l < p */
2331 do
2332 {
Ron Eldorca6ff582017-01-12 14:50:50 +02002333 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
2336 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002337
2338 if( count++ > 10 )
Jonas6645fd32020-05-08 16:57:18 +09002339 {
2340 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
2341 goto cleanup;
2342 }
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
2347 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 +01002348
2349cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002351
2352 return( ret );
2353}
2354
2355/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002356 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2357 * for Montgomery curves in x/z coordinates.
2358 *
2359 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2360 * with
2361 * d = X1
2362 * P = (X2, Z2)
2363 * Q = (X3, Z3)
2364 * R = (X4, Z4)
2365 * S = (X5, Z5)
2366 * and eliminating temporary variables tO, ..., t4.
2367 *
2368 * Cost: 5M + 4S
2369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2371 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2372 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2373 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002374{
2375 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002377
Janos Follathb0697532016-08-18 12:38:46 +01002378#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002379 if( mbedtls_internal_ecp_grp_capable( grp ) )
2380 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002381#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2384 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2385 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
2388 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
2389 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
2390 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
2391 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
2392 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
2393 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
2394 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
2395 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
2396 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
2397 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
2398 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
2399 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
2400 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
2401 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
2402 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
2403 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
2404 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 +01002405
2406cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2408 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2409 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002410
2411 return( ret );
2412}
2413
2414/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002415 * Multiplication with Montgomery ladder in x/z coordinates,
2416 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2419 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002420 int (*f_rng)(void *, unsigned char *, size_t),
2421 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002422{
2423 int ret;
2424 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002425 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 mbedtls_ecp_point RP;
2427 mbedtls_mpi PX;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002428#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2429 ecp_drbg_context drbg_ctx;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002430
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002431 ecp_drbg_init( &drbg_ctx );
2432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002434
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002435#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2436 if( f_rng == NULL )
2437 {
2438 MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) );
2439 f_rng = &ecp_drbg_random;
2440 p_rng = &drbg_ctx;
2441 }
2442#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2443
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002444 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2446 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002447
2448 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2450 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2451 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002452
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002453 /* RP.X might be sligtly larger than P, so reduce it */
2454 MOD_ADD( RP.X );
2455
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002456 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002457 if( f_rng != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002459
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002460 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002461 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002462 while( i-- > 0 )
2463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002465 /*
2466 * if (b) R = 2R + P else R = 2R,
2467 * which is:
2468 * if (b) double_add( RP, R, RP, R )
2469 * else double_add( R, RP, R, RP )
2470 * but using safe conditional swaps to avoid leaks
2471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2473 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2474 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2475 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002477 }
2478
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002479 /*
2480 * Knowledge of the projective coordinates may leak the last few bits of the
2481 * scalar [1], and since our MPI implementation isn't constant-flow,
2482 * inversion (used for coordinate normalization) may leak the full value
2483 * of its input via side-channels [2].
2484 *
2485 * [1] https://eprint.iacr.org/2003/191
2486 * [2] https://eprint.iacr.org/2020/055
2487 *
2488 * Avoid the leak by randomizing coordinates before we normalize them.
2489 */
2490 if( f_rng != NULL )
2491 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
2492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002494
2495cleanup:
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002496#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2497 ecp_drbg_free( &drbg_ctx );
2498#endif
2499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002501
2502 return( ret );
2503}
2504
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002505#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002506
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002507/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002508 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002509 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002510int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002512 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2513 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002514{
Janos Follathb0697532016-08-18 12:38:46 +01002515 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002516#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2517 char is_grp_capable = 0;
2518#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002519 ECP_VALIDATE_RET( grp != NULL );
2520 ECP_VALIDATE_RET( R != NULL );
2521 ECP_VALIDATE_RET( m != NULL );
2522 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002523
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002524#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002525 /* reset ops count for this call if top-level */
2526 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2527 rs_ctx->ops_done = 0;
2528#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002529
Janos Follathc44ab972016-11-18 16:38:23 +00002530#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002531 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002532 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002533#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002534
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002535#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002536 /* skip argument check when restarting */
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002537 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002538#endif
2539 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002540 /* check_privkey is free */
2541 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2542
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002543 /* Common sanity checks */
2544 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2545 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002546 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002547
2548 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002549#if defined(ECP_MONTGOMERY)
2550 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002551 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002552#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002553#if defined(ECP_SHORTWEIERSTRASS)
2554 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002555 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002556#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002557
Janos Follath6c8ccd52016-11-29 15:37:09 +00002558cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002559
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002560#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002561 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002562 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002563#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002564
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002565#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002566 if( rs_ctx != NULL )
2567 rs_ctx->depth--;
2568#endif
2569
Janos Follathb0697532016-08-18 12:38:46 +01002570 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002571}
2572
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002573/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002574 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002575 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002576int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002577 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002578 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002579{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002580 ECP_VALIDATE_RET( grp != NULL );
2581 ECP_VALIDATE_RET( R != NULL );
2582 ECP_VALIDATE_RET( m != NULL );
2583 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002584 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002585}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002586
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002587#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002588/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002589 * Check that an affine point is valid as a public key,
2590 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002593{
2594 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002596
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002597 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2599 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2600 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2601 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2602 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002605
2606 /*
2607 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002608 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
2611 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002612
2613 /* Special case for A = -3 */
2614 if( grp->A.p == NULL )
2615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002617 }
2618 else
2619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002621 }
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
2624 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2627 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002628
2629cleanup:
2630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002632
2633 return( ret );
2634}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002635#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002636
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002637/*
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002638 * R = m * P with shortcuts for m == 1 and m == -1
2639 * NOT constant-time - ONLY for short Weierstrass!
2640 */
2641static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2642 mbedtls_ecp_point *R,
2643 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002644 const mbedtls_ecp_point *P,
2645 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002646{
2647 int ret;
2648
2649 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
2650 {
2651 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2652 }
2653 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2654 {
2655 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2656 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2657 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2658 }
2659 else
2660 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002661 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2662 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002663 }
2664
2665cleanup:
2666 return( ret );
2667}
2668
2669/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002670 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002671 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002672 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002673int mbedtls_ecp_muladd_restartable(
2674 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002675 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002676 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2677 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002678{
2679 int ret;
2680 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002681 mbedtls_ecp_point *pmP = &mP;
2682 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002683#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2684 char is_grp_capable = 0;
2685#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002686 ECP_VALIDATE_RET( grp != NULL );
2687 ECP_VALIDATE_RET( R != NULL );
2688 ECP_VALIDATE_RET( m != NULL );
2689 ECP_VALIDATE_RET( P != NULL );
2690 ECP_VALIDATE_RET( n != NULL );
2691 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002692
2693 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
2694 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2695
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002696 mbedtls_ecp_point_init( &mP );
2697
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002698 ECP_RS_ENTER( ma );
2699
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002700#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002701 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2702 {
2703 /* redirect intermediate results to restart context */
2704 pmP = &rs_ctx->ma->mP;
2705 pR = &rs_ctx->ma->R;
2706
2707 /* jump to next operation */
2708 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2709 goto mul2;
2710 if( rs_ctx->ma->state == ecp_rsma_add )
2711 goto add;
2712 if( rs_ctx->ma->state == ecp_rsma_norm )
2713 goto norm;
2714 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002715#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002716
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002717 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002718#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002719 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002720 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002721
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002722mul2:
2723#endif
2724 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Janos Follathaf6f2692018-12-07 09:55:24 +00002725
2726#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2727 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2728 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2729#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2730
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002731#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002732 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002733 rs_ctx->ma->state = ecp_rsma_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002734
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002735add:
2736#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002737 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002738 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002739#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002740 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002741 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002742
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002743norm:
2744#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002745 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002746 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2747
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002748#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002749 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2750 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2751#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002752
2753cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002754#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002755 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002756 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002757#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002758
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002759 mbedtls_ecp_point_free( &mP );
2760
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002761 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002762
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002763 return( ret );
2764}
2765
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002766/*
2767 * Linear combination
2768 * NOT constant-time
2769 */
2770int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2771 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2772 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2773{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002774 ECP_VALIDATE_RET( grp != NULL );
2775 ECP_VALIDATE_RET( R != NULL );
2776 ECP_VALIDATE_RET( m != NULL );
2777 ECP_VALIDATE_RET( P != NULL );
2778 ECP_VALIDATE_RET( n != NULL );
2779 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002780 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2781}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002782
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002783#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002784/*
2785 * Check validity of a public key for Montgomery curves with x-only schemes
2786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002788{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002789 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002790 /* Allow any public value, if it's too big then we'll just reduce it mod p
2791 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2793 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002794
2795 return( 0 );
2796}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002797#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002798
2799/*
2800 * Check that a point is valid as a public key
2801 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002802int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
2803 const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002804{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002805 ECP_VALIDATE_RET( grp != NULL );
2806 ECP_VALIDATE_RET( pt != NULL );
2807
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002808 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2810 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002811
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002812#if defined(ECP_MONTGOMERY)
2813 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002814 return( ecp_check_pubkey_mx( grp, pt ) );
2815#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002816#if defined(ECP_SHORTWEIERSTRASS)
2817 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002818 return( ecp_check_pubkey_sw( grp, pt ) );
2819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002821}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002822
2823/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002825 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002826int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
2827 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002828{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002829 ECP_VALIDATE_RET( grp != NULL );
2830 ECP_VALIDATE_RET( d != NULL );
2831
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002832#if defined(ECP_MONTGOMERY)
2833 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002834 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002835 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2837 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002838 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002840
2841 /* see [Curve25519] page 5 */
2842 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
2843 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2844
2845 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002846 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002847#endif /* ECP_MONTGOMERY */
2848#if defined(ECP_SHORTWEIERSTRASS)
2849 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002850 {
2851 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2853 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2854 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002855 else
2856 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002857 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002858#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002861}
2862
2863/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002864 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002865 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002866int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
2867 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002868 int (*f_rng)(void *, unsigned char *, size_t),
2869 void *p_rng )
2870{
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002871 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002872 size_t n_size;
2873
2874 ECP_VALIDATE_RET( grp != NULL );
2875 ECP_VALIDATE_RET( d != NULL );
2876 ECP_VALIDATE_RET( f_rng != NULL );
2877
2878 n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002879
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002880#if defined(ECP_MONTGOMERY)
2881 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002882 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002883 /* [M225] page 5 */
2884 size_t b;
2885
Janos Follath98e28a72016-05-31 14:03:54 +01002886 do {
2887 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
2888 } while( mbedtls_mpi_bitlen( d ) == 0);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002889
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002890 /* Make sure the most significant bit is nbits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002891 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002892 if( b > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002896
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002897 /* Make sure the last two bits are unset for Curve448, three bits for
2898 Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
2900 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002901 if( grp->nbits == 254 )
2902 {
2903 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
2904 }
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002905 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002906#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002907
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002908#if defined(ECP_SHORTWEIERSTRASS)
2909 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002910 {
2911 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002912 int count = 0;
Janos Follath867a3ab2019-10-11 14:21:53 +01002913 unsigned cmp = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01002914
2915 /*
2916 * Match the procedure given in RFC 6979 (deterministic ECDSA):
2917 * - use the same byte ordering;
2918 * - keep the leftmost nbits bits of the generated octet string;
2919 * - try until result is in the desired range.
2920 * This also avoids any biais, which is especially important for ECDSA.
2921 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002922 do
2923 {
Hanno Becker7c8cb9c2017-10-17 15:19:38 +01002924 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002926
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01002927 /*
2928 * Each try has at worst a probability 1/2 of failing (the msb has
2929 * a probability 1/2 of being 0, and then the result will be < N),
2930 * so after 30 tries failure probability is a most 2**(-30).
2931 *
2932 * For most curves, 1 try is enough with overwhelming probability,
2933 * since N starts with a lot of 1s in binary, but some curves
2934 * such as secp224k1 are actually very close to the worst case.
2935 */
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02002936 if( ++count > 30 )
2937 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Janos Follath4c3408b2019-09-16 14:27:39 +01002938
Janos Follath867a3ab2019-10-11 14:21:53 +01002939 ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
Janos Follath4c3408b2019-09-16 14:27:39 +01002940 if( ret != 0 )
2941 {
2942 goto cleanup;
2943 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002944 }
Janos Follath867a3ab2019-10-11 14:21:53 +01002945 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002946 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002947#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002948
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002949cleanup:
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002950 return( ret );
2951}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002952
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002953/*
2954 * Generate a keypair with configurable base point
2955 */
2956int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
2957 const mbedtls_ecp_point *G,
2958 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2959 int (*f_rng)(void *, unsigned char *, size_t),
2960 void *p_rng )
2961{
2962 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002963 ECP_VALIDATE_RET( grp != NULL );
2964 ECP_VALIDATE_RET( d != NULL );
2965 ECP_VALIDATE_RET( G != NULL );
2966 ECP_VALIDATE_RET( Q != NULL );
2967 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002968
2969 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
2970 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
2971
2972cleanup:
2973 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02002974}
2975
2976/*
2977 * Generate key pair, wrapper for conventional base point
2978 */
2979int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
2980 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2981 int (*f_rng)(void *, unsigned char *, size_t),
2982 void *p_rng )
2983{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002984 ECP_VALIDATE_RET( grp != NULL );
2985 ECP_VALIDATE_RET( d != NULL );
2986 ECP_VALIDATE_RET( Q != NULL );
2987 ECP_VALIDATE_RET( f_rng != NULL );
2988
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02002989 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002990}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01002991
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002992/*
2993 * Generate a keypair, prettier wrapper
2994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002996 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2997{
2998 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002999 ECP_VALIDATE_RET( key != NULL );
3000 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003001
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003002 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003003 return( ret );
3004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003006}
3007
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003008/*
3009 * Check a public-private key pair
3010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003012{
3013 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 mbedtls_ecp_point Q;
3015 mbedtls_ecp_group grp;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003016 ECP_VALIDATE_RET( pub != NULL );
3017 ECP_VALIDATE_RET( prv != NULL );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003020 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
3022 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
3023 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003026 }
3027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 mbedtls_ecp_point_init( &Q );
3029 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 /* mbedtls_ecp_mul() needs a non-const group... */
3032 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003033
3034 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
3038 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
3039 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003042 goto cleanup;
3043 }
3044
3045cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 mbedtls_ecp_point_free( &Q );
3047 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003048
3049 return( ret );
3050}
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003053
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003054/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003055 * Checkup routine
3056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003058{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003059 int ret;
3060 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 mbedtls_ecp_group grp;
3062 mbedtls_ecp_point R, P;
3063 mbedtls_mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003064 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003065 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003066 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003067 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003068 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003069 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003070 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003071 "400000000000000000000000000000000000000000000000", /* one and zeros */
3072 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3073 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003074 };
3075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 mbedtls_ecp_group_init( &grp );
3077 mbedtls_ecp_point_init( &R );
3078 mbedtls_ecp_point_init( &P );
3079 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003080
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003081 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003083 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003084#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003085 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003086#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003087
3088 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003090
3091 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
3093 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003094
3095 add_count = 0;
3096 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003097 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3099 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003100
3101 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3102 {
3103 add_c_prev = add_count;
3104 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003105 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003106 add_count = 0;
3107 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003108 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3111 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003112
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003113 if( add_count != add_c_prev ||
3114 dbl_count != dbl_c_prev ||
3115 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003116 {
3117 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003119
3120 ret = 1;
3121 goto cleanup;
3122 }
3123 }
3124
3125 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003127
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003128 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003130 /* We computed P = 2G last time, use it */
3131
3132 add_count = 0;
3133 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003134 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3136 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003137
3138 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3139 {
3140 add_c_prev = add_count;
3141 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003142 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003143 add_count = 0;
3144 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003145 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3148 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003149
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003150 if( add_count != add_c_prev ||
3151 dbl_count != dbl_c_prev ||
3152 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003153 {
3154 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003156
3157 ret = 1;
3158 goto cleanup;
3159 }
3160 }
3161
3162 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003164
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003165cleanup:
3166
3167 if( ret < 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 mbedtls_ecp_group_free( &grp );
3171 mbedtls_ecp_point_free( &R );
3172 mbedtls_ecp_point_free( &P );
3173 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003174
3175 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003177
3178 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003179}
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003182
Janos Follathb0697532016-08-18 12:38:46 +01003183#endif /* !MBEDTLS_ECP_ALT */
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#endif /* MBEDTLS_ECP_C */