blob: 8e87e60c3f192818402514a21562fa92f24d9eb4 [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é-Gonnard047986c2020-06-04 09:43:14 +0200284#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
285 ecp_drbg_context drbg_ctx;
286 unsigned char drbg_seeded;
287#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100288};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100289
290/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200291 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100292 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200293static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100294{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200295 mbedtls_ecp_point_init( &ctx->R );
296 ctx->i = 0;
297 ctx->T = NULL;
298 ctx->T_size = 0;
299 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +0200300#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
301 ecp_drbg_init( &ctx->drbg_ctx );
302 ctx->drbg_seeded = 0;
303#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100304}
305
306/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200307 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100308 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200309static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100310{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100311 unsigned char i;
312
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100313 if( ctx == NULL )
314 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100315
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100316 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100317
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200318 if( ctx->T != NULL )
319 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100320 for( i = 0; i < ctx->T_size; i++ )
321 mbedtls_ecp_point_free( ctx->T + i );
322 mbedtls_free( ctx->T );
323 }
324
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +0200325#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
326 ecp_drbg_free( &ctx->drbg_ctx );
327#endif
328
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200329 ecp_restart_rsm_init( ctx );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100330}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100331
332/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200333 * Restart context for ecp_muladd()
334 */
335struct mbedtls_ecp_restart_muladd
336{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200337 mbedtls_ecp_point mP; /* mP value */
338 mbedtls_ecp_point R; /* R intermediate result */
339 enum { /* what should we do next? */
340 ecp_rsma_mul1 = 0, /* first multiplication */
341 ecp_rsma_mul2, /* second multiplication */
342 ecp_rsma_add, /* addition */
343 ecp_rsma_norm, /* normalization */
344 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200345};
346
347/*
348 * Init restart_muladd sub-context
349 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200350static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200351{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200352 mbedtls_ecp_point_init( &ctx->mP );
353 mbedtls_ecp_point_init( &ctx->R );
354 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200355}
356
357/*
358 * Free the components of a restart_muladd sub-context
359 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200360static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200361{
362 if( ctx == NULL )
363 return;
364
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200365 mbedtls_ecp_point_free( &ctx->mP );
366 mbedtls_ecp_point_free( &ctx->R );
367
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200368 ecp_restart_ma_init( ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200369}
370
371/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200372 * Initialize a restart context
373 */
374void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
375{
Hanno Becker80f71682018-12-18 23:44:43 +0000376 ECP_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200377 ctx->ops_done = 0;
378 ctx->depth = 0;
379 ctx->rsm = NULL;
380 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200381}
382
383/*
384 * Free the components of a restart context
385 */
386void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
387{
388 if( ctx == NULL )
389 return;
390
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200391 ecp_restart_rsm_free( ctx->rsm );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200392 mbedtls_free( ctx->rsm );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200393
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200394 ecp_restart_ma_free( ctx->ma );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200395 mbedtls_free( ctx->ma );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200396
397 mbedtls_ecp_restart_init( ctx );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200398}
399
400/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100401 * Check if we can do the next step
402 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200403int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
404 mbedtls_ecp_restart_ctx *rs_ctx,
405 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100406{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000407 ECP_VALIDATE_RET( grp != NULL );
408
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200409 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100410 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100411 /* scale depending on curve size: the chosen reference is 256-bit,
412 * and multiplication is quadratic. Round to the closest integer. */
413 if( grp->pbits >= 512 )
414 ops *= 4;
415 else if( grp->pbits >= 384 )
416 ops *= 2;
417
Hanno Beckerb10c6602018-10-26 13:50:13 +0100418 /* Avoid infinite loops: always allow first step.
419 * Because of that, however, it's not generally true
420 * that ops_done <= ecp_max_ops, so the check
421 * ops_done > ecp_max_ops below is mandatory. */
422 if( ( rs_ctx->ops_done != 0 ) &&
423 ( rs_ctx->ops_done > ecp_max_ops ||
424 ops > ecp_max_ops - rs_ctx->ops_done ) )
425 {
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100426 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
Hanno Beckerb10c6602018-10-26 13:50:13 +0100427 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100428
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100429 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200430 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100431 }
432
433 return( 0 );
434}
435
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200436/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200437#define ECP_RS_ENTER( SUB ) do { \
438 /* reset ops count for this call if top-level */ \
439 if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
440 rs_ctx->ops_done = 0; \
441 \
442 /* set up our own sub-context if needed */ \
443 if( mbedtls_ecp_restart_is_enabled() && \
444 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
445 { \
446 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
447 if( rs_ctx->SUB == NULL ) \
448 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
449 \
450 ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
451 } \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200452} while( 0 )
453
454/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200455#define ECP_RS_LEAVE( SUB ) do { \
456 /* clear our sub-context when not in progress (done or error) */ \
457 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
458 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
459 { \
460 ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
461 mbedtls_free( rs_ctx->SUB ); \
462 rs_ctx->SUB = NULL; \
463 } \
464 \
465 if( rs_ctx != NULL ) \
466 rs_ctx->depth--; \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200467} while( 0 )
468
469#else /* MBEDTLS_ECP_RESTARTABLE */
470
471#define ECP_RS_ENTER( sub ) (void) rs_ctx;
472#define ECP_RS_LEAVE( sub ) (void) rs_ctx;
473
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200474#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
477 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
478 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
479 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
480 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
481 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
482 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
483 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
484 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
485 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
486 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200487#define ECP_SHORTWEIERSTRASS
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100488#endif
489
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000490#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
491 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200492#define ECP_MONTGOMERY
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100493#endif
494
495/*
496 * Curve types: internal for now, might be exposed later
497 */
498typedef enum
499{
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200500 ECP_TYPE_NONE = 0,
501 ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
502 ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100503} ecp_curve_type;
504
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100505/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200506 * List of supported curves:
507 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200508 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200509 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200510 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100511 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100512 * Curves are listed in order: largest curves first, and for a given size,
513 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200514 *
515 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
520 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
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_BP512R1_ENABLED)
523 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
526 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
529 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
532 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
535 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
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_BP256R1_ENABLED)
538 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
541 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
544 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
547 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100548#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
550 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100551#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200553};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100554
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200555#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
556 sizeof( ecp_supported_curves[0] )
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200559
560/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200561 * List of supported curves and associated info
562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200564{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200565 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200566}
567
568/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100569 * List of supported curves, group ID only
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100572{
573 static int init_done = 0;
574
575 if( ! init_done )
576 {
577 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 for( curve_info = mbedtls_ecp_curve_list();
581 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100582 curve_info++ )
583 {
584 ecp_supported_grp_id[i++] = curve_info->grp_id;
585 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100587
588 init_done = 1;
589 }
590
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200591 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100592}
593
594/*
595 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597const 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 +0200598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 for( curve_info = mbedtls_ecp_curve_list();
602 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200603 curve_info++ )
604 {
605 if( curve_info->grp_id == grp_id )
606 return( curve_info );
607 }
608
609 return( NULL );
610}
611
612/*
613 * Get the curve info from the TLS identifier
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200616{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 for( curve_info = mbedtls_ecp_curve_list();
620 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200621 curve_info++ )
622 {
623 if( curve_info->tls_id == tls_id )
624 return( curve_info );
625 }
626
627 return( NULL );
628}
629
630/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100631 * Get the curve info from the name
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100636
Hanno Beckerb7a04a72018-12-18 23:50:21 +0000637 if( name == NULL )
638 return( NULL );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 for( curve_info = mbedtls_ecp_curve_list();
641 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100642 curve_info++ )
643 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200644 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100645 return( curve_info );
646 }
647
648 return( NULL );
649}
650
651/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100652 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100655{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100656 if( grp->G.X.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200657 return( ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100658
659 if( grp->G.Y.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200660 return( ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100661 else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200662 return( ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100663}
664
665/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100666 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100669{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000670 ECP_VALIDATE( pt != NULL );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_mpi_init( &pt->X );
673 mbedtls_mpi_init( &pt->Y );
674 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100675}
676
677/*
678 * Initialize (the components of) a group
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100681{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000682 ECP_VALIDATE( grp != NULL );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100683
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200684 grp->id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200685 mbedtls_mpi_init( &grp->P );
686 mbedtls_mpi_init( &grp->A );
687 mbedtls_mpi_init( &grp->B );
688 mbedtls_ecp_point_init( &grp->G );
689 mbedtls_mpi_init( &grp->N );
690 grp->pbits = 0;
691 grp->nbits = 0;
692 grp->h = 0;
693 grp->modp = NULL;
694 grp->t_pre = NULL;
695 grp->t_post = NULL;
696 grp->t_data = NULL;
697 grp->T = NULL;
698 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100699}
700
701/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200702 * Initialize (the components of) a key pair
703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200705{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000706 ECP_VALIDATE( key != NULL );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_ecp_group_init( &key->grp );
709 mbedtls_mpi_init( &key->d );
710 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200711}
712
713/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100714 * Unallocate (the components of) a point
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100717{
718 if( pt == NULL )
719 return;
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_mpi_free( &( pt->X ) );
722 mbedtls_mpi_free( &( pt->Y ) );
723 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100724}
725
726/*
727 * Unallocate (the components of) a group
728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100730{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200731 size_t i;
732
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100733 if( grp == NULL )
734 return;
735
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100736 if( grp->h != 1 )
737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_mpi_free( &grp->P );
739 mbedtls_mpi_free( &grp->A );
740 mbedtls_mpi_free( &grp->B );
741 mbedtls_ecp_point_free( &grp->G );
742 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100743 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200744
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200745 if( grp->T != NULL )
746 {
747 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_ecp_point_free( &grp->T[i] );
749 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200750 }
751
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500752 mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100753}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100754
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100755/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200756 * Unallocate (the components of) a key pair
757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200759{
Paul Bakker66d5d072014-06-17 16:39:18 +0200760 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200761 return;
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_ecp_group_free( &key->grp );
764 mbedtls_mpi_free( &key->d );
765 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200766}
767
768/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200769 * Copy the contents of a point
770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200772{
773 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000774 ECP_VALIDATE_RET( P != NULL );
775 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
778 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
779 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200780
781cleanup:
782 return( ret );
783}
784
785/*
786 * Copy the contents of a group object
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200789{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000790 ECP_VALIDATE_RET( dst != NULL );
791 ECP_VALIDATE_RET( src != NULL );
792
793 return( mbedtls_ecp_group_load( dst, src->id ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200794}
795
796/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100797 * Set point to zero
798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100800{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100801 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000802 ECP_VALIDATE_RET( pt != NULL );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
805 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
806 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100807
808cleanup:
809 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100810}
811
812/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100813 * Tell if a point is zero
814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100816{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000817 ECP_VALIDATE_RET( pt != NULL );
818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100820}
821
822/*
Brian J Murrayf343de12018-10-22 16:40:49 -0700823 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200824 */
825int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
826 const mbedtls_ecp_point *Q )
827{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000828 ECP_VALIDATE_RET( P != NULL );
829 ECP_VALIDATE_RET( Q != NULL );
830
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200831 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
832 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
833 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
834 {
835 return( 0 );
836 }
837
838 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
839}
840
841/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100842 * Import a non-zero point from ASCII strings
843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100845 const char *x, const char *y )
846{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100847 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000848 ECP_VALIDATE_RET( P != NULL );
849 ECP_VALIDATE_RET( x != NULL );
850 ECP_VALIDATE_RET( y != NULL );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
853 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
854 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100855
856cleanup:
857 return( ret );
858}
859
860/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100861 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100862 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000863int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp,
864 const mbedtls_ecp_point *P,
865 int format, size_t *olen,
866 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100867{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200868 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100869 size_t plen;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000870 ECP_VALIDATE_RET( grp != NULL );
871 ECP_VALIDATE_RET( P != NULL );
872 ECP_VALIDATE_RET( olen != NULL );
873 ECP_VALIDATE_RET( buf != NULL );
874 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
875 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100876
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100877 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100878 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100881 {
882 if( buflen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100884
885 buf[0] = 0x00;
886 *olen = 1;
887
888 return( 0 );
889 }
890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100894 {
895 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100896
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100897 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100899
900 buf[0] = 0x04;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
902 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100903 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100905 {
906 *olen = plen + 1;
907
908 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
912 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100913 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100914
915cleanup:
916 return( ret );
917}
918
919/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100920 * Import a point from unsigned binary data (SEC1 2.3.4)
921 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000922int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
923 mbedtls_ecp_point *pt,
924 const unsigned char *buf, size_t ilen )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100925{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100926 int ret;
927 size_t plen;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000928 ECP_VALIDATE_RET( grp != NULL );
929 ECP_VALIDATE_RET( pt != NULL );
930 ECP_VALIDATE_RET( buf != NULL );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100931
Paul Bakker82788fb2014-10-20 13:59:19 +0200932 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200934
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100935 if( buf[0] == 0x00 )
936 {
937 if( ilen == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 return( mbedtls_ecp_set_zero( pt ) );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100939 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100941 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100944
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100945 if( buf[0] != 0x04 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100947
948 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
952 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
953 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100954
955cleanup:
956 return( ret );
957}
958
959/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100960 * Import a point from a TLS ECPoint record (RFC 4492)
961 * struct {
962 * opaque point <1..2^8-1>;
963 * } ECPoint;
964 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000965int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
966 mbedtls_ecp_point *pt,
967 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100968{
969 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100970 const unsigned char *buf_start;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000971 ECP_VALIDATE_RET( grp != NULL );
972 ECP_VALIDATE_RET( pt != NULL );
973 ECP_VALIDATE_RET( buf != NULL );
974 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100975
976 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200977 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100978 */
979 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100981
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100982 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100983 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100985
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100986 /*
987 * Save buffer start for read_binary and update buf
988 */
989 buf_start = *buf;
990 *buf += data_len;
991
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000992 return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100993}
994
995/*
996 * Export a point as a TLS ECPoint record (RFC 4492)
997 * struct {
998 * opaque point <1..2^8-1>;
999 * } ECPoint;
1000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001002 int format, size_t *olen,
1003 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001004{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001005 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001006 ECP_VALIDATE_RET( grp != NULL );
1007 ECP_VALIDATE_RET( pt != NULL );
1008 ECP_VALIDATE_RET( olen != NULL );
1009 ECP_VALIDATE_RET( buf != NULL );
1010 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1011 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001012
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001013 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001014 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001015 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001016 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001020 olen, buf + 1, blen - 1) ) != 0 )
1021 return( ret );
1022
1023 /*
1024 * write length to the first byte and update total length
1025 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001026 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001027 ++*olen;
1028
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001029 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001030}
1031
1032/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001033 * Set a group from an ECParameters record (RFC 4492)
1034 */
Janos Follath89ac8c92018-10-30 11:24:05 +00001035int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
1036 const unsigned char **buf, size_t len )
1037{
1038 int ret;
1039 mbedtls_ecp_group_id grp_id;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001040 ECP_VALIDATE_RET( grp != NULL );
1041 ECP_VALIDATE_RET( buf != NULL );
1042 ECP_VALIDATE_RET( *buf != NULL );
Janos Follath89ac8c92018-10-30 11:24:05 +00001043
1044 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 )
1045 return( ret );
1046
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001047 return( mbedtls_ecp_group_load( grp, grp_id ) );
Janos Follath89ac8c92018-10-30 11:24:05 +00001048}
1049
1050/*
1051 * Read a group id from an ECParameters record (RFC 4492) and convert it to
1052 * mbedtls_ecp_group_id.
1053 */
1054int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
1055 const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001056{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001057 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 const mbedtls_ecp_curve_info *curve_info;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001059 ECP_VALIDATE_RET( grp != NULL );
1060 ECP_VALIDATE_RET( buf != NULL );
1061 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001062
1063 /*
1064 * We expect at least three bytes (see below)
1065 */
1066 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001068
1069 /*
1070 * First byte is curve_type; only named_curve is handled
1071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
1073 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001074
1075 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001076 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001077 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001078 tls_id = *(*buf)++;
1079 tls_id <<= 8;
1080 tls_id |= *(*buf)++;
1081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
1083 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001084
Janos Follath89ac8c92018-10-30 11:24:05 +00001085 *grp = curve_info->grp_id;
1086
1087 return( 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001088}
1089
1090/*
1091 * Write the ECParameters record corresponding to a group (RFC 4492)
1092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001094 unsigned char *buf, size_t blen )
1095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 const mbedtls_ecp_curve_info *curve_info;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001097 ECP_VALIDATE_RET( grp != NULL );
1098 ECP_VALIDATE_RET( buf != NULL );
1099 ECP_VALIDATE_RET( olen != NULL );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
1102 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001103
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001104 /*
1105 * We are going to write 3 bytes (see below)
1106 */
1107 *olen = 3;
1108 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001110
1111 /*
1112 * First byte is curve_type, always named_curve
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001115
1116 /*
1117 * Next two bytes are the namedcurve value
1118 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001119 buf[0] = curve_info->tls_id >> 8;
1120 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001121
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001122 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001123}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001124
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001125/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1127 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001128 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 * 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 +02001130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001132{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001133 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001134
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001135 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001137
1138 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001140 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001143 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001146
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001147 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
1149 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001152 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001154
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001155cleanup:
1156 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001157}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001158
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001159/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001160 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001161 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001162 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 * 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 +01001164 * bring the result back to this range.
1165 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001166 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001167 */
1168
1169/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 * 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 +01001171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001173#define INC_MUL_COUNT mul_count++;
1174#else
1175#define INC_MUL_COUNT
1176#endif
1177
Hanno Beckerd6028a12018-10-15 12:01:35 +01001178#define MOD_MUL( N ) \
1179 do \
1180 { \
1181 MBEDTLS_MPI_CHK( ecp_modp( &(N), grp ) ); \
1182 INC_MUL_COUNT \
1183 } while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001184
1185/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001187 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001188 */
Hanno Beckerd6028a12018-10-15 12:01:35 +01001189#define MOD_SUB( N ) \
1190 while( (N).s < 0 && mbedtls_mpi_cmp_int( &(N), 0 ) != 0 ) \
1191 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001192
1193/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 * 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 +02001195 * We known P, N and the result are positive, so sub_abs is correct, and
1196 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001197 */
Hanno Beckerd6028a12018-10-15 12:01:35 +01001198#define MOD_ADD( N ) \
1199 while( mbedtls_mpi_cmp_mpi( &(N), &grp->P ) >= 0 ) \
1200 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001201
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001202#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001203/*
1204 * For curves in short Weierstrass form, we do all the internal operations in
1205 * Jacobian coordinates.
1206 *
1207 * For multiplication, we'll use a comb method with coutermeasueres against
1208 * SPA, hence timing attacks.
1209 */
1210
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001211/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001212 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001213 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001216{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001217 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001221 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001222
Janos Follathb0697532016-08-18 12:38:46 +01001223#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001224 if( mbedtls_internal_ecp_grp_capable( grp ) )
1225 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +01001226#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001229
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001230 /*
1231 * X = X / Z^2 mod p
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
1234 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1235 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 +01001236
1237 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001238 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
1241 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 +01001242
1243 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001244 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001247
1248cleanup:
1249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001251
1252 return( ret );
1253}
1254
1255/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001256 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001257 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001258 * (See for example Cohen's "A Course in Computational Algebraic Number
1259 * Theory", Algorithm 10.3.4.)
1260 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001261 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001262 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001263 *
1264 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001267 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001268{
1269 int ret;
1270 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_mpi *c, u, Zi, ZZi;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001272
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001273 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001274 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001275
Janos Follathb0697532016-08-18 12:38:46 +01001276#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001277 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001278 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +01001279#endif
1280
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001281 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001282 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001283
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001284 for( i = 0; i < T_size; i++ )
1285 mbedtls_mpi_init( &c[i] );
1286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001288
1289 /*
1290 * c[i] = Z_0 * ... * Z_i
1291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001293 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001296 MOD_MUL( c[i] );
1297 }
1298
1299 /*
1300 * u = 1 / (Z_0 * ... * Z_n) mod P
1301 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001302 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001303
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001304 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001305 {
1306 /*
1307 * Zi = 1 / Z_i mod p
1308 * u = 1 / (Z_0 * ... * Z_i) mod P
1309 */
1310 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001312 }
1313 else
1314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
1316 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001317 }
1318
1319 /*
1320 * proceed as in normalize()
1321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1323 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
1324 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
1325 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 +01001326
1327 /*
1328 * Post-precessing: reclaim some memory by shrinking coordinates
1329 * - not storing Z (always 1)
1330 * - shrinking other coordinates, but still keeping the same number of
1331 * limbs as P, as otherwise it will too likely be regrown too fast.
1332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1334 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1335 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001336
1337 if( i == 0 )
1338 break;
1339 }
1340
1341cleanup:
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001344 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_mpi_free( &c[i] );
1346 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001347
1348 return( ret );
1349}
1350
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001351/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001352 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1353 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1356 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001357 unsigned char inv )
1358{
1359 int ret;
1360 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001364
1365 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1367 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1368 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001369
1370cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001372
1373 return( ret );
1374}
1375
1376/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001377 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001378 *
Peter Dettmance661b22015-02-07 14:43:51 +07001379 * 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 +01001380 *
Peter Dettmance661b22015-02-07 14:43:51 +07001381 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1382 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1383 *
1384 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1385 *
1386 * Cost: 1D := 3M + 4S (A == 0)
1387 * 4M + 4S (A == -3)
1388 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1391 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001392{
1393 int ret;
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001394 mbedtls_mpi M, S, T, U;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001397 dbl_count++;
1398#endif
1399
Janos Follathb0697532016-08-18 12:38:46 +01001400#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001401 if( mbedtls_internal_ecp_grp_capable( grp ) )
1402 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001403#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001404
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001405 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 +01001406
1407 /* Special case for A = -3 */
1408 if( grp->A.p == NULL )
1409 {
Peter Dettmance661b22015-02-07 14:43:51 +07001410 /* M = 3(X + Z^2)(X - Z^2) */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001411 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1412 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
1413 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
1414 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
1415 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001416 }
1417 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001418 {
Peter Dettmance661b22015-02-07 14:43:51 +07001419 /* M = 3.X^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001420 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
1421 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001422
1423 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001424 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001425 {
1426 /* M += A.Z^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001427 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1428 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
1429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
1430 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001431 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001432 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001433
Peter Dettmance661b22015-02-07 14:43:51 +07001434 /* S = 4.X.Y^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001435 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
1436 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
1437 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
1438 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001439
Peter Dettmance661b22015-02-07 14:43:51 +07001440 /* U = 8.Y^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001441 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
1442 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001443
1444 /* T = M^2 - 2.S */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001445 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
1446 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
1447 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
Peter Dettmance661b22015-02-07 14:43:51 +07001448
1449 /* S = M(S - T) - U */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001450 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
1451 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
1452 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
Peter Dettmance661b22015-02-07 14:43:51 +07001453
1454 /* U = 2.Y.Z */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001455 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
1456 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001457
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001458 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1459 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1460 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001461
1462cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001463 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 +02001464
1465 return( ret );
1466}
1467
1468/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001469 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001470 *
1471 * The coordinates of Q must be normalized (= affine),
1472 * but those of P don't need to. R is not normalized.
1473 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001474 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001475 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001476 * - at each step, P, Q and R are multiples of the base point, the factor
1477 * being less than its order, so none of them is zero;
1478 * - Q is an odd multiple of the base point, P an even multiple,
1479 * due to the choice of precomputed points in the modified comb method.
1480 * So branches for these cases do not leak secret information.
1481 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001482 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1483 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001484 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1487 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001488{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001489 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001493 add_count++;
1494#endif
1495
Janos Follathb0697532016-08-18 12:38:46 +01001496#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001497 if( mbedtls_internal_ecp_grp_capable( grp ) )
1498 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001499#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001500
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001501 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001502 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
1505 return( mbedtls_ecp_copy( R, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
1508 return( mbedtls_ecp_copy( R, P ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001509
1510 /*
1511 * Make sure Q coordinates are normalized
1512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
1514 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
1517 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1520 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1521 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1522 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1523 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1524 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001525
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001526 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001530 {
1531 ret = ecp_double_jac( grp, R, P );
1532 goto cleanup;
1533 }
1534 else
1535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 ret = mbedtls_ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001537 goto cleanup;
1538 }
1539 }
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1542 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1543 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1544 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1545 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1546 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1547 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1548 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1549 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1550 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1551 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1552 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1555 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1556 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001557
1558cleanup:
1559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1561 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001562
1563 return( ret );
1564}
1565
1566/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001567 * Randomize jacobian coordinates:
1568 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001569 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001570 *
1571 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001574 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1575{
1576 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 mbedtls_mpi l, ll;
Janos Follathb0697532016-08-18 12:38:46 +01001578 size_t p_size;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001579 int count = 0;
1580
Janos Follathb0697532016-08-18 12:38:46 +01001581#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001582 if( mbedtls_internal_ecp_grp_capable( grp ) )
1583 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001584#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001585
1586 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001588
1589 /* Generate l such that 1 < l < p */
1590 do
1591 {
Ron Eldorca6ff582017-01-12 14:50:50 +02001592 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1595 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001596
1597 if( count++ > 10 )
Jonas6645fd32020-05-08 16:57:18 +09001598 {
1599 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
1600 goto cleanup;
1601 }
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001604
1605 /* Z = l * Z */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 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 +02001607
1608 /* X = l^2 * X */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1610 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 +02001611
1612 /* Y = l^3 * Y */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1614 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 +02001615
1616cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001618
1619 return( ret );
1620}
1621
1622/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001623 * Check and define parameters used by the comb method (see below for details)
1624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1626#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001627#endif
1628
1629/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001631
1632/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001634
1635/*
1636 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001637 *
1638 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001639 * modified version that provides resistance to SPA by avoiding zero
1640 * digits in the representation as in [3]. We modify the method further by
1641 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001642 * representation uses one more K_i, due to carries, but saves on the size of
1643 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001644 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001645 * Summary of the comb method and its modifications:
1646 *
1647 * - The goal is to compute m*P for some w*d-bit integer m.
1648 *
1649 * - The basic comb method splits m into the w-bit integers
1650 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1651 * index has residue i modulo d, and computes m * P as
1652 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1653 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1654 *
1655 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1656 * .. + 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]] ..,
1657 * thereby successively converting it into a form where all summands
1658 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1659 *
1660 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1661 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1662 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1663 * Performing and iterating this procedure for those x[i] that are even
1664 * (keeping track of carry), we can transform the original sum into one of the form
1665 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1666 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1667 * which is why we are only computing half of it in the first place in
1668 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1669 *
1670 * - For the sake of compactness, only the seven low-order bits of x[i]
1671 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001672 * 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 +02001673 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001674 *
1675 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001676 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001677 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001679 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1680 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001681 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001682static void ecp_comb_recode_core( unsigned char x[], size_t d,
1683 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001684{
1685 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001686 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001687
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001688 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001689
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001690 /* First get the classical comb values (except for x_d = 0) */
1691 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001692 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001694
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001695 /* Now make sure x_1 .. x_d are odd */
1696 c = 0;
1697 for( i = 1; i <= d; i++ )
1698 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001699 /* Add carry and update it */
1700 cc = x[i] & c;
1701 x[i] = x[i] ^ c;
1702 c = cc;
1703
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001704 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001705 adjust = 1 - ( x[i] & 0x01 );
1706 c |= x[i] & ( x[i-1] * adjust );
1707 x[i] = x[i] ^ ( x[i-1] * adjust );
1708 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001709 }
1710}
1711
1712/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001713 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001714 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001715 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001716 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001717 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1718 * 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 +01001719 *
1720 * 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 +02001721 *
1722 * Note: Even comb values (those where P would be omitted from the
1723 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001724 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001725 *
1726 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001727 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001728 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1729 * (3) [add] Computation of all T[i]
1730 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001731 *
1732 * Step 1 can be interrupted but not the others; together with the final
1733 * coordinate normalization they are the largest steps done at once, depending
1734 * on the window size. Here are operation counts for P-256:
1735 *
1736 * step (2) (3) (4)
1737 * w = 5 142 165 208
1738 * w = 4 136 77 160
1739 * w = 3 130 33 136
1740 * w = 2 124 11 124
1741 *
1742 * So if ECC operations are blocking for too long even with a low max_ops
1743 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1744 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1747 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001748 unsigned char w, size_t d,
1749 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001750{
1751 int ret;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001752 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001753 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001754 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001756
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001757#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001758 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001759 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001760 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
1761 goto dbl;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001762 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001763 goto norm_dbl;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001764 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
1765 goto add;
1766 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
1767 goto norm_add;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001768 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001769#else
1770 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001771#endif
1772
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001773#if defined(MBEDTLS_ECP_RESTARTABLE)
1774 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1775 {
1776 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1777
1778 /* initial state for the loop */
1779 rs_ctx->rsm->i = 0;
1780 }
1781
1782dbl:
1783#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001784 /*
1785 * Set T[0] = P and
1786 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001789
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001790#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001791 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
1792 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001793 else
1794#endif
1795 j = 0;
1796
1797 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001798 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001799 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001800
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001801 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001802 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001803
1804 if( j % d == 0 )
1805 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1806
1807 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001808 }
1809
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001810#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001811 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1812 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1813
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001814norm_dbl:
1815#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001816 /*
1817 * Normalize current elements in T. As T has holes,
1818 * use an auxiliary array of pointers to elements in T.
1819 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001820 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001821 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001822 TT[j++] = T + i;
1823
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001824 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001825
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001826 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001827
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001828#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001829 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1830 rs_ctx->rsm->state = ecp_rsm_pre_add;
1831
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001832add:
1833#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001834 /*
1835 * Compute the remaining ones using the minimal number of additions
1836 * Be careful to update T[2^l] only after using it!
1837 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001838 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001839
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001840 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001841 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001842 j = i;
1843 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001845 }
1846
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001847#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001848 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1849 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1850
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001851norm_add:
1852#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001853 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001854 * Normalize final elements in T. Even though there are no holes now, we
1855 * still need the auxiliary array for homogeneity with the previous
1856 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001857 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001858 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001859 TT[j] = T + j + 1;
1860
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001861 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001862
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001863 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001864
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001865cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001866#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001867 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1868 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001869 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001870 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001871 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001872 }
1873#endif
Janos Follathb0697532016-08-18 12:38:46 +01001874
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001875 return( ret );
1876}
1877
1878/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001879 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001880 *
1881 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001884 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001885 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001886{
1887 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001888 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001889
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001890 /* Ignore the "sign" bit and scale down */
1891 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001892
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001893 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001894 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1897 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001898 }
1899
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001900 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001902
1903cleanup:
1904 return( ret );
1905}
1906
1907/*
1908 * Core multiplication algorithm for the (modified) comb method.
1909 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001910 *
1911 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001914 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001915 const unsigned char x[], size_t d,
1916 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001917 void *p_rng,
1918 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001919{
1920 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001922 size_t i;
1923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001925
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001926#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001927 (void) rs_ctx;
1928#endif
1929
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001930#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001931 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1932 rs_ctx->rsm->state != ecp_rsm_comb_core )
1933 {
1934 rs_ctx->rsm->i = 0;
1935 rs_ctx->rsm->state = ecp_rsm_comb_core;
1936 }
1937
1938 /* new 'if' instead of nested for the sake of the 'else' branch */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001939 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001940 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001941 /* restore current index (R already pointing to rs_ctx->rsm->R) */
1942 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001943 }
1944 else
1945#endif
1946 {
1947 /* Start with a non-zero point and randomize its coordinates */
1948 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001949 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001950 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
1951 if( f_rng != 0 )
1952 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
1953 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001954
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001955 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001956 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001957 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001958 --i;
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001961 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001963 }
1964
1965cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001968
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001969#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001970 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1971 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001972 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001973 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001974 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001975 }
1976#endif
1977
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001978 return( ret );
1979}
1980
1981/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001982 * Recode the scalar to get constant-time comb multiplication
1983 *
1984 * As the actual scalar recoding needs an odd scalar as a starting point,
1985 * this wrapper ensures that by replacing m by N - m if necessary, and
1986 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001987 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001988 * This works because we only support large prime order for Short Weierstrass
1989 * curves, so N is always odd hence either m or N - m is.
1990 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001991 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001992 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001993static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
1994 const mbedtls_mpi *m,
1995 unsigned char k[COMB_MAX_D + 1],
1996 size_t d,
1997 unsigned char w,
1998 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001999{
2000 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002001 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002002
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002003 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002004 mbedtls_mpi_init( &mm );
2005
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002006 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002007 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
2008 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2009
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002010 /* do we need the parity trick? */
2011 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
2012
2013 /* execute parity fix in constant time */
2014 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002015 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002016 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
2017
2018 /* actual scalar recoding */
2019 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002020
2021cleanup:
2022 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002023 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002024
2025 return( ret );
2026}
2027
2028/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002029 * Perform comb multiplication (for short Weierstrass curves)
2030 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002031 *
2032 * Scalar recoding may use a parity trick that makes us compute -m * P,
2033 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002034 */
2035static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
2036 mbedtls_ecp_point *R,
2037 const mbedtls_mpi *m,
2038 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002039 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002040 unsigned char w,
2041 size_t d,
2042 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002043 void *p_rng,
2044 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002045{
2046 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002047 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002048 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002049 mbedtls_ecp_point *RR = R;
2050
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002051#if defined(MBEDTLS_ECP_RESTARTABLE)
2052 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2053 {
2054 RR = &rs_ctx->rsm->R;
2055
2056 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
2057 goto final_norm;
2058 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002059#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002060
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002061 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
2062 &parity_trick ) );
2063 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
2064 f_rng, p_rng, rs_ctx ) );
2065 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
2066
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002067#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002068 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002069 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002070
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002071final_norm:
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002072#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002073 /*
2074 * Knowledge of the jacobian coordinates may leak the last few bits of the
2075 * scalar [1], and since our MPI implementation isn't constant-flow,
2076 * inversion (used for coordinate normalization) may leak the full value
2077 * of its input via side-channels [2].
2078 *
2079 * [1] https://eprint.iacr.org/2003/191
2080 * [2] https://eprint.iacr.org/2020/055
2081 *
2082 * Avoid the leak by randomizing coordinates before we normalize them.
2083 */
2084 if( f_rng != 0 )
2085 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
2086
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002087 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002088 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
2089
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002090#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02002091 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2092 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002093#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002094
2095cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002096 return( ret );
2097}
2098
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002099/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002100 * Pick window size based on curve size and whether we optimize for base point
2101 */
2102static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
2103 unsigned char p_eq_g )
2104{
2105 unsigned char w;
2106
2107 /*
2108 * Minimize the number of multiplications, that is minimize
2109 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2110 * (see costs of the various parts, with 1S = 1M)
2111 */
2112 w = grp->nbits >= 384 ? 5 : 4;
2113
2114 /*
2115 * If P == G, pre-compute a bit more, since this may be re-used later.
2116 * Just adding one avoids upping the cost of the first mul too much,
2117 * and the memory cost too.
2118 */
2119 if( p_eq_g )
2120 w++;
2121
2122 /*
2123 * Make sure w is within bounds.
2124 * (The last test is useful only for very small curves in the test suite.)
2125 */
2126 if( w > MBEDTLS_ECP_WINDOW_SIZE )
2127 w = MBEDTLS_ECP_WINDOW_SIZE;
2128 if( w >= grp->nbits )
2129 w = 2;
2130
2131 return( w );
2132}
2133
2134/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002135 * Multiplication using the comb method - for curves in short Weierstrass form
2136 *
2137 * This function is mainly responsible for administrative work:
2138 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002139 * - managing the table of precomputed points (passed between the below two
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002140 * functions): allocation, computation, ownership tranfer, freeing.
2141 *
2142 * It delegates the actual arithmetic work to:
2143 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2144 *
2145 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2148 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002149 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002150 void *p_rng,
2151 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002152{
2153 int ret;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002154 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002155 size_t d;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002156 unsigned char T_size = 0, T_ok = 0;
2157 mbedtls_ecp_point *T = NULL;
2158#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2159 ecp_drbg_context drbg_ctx;
2160
2161 ecp_drbg_init( &drbg_ctx );
2162#endif
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002163
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002164 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002165
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002166#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2167 if( f_rng == NULL )
2168 {
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +02002169 /* Adjust pointers */
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002170 f_rng = &ecp_drbg_random;
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +02002171#if defined(MBEDTLS_ECP_RESTARTABLE)
2172 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2173 p_rng = &rs_ctx->rsm->drbg_ctx;
2174 else
2175#endif
2176 p_rng = &drbg_ctx;
2177
2178 /* Initialize internal DRBG if necessary */
2179#if defined(MBEDTLS_ECP_RESTARTABLE)
2180 if( rs_ctx == NULL || rs_ctx->rsm == NULL ||
2181 rs_ctx->rsm->drbg_seeded == 0 )
2182#endif
2183 {
2184 MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m ) );
2185 }
2186#if defined(MBEDTLS_ECP_RESTARTABLE)
2187 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2188 rs_ctx->rsm->drbg_seeded = 1;
2189#endif
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002190 }
2191#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2192
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002193 /* Is P the base point ? */
2194#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2195 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
2196 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002197#else
2198 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002199#endif
2200
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002201 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002202 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002203 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002204 d = ( grp->nbits + w - 1 ) / w;
2205
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002206 /* Pre-computed table: do we have it already for the base point? */
2207 if( p_eq_g && grp->T != NULL )
2208 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002209 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002210 T = grp->T;
2211 T_ok = 1;
2212 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002213 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002214#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002215 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002216 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002217 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002218 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002219 T = rs_ctx->rsm->T;
2220 rs_ctx->rsm->T = NULL;
2221 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002222
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002223 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002224 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002225 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002226 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002227#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002228 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002229 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002230 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002231 if( T == NULL )
2232 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002233 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002234 goto cleanup;
2235 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002236
2237 for( i = 0; i < T_size; i++ )
2238 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002239
2240 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002241 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002242
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002243 /* Compute table (or finish computing it) if not done already */
2244 if( !T_ok )
2245 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002246 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002247
2248 if( p_eq_g )
2249 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002250 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002251 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002252 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002253 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002254 }
2255 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002256
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002257 /* Actual comb multiplication using precomputed points */
2258 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002259 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002260 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002261
2262cleanup:
2263
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002264#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2265 ecp_drbg_free( &drbg_ctx );
2266#endif
2267
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002268 /* does T belong to the group? */
2269 if( T == grp->T )
2270 T = NULL;
2271
2272 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002273#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002274 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 +01002275 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002276 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002277 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002278 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002279 T = NULL;
2280 }
2281#endif
2282
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002283 /* did T belong to us? then let's destroy it! */
2284 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002285 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002286 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_ecp_point_free( &T[i] );
2288 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002289 }
2290
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002291 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002292#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002293 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2294#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002295 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01002296 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002298
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002299 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002300
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002301 return( ret );
2302}
2303
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002304#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002305
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002306#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002307/*
2308 * For Montgomery curves, we do all the internal arithmetic in projective
2309 * coordinates. Import/export of points uses only the x coordinates, which is
2310 * internaly represented as X / Z.
2311 *
2312 * For scalar multiplication, we'll use a Montgomery ladder.
2313 */
2314
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002315/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002316 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2317 * Cost: 1M + 1I
2318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002320{
2321 int ret;
2322
Janos Follathb0697532016-08-18 12:38:46 +01002323#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002324 if( mbedtls_internal_ecp_grp_capable( grp ) )
2325 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002326#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
2329 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
2330 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002331
2332cleanup:
2333 return( ret );
2334}
2335
2336/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002337 * Randomize projective x/z coordinates:
2338 * (X, Z) -> (l X, l Z) for random l
2339 * This is sort of the reverse operation of ecp_normalize_mxz().
2340 *
2341 * This countermeasure was first suggested in [2].
2342 * Cost: 2M
2343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002345 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2346{
2347 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 mbedtls_mpi l;
Janos Follathb0697532016-08-18 12:38:46 +01002349 size_t p_size;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002350 int count = 0;
2351
Janos Follathb0697532016-08-18 12:38:46 +01002352#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002353 if( mbedtls_internal_ecp_grp_capable( grp ) )
2354 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Janos Follath372697b2016-10-28 16:53:11 +01002355#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002356
2357 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002359
2360 /* Generate l such that 1 < l < p */
2361 do
2362 {
Ron Eldorca6ff582017-01-12 14:50:50 +02002363 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
2366 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002367
2368 if( count++ > 10 )
Jonas6645fd32020-05-08 16:57:18 +09002369 {
2370 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
2371 goto cleanup;
2372 }
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
2377 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 +01002378
2379cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002381
2382 return( ret );
2383}
2384
2385/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002386 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2387 * for Montgomery curves in x/z coordinates.
2388 *
2389 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2390 * with
2391 * d = X1
2392 * P = (X2, Z2)
2393 * Q = (X3, Z3)
2394 * R = (X4, Z4)
2395 * S = (X5, Z5)
2396 * and eliminating temporary variables tO, ..., t4.
2397 *
2398 * Cost: 5M + 4S
2399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2401 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2402 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2403 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002404{
2405 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002407
Janos Follathb0697532016-08-18 12:38:46 +01002408#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002409 if( mbedtls_internal_ecp_grp_capable( grp ) )
2410 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002411#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2414 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2415 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
2418 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
2419 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
2420 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
2421 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
2422 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
2423 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
2424 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
2425 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
2429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
2430 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
2431 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
2432 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
2433 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
2434 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 +01002435
2436cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2438 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2439 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002440
2441 return( ret );
2442}
2443
2444/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002445 * Multiplication with Montgomery ladder in x/z coordinates,
2446 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2449 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002450 int (*f_rng)(void *, unsigned char *, size_t),
2451 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002452{
2453 int ret;
2454 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002455 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 mbedtls_ecp_point RP;
2457 mbedtls_mpi PX;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002458#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2459 ecp_drbg_context drbg_ctx;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002460
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002461 ecp_drbg_init( &drbg_ctx );
2462#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002464
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002465#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2466 if( f_rng == NULL )
2467 {
2468 MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) );
2469 f_rng = &ecp_drbg_random;
2470 p_rng = &drbg_ctx;
2471 }
2472#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2473
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002474 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2476 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002477
2478 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2480 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2481 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002482
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002483 /* RP.X might be sligtly larger than P, so reduce it */
2484 MOD_ADD( RP.X );
2485
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002486 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002487 if( f_rng != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002489
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002490 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002491 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002492 while( i-- > 0 )
2493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002495 /*
2496 * if (b) R = 2R + P else R = 2R,
2497 * which is:
2498 * if (b) double_add( RP, R, RP, R )
2499 * else double_add( R, RP, R, RP )
2500 * but using safe conditional swaps to avoid leaks
2501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2503 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2504 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2505 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2506 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002507 }
2508
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002509 /*
2510 * Knowledge of the projective coordinates may leak the last few bits of the
2511 * scalar [1], and since our MPI implementation isn't constant-flow,
2512 * inversion (used for coordinate normalization) may leak the full value
2513 * of its input via side-channels [2].
2514 *
2515 * [1] https://eprint.iacr.org/2003/191
2516 * [2] https://eprint.iacr.org/2020/055
2517 *
2518 * Avoid the leak by randomizing coordinates before we normalize them.
2519 */
2520 if( f_rng != NULL )
2521 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002524
2525cleanup:
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002526#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2527 ecp_drbg_free( &drbg_ctx );
2528#endif
2529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002531
2532 return( ret );
2533}
2534
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002535#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002536
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002537/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002538 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002539 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002540int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002542 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2543 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002544{
Janos Follathb0697532016-08-18 12:38:46 +01002545 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002546#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2547 char is_grp_capable = 0;
2548#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002549 ECP_VALIDATE_RET( grp != NULL );
2550 ECP_VALIDATE_RET( R != NULL );
2551 ECP_VALIDATE_RET( m != NULL );
2552 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002553
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002554#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002555 /* reset ops count for this call if top-level */
2556 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2557 rs_ctx->ops_done = 0;
2558#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002559
Janos Follathc44ab972016-11-18 16:38:23 +00002560#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002561 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002562 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( 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é-Gonnard95aedfe2017-08-24 13:47:04 +02002565#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002566 /* skip argument check when restarting */
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002567 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002568#endif
2569 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002570 /* check_privkey is free */
2571 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2572
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002573 /* Common sanity checks */
2574 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2575 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002576 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002577
2578 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002579#if defined(ECP_MONTGOMERY)
2580 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002581 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002582#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002583#if defined(ECP_SHORTWEIERSTRASS)
2584 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002585 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002586#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002587
Janos Follath6c8ccd52016-11-29 15:37:09 +00002588cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002589
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002590#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002591 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002592 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002593#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002594
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002595#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002596 if( rs_ctx != NULL )
2597 rs_ctx->depth--;
2598#endif
2599
Janos Follathb0697532016-08-18 12:38:46 +01002600 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002601}
2602
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002603/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002604 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002605 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002606int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002607 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002608 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002609{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002610 ECP_VALIDATE_RET( grp != NULL );
2611 ECP_VALIDATE_RET( R != NULL );
2612 ECP_VALIDATE_RET( m != NULL );
2613 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002614 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002615}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002616
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002617#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002618/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002619 * Check that an affine point is valid as a public key,
2620 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002623{
2624 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002626
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002627 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2629 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2630 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2631 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2632 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002635
2636 /*
2637 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002638 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
2641 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002642
2643 /* Special case for A = -3 */
2644 if( grp->A.p == NULL )
2645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002647 }
2648 else
2649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002651 }
2652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
2654 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2657 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002658
2659cleanup:
2660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002662
2663 return( ret );
2664}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002665#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002666
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002667/*
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002668 * R = m * P with shortcuts for m == 1 and m == -1
2669 * NOT constant-time - ONLY for short Weierstrass!
2670 */
2671static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2672 mbedtls_ecp_point *R,
2673 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002674 const mbedtls_ecp_point *P,
2675 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002676{
2677 int ret;
2678
2679 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
2680 {
2681 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2682 }
2683 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2684 {
2685 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2686 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2687 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2688 }
2689 else
2690 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002691 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2692 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002693 }
2694
2695cleanup:
2696 return( ret );
2697}
2698
2699/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002700 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002701 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002702 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002703int mbedtls_ecp_muladd_restartable(
2704 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002705 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002706 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2707 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002708{
2709 int ret;
2710 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002711 mbedtls_ecp_point *pmP = &mP;
2712 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002713#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2714 char is_grp_capable = 0;
2715#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002716 ECP_VALIDATE_RET( grp != NULL );
2717 ECP_VALIDATE_RET( R != NULL );
2718 ECP_VALIDATE_RET( m != NULL );
2719 ECP_VALIDATE_RET( P != NULL );
2720 ECP_VALIDATE_RET( n != NULL );
2721 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002722
2723 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
2724 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2725
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002726 mbedtls_ecp_point_init( &mP );
2727
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002728 ECP_RS_ENTER( ma );
2729
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002730#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002731 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2732 {
2733 /* redirect intermediate results to restart context */
2734 pmP = &rs_ctx->ma->mP;
2735 pR = &rs_ctx->ma->R;
2736
2737 /* jump to next operation */
2738 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2739 goto mul2;
2740 if( rs_ctx->ma->state == ecp_rsma_add )
2741 goto add;
2742 if( rs_ctx->ma->state == ecp_rsma_norm )
2743 goto norm;
2744 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002745#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002746
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002747 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
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 )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002750 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002751
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002752mul2:
2753#endif
2754 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Janos Follathaf6f2692018-12-07 09:55:24 +00002755
2756#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2757 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2758 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2759#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2760
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002761#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002762 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002763 rs_ctx->ma->state = ecp_rsma_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002764
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002765add:
2766#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002767 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002768 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002769#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002770 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002771 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002772
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002773norm:
2774#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002775 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002776 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2777
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002778#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002779 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2780 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2781#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002782
2783cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002784#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002785 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002786 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002787#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002788
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002789 mbedtls_ecp_point_free( &mP );
2790
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002791 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002792
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002793 return( ret );
2794}
2795
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002796/*
2797 * Linear combination
2798 * NOT constant-time
2799 */
2800int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2801 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2802 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2803{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002804 ECP_VALIDATE_RET( grp != NULL );
2805 ECP_VALIDATE_RET( R != NULL );
2806 ECP_VALIDATE_RET( m != NULL );
2807 ECP_VALIDATE_RET( P != NULL );
2808 ECP_VALIDATE_RET( n != NULL );
2809 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002810 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2811}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002812
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002813#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002814/*
2815 * Check validity of a public key for Montgomery curves with x-only schemes
2816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002818{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002819 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002820 /* Allow any public value, if it's too big then we'll just reduce it mod p
2821 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2823 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002824
2825 return( 0 );
2826}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002827#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002828
2829/*
2830 * Check that a point is valid as a public key
2831 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002832int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
2833 const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002834{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002835 ECP_VALIDATE_RET( grp != NULL );
2836 ECP_VALIDATE_RET( pt != NULL );
2837
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002838 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2840 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002841
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002842#if defined(ECP_MONTGOMERY)
2843 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002844 return( ecp_check_pubkey_mx( grp, pt ) );
2845#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002846#if defined(ECP_SHORTWEIERSTRASS)
2847 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002848 return( ecp_check_pubkey_sw( grp, pt ) );
2849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002851}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002852
2853/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002855 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002856int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
2857 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002858{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002859 ECP_VALIDATE_RET( grp != NULL );
2860 ECP_VALIDATE_RET( d != NULL );
2861
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002862#if defined(ECP_MONTGOMERY)
2863 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002864 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002865 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2867 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002868 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002870
2871 /* see [Curve25519] page 5 */
2872 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
2873 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2874
2875 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002876 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002877#endif /* ECP_MONTGOMERY */
2878#if defined(ECP_SHORTWEIERSTRASS)
2879 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002880 {
2881 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2883 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2884 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002885 else
2886 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002887 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002888#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002891}
2892
2893/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002894 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002895 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002896int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
2897 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002898 int (*f_rng)(void *, unsigned char *, size_t),
2899 void *p_rng )
2900{
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002901 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002902 size_t n_size;
2903
2904 ECP_VALIDATE_RET( grp != NULL );
2905 ECP_VALIDATE_RET( d != NULL );
2906 ECP_VALIDATE_RET( f_rng != NULL );
2907
2908 n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002909
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002910#if defined(ECP_MONTGOMERY)
2911 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002912 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002913 /* [M225] page 5 */
2914 size_t b;
2915
Janos Follath98e28a72016-05-31 14:03:54 +01002916 do {
2917 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
2918 } while( mbedtls_mpi_bitlen( d ) == 0);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002919
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002920 /* Make sure the most significant bit is nbits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002921 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002922 if( b > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002924 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002926
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002927 /* Make sure the last two bits are unset for Curve448, three bits for
2928 Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
2930 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002931 if( grp->nbits == 254 )
2932 {
2933 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
2934 }
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002935 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002936#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002937
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002938#if defined(ECP_SHORTWEIERSTRASS)
2939 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002940 {
2941 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002942 int count = 0;
Janos Follath867a3ab2019-10-11 14:21:53 +01002943 unsigned cmp = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01002944
2945 /*
2946 * Match the procedure given in RFC 6979 (deterministic ECDSA):
2947 * - use the same byte ordering;
2948 * - keep the leftmost nbits bits of the generated octet string;
2949 * - try until result is in the desired range.
2950 * This also avoids any biais, which is especially important for ECDSA.
2951 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002952 do
2953 {
Hanno Becker7c8cb9c2017-10-17 15:19:38 +01002954 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002956
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01002957 /*
2958 * Each try has at worst a probability 1/2 of failing (the msb has
2959 * a probability 1/2 of being 0, and then the result will be < N),
2960 * so after 30 tries failure probability is a most 2**(-30).
2961 *
2962 * For most curves, 1 try is enough with overwhelming probability,
2963 * since N starts with a lot of 1s in binary, but some curves
2964 * such as secp224k1 are actually very close to the worst case.
2965 */
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02002966 if( ++count > 30 )
2967 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Janos Follath4c3408b2019-09-16 14:27:39 +01002968
Janos Follath867a3ab2019-10-11 14:21:53 +01002969 ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
Janos Follath4c3408b2019-09-16 14:27:39 +01002970 if( ret != 0 )
2971 {
2972 goto cleanup;
2973 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002974 }
Janos Follath867a3ab2019-10-11 14:21:53 +01002975 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002976 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002977#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002978
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002979cleanup:
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002980 return( ret );
2981}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002982
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002983/*
2984 * Generate a keypair with configurable base point
2985 */
2986int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
2987 const mbedtls_ecp_point *G,
2988 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2989 int (*f_rng)(void *, unsigned char *, size_t),
2990 void *p_rng )
2991{
2992 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002993 ECP_VALIDATE_RET( grp != NULL );
2994 ECP_VALIDATE_RET( d != NULL );
2995 ECP_VALIDATE_RET( G != NULL );
2996 ECP_VALIDATE_RET( Q != NULL );
2997 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002998
2999 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
3000 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
3001
3002cleanup:
3003 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003004}
3005
3006/*
3007 * Generate key pair, wrapper for conventional base point
3008 */
3009int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
3010 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3011 int (*f_rng)(void *, unsigned char *, size_t),
3012 void *p_rng )
3013{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003014 ECP_VALIDATE_RET( grp != NULL );
3015 ECP_VALIDATE_RET( d != NULL );
3016 ECP_VALIDATE_RET( Q != NULL );
3017 ECP_VALIDATE_RET( f_rng != NULL );
3018
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003019 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003020}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003021
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003022/*
3023 * Generate a keypair, prettier wrapper
3024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003026 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
3027{
3028 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003029 ECP_VALIDATE_RET( key != NULL );
3030 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003031
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003032 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003033 return( ret );
3034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003036}
3037
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003038/*
3039 * Check a public-private key pair
3040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003042{
3043 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 mbedtls_ecp_point Q;
3045 mbedtls_ecp_group grp;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003046 ECP_VALIDATE_RET( pub != NULL );
3047 ECP_VALIDATE_RET( prv != NULL );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003050 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
3052 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
3053 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003056 }
3057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058 mbedtls_ecp_point_init( &Q );
3059 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 /* mbedtls_ecp_mul() needs a non-const group... */
3062 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003063
3064 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
3068 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
3069 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003072 goto cleanup;
3073 }
3074
3075cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 mbedtls_ecp_point_free( &Q );
3077 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003078
3079 return( ret );
3080}
3081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003083
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003084/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003085 * Checkup routine
3086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003088{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003089 int ret;
3090 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 mbedtls_ecp_group grp;
3092 mbedtls_ecp_point R, P;
3093 mbedtls_mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003094 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003095 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003096 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003097 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003098 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003099 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003100 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003101 "400000000000000000000000000000000000000000000000", /* one and zeros */
3102 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3103 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003104 };
3105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 mbedtls_ecp_group_init( &grp );
3107 mbedtls_ecp_point_init( &R );
3108 mbedtls_ecp_point_init( &P );
3109 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003110
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003111 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003113 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003114#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003115 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003116#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003117
3118 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003120
3121 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
3123 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003124
3125 add_count = 0;
3126 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003127 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3129 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003130
3131 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3132 {
3133 add_c_prev = add_count;
3134 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003135 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003136 add_count = 0;
3137 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003138 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3141 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003142
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003143 if( add_count != add_c_prev ||
3144 dbl_count != dbl_c_prev ||
3145 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003146 {
3147 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003149
3150 ret = 1;
3151 goto cleanup;
3152 }
3153 }
3154
3155 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003157
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003158 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003160 /* We computed P = 2G last time, use it */
3161
3162 add_count = 0;
3163 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003164 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3166 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003167
3168 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3169 {
3170 add_c_prev = add_count;
3171 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003172 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003173 add_count = 0;
3174 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003175 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3178 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003179
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003180 if( add_count != add_c_prev ||
3181 dbl_count != dbl_c_prev ||
3182 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003183 {
3184 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003186
3187 ret = 1;
3188 goto cleanup;
3189 }
3190 }
3191
3192 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003194
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003195cleanup:
3196
3197 if( ret < 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 mbedtls_ecp_group_free( &grp );
3201 mbedtls_ecp_point_free( &R );
3202 mbedtls_ecp_point_free( &P );
3203 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003204
3205 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003207
3208 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003209}
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003212
Janos Follathb0697532016-08-18 12:38:46 +01003213#endif /* !MBEDTLS_ECP_ALT */
3214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#endif /* MBEDTLS_ECP_C */