blob: 67edf4994120b97a8793c97b66aad5870b382cbd [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 ) );
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02001951#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001952 if( f_rng != 0 )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02001953#endif
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001954 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
1955 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001956
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001957 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001958 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001959 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001960 --i;
1961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001963 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001965 }
1966
1967cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001970
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001971#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001972 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1973 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001974 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001975 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001976 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001977 }
1978#endif
1979
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001980 return( ret );
1981}
1982
1983/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001984 * Recode the scalar to get constant-time comb multiplication
1985 *
1986 * As the actual scalar recoding needs an odd scalar as a starting point,
1987 * this wrapper ensures that by replacing m by N - m if necessary, and
1988 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001989 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001990 * This works because we only support large prime order for Short Weierstrass
1991 * curves, so N is always odd hence either m or N - m is.
1992 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001993 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001994 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001995static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
1996 const mbedtls_mpi *m,
1997 unsigned char k[COMB_MAX_D + 1],
1998 size_t d,
1999 unsigned char w,
2000 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002001{
2002 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002003 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002004
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002005 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002006 mbedtls_mpi_init( &mm );
2007
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002008 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002009 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
2010 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2011
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002012 /* do we need the parity trick? */
2013 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
2014
2015 /* execute parity fix in constant time */
2016 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002017 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002018 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
2019
2020 /* actual scalar recoding */
2021 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002022
2023cleanup:
2024 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002025 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002026
2027 return( ret );
2028}
2029
2030/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002031 * Perform comb multiplication (for short Weierstrass curves)
2032 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002033 *
2034 * Scalar recoding may use a parity trick that makes us compute -m * P,
2035 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002036 */
2037static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
2038 mbedtls_ecp_point *R,
2039 const mbedtls_mpi *m,
2040 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002041 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002042 unsigned char w,
2043 size_t d,
2044 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002045 void *p_rng,
2046 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002047{
2048 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002049 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002050 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002051 mbedtls_ecp_point *RR = R;
2052
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002053#if defined(MBEDTLS_ECP_RESTARTABLE)
2054 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2055 {
2056 RR = &rs_ctx->rsm->R;
2057
2058 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
2059 goto final_norm;
2060 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002061#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002062
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002063 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
2064 &parity_trick ) );
2065 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
2066 f_rng, p_rng, rs_ctx ) );
2067 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
2068
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002069#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002070 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002071 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002072
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002073final_norm:
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002074#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002075 /*
2076 * Knowledge of the jacobian coordinates may leak the last few bits of the
2077 * scalar [1], and since our MPI implementation isn't constant-flow,
2078 * inversion (used for coordinate normalization) may leak the full value
2079 * of its input via side-channels [2].
2080 *
2081 * [1] https://eprint.iacr.org/2003/191
2082 * [2] https://eprint.iacr.org/2020/055
2083 *
2084 * Avoid the leak by randomizing coordinates before we normalize them.
2085 */
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002086#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002087 if( f_rng != 0 )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002088#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002089 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
2090
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002091 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002092 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
2093
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002094#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02002095 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2096 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002097#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002098
2099cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002100 return( ret );
2101}
2102
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002103/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002104 * Pick window size based on curve size and whether we optimize for base point
2105 */
2106static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
2107 unsigned char p_eq_g )
2108{
2109 unsigned char w;
2110
2111 /*
2112 * Minimize the number of multiplications, that is minimize
2113 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2114 * (see costs of the various parts, with 1S = 1M)
2115 */
2116 w = grp->nbits >= 384 ? 5 : 4;
2117
2118 /*
2119 * If P == G, pre-compute a bit more, since this may be re-used later.
2120 * Just adding one avoids upping the cost of the first mul too much,
2121 * and the memory cost too.
2122 */
2123 if( p_eq_g )
2124 w++;
2125
2126 /*
2127 * Make sure w is within bounds.
2128 * (The last test is useful only for very small curves in the test suite.)
2129 */
2130 if( w > MBEDTLS_ECP_WINDOW_SIZE )
2131 w = MBEDTLS_ECP_WINDOW_SIZE;
2132 if( w >= grp->nbits )
2133 w = 2;
2134
2135 return( w );
2136}
2137
2138/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002139 * Multiplication using the comb method - for curves in short Weierstrass form
2140 *
2141 * This function is mainly responsible for administrative work:
2142 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002143 * - managing the table of precomputed points (passed between the below two
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002144 * functions): allocation, computation, ownership tranfer, freeing.
2145 *
2146 * It delegates the actual arithmetic work to:
2147 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2148 *
2149 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2152 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002153 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002154 void *p_rng,
2155 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002156{
2157 int ret;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002158 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002159 size_t d;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002160 unsigned char T_size = 0, T_ok = 0;
2161 mbedtls_ecp_point *T = NULL;
2162#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2163 ecp_drbg_context drbg_ctx;
2164
2165 ecp_drbg_init( &drbg_ctx );
2166#endif
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002167
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002168 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002169
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002170#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2171 if( f_rng == NULL )
2172 {
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +02002173 /* Adjust pointers */
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002174 f_rng = &ecp_drbg_random;
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +02002175#if defined(MBEDTLS_ECP_RESTARTABLE)
2176 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2177 p_rng = &rs_ctx->rsm->drbg_ctx;
2178 else
2179#endif
2180 p_rng = &drbg_ctx;
2181
2182 /* Initialize internal DRBG if necessary */
2183#if defined(MBEDTLS_ECP_RESTARTABLE)
2184 if( rs_ctx == NULL || rs_ctx->rsm == NULL ||
2185 rs_ctx->rsm->drbg_seeded == 0 )
2186#endif
2187 {
2188 MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m ) );
2189 }
2190#if defined(MBEDTLS_ECP_RESTARTABLE)
2191 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2192 rs_ctx->rsm->drbg_seeded = 1;
2193#endif
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002194 }
2195#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2196
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002197 /* Is P the base point ? */
2198#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2199 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
2200 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002201#else
2202 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002203#endif
2204
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002205 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002206 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002207 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002208 d = ( grp->nbits + w - 1 ) / w;
2209
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002210 /* Pre-computed table: do we have it already for the base point? */
2211 if( p_eq_g && grp->T != NULL )
2212 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002213 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002214 T = grp->T;
2215 T_ok = 1;
2216 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002217 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002218#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002219 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002220 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002221 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002222 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002223 T = rs_ctx->rsm->T;
2224 rs_ctx->rsm->T = NULL;
2225 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002226
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002227 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002228 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002229 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002230 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002231#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002232 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002233 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002234 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002235 if( T == NULL )
2236 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002237 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002238 goto cleanup;
2239 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002240
2241 for( i = 0; i < T_size; i++ )
2242 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002243
2244 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002245 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002246
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002247 /* Compute table (or finish computing it) if not done already */
2248 if( !T_ok )
2249 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002250 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002251
2252 if( p_eq_g )
2253 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002254 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002255 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002256 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002257 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002258 }
2259 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002260
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002261 /* Actual comb multiplication using precomputed points */
2262 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002263 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002264 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002265
2266cleanup:
2267
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002268#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2269 ecp_drbg_free( &drbg_ctx );
2270#endif
2271
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002272 /* does T belong to the group? */
2273 if( T == grp->T )
2274 T = NULL;
2275
2276 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002277#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002278 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 +01002279 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002280 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002281 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002282 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002283 T = NULL;
2284 }
2285#endif
2286
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002287 /* did T belong to us? then let's destroy it! */
2288 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002289 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002290 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 mbedtls_ecp_point_free( &T[i] );
2292 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002293 }
2294
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002295 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002296#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002297 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2298#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002299 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01002300 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002302
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002303 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002304
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002305 return( ret );
2306}
2307
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002308#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002309
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002310#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002311/*
2312 * For Montgomery curves, we do all the internal arithmetic in projective
2313 * coordinates. Import/export of points uses only the x coordinates, which is
2314 * internaly represented as X / Z.
2315 *
2316 * For scalar multiplication, we'll use a Montgomery ladder.
2317 */
2318
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002319/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002320 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2321 * Cost: 1M + 1I
2322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002324{
2325 int ret;
2326
Janos Follathb0697532016-08-18 12:38:46 +01002327#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002328 if( mbedtls_internal_ecp_grp_capable( grp ) )
2329 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002330#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
2333 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
2334 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002335
2336cleanup:
2337 return( ret );
2338}
2339
2340/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002341 * Randomize projective x/z coordinates:
2342 * (X, Z) -> (l X, l Z) for random l
2343 * This is sort of the reverse operation of ecp_normalize_mxz().
2344 *
2345 * This countermeasure was first suggested in [2].
2346 * Cost: 2M
2347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002349 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2350{
2351 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 mbedtls_mpi l;
Janos Follathb0697532016-08-18 12:38:46 +01002353 size_t p_size;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002354 int count = 0;
2355
Janos Follathb0697532016-08-18 12:38:46 +01002356#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002357 if( mbedtls_internal_ecp_grp_capable( grp ) )
2358 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Janos Follath372697b2016-10-28 16:53:11 +01002359#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002360
2361 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002363
2364 /* Generate l such that 1 < l < p */
2365 do
2366 {
Ron Eldorca6ff582017-01-12 14:50:50 +02002367 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002371
2372 if( count++ > 10 )
Jonas6645fd32020-05-08 16:57:18 +09002373 {
2374 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
2375 goto cleanup;
2376 }
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
2381 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 +01002382
2383cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002385
2386 return( ret );
2387}
2388
2389/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002390 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2391 * for Montgomery curves in x/z coordinates.
2392 *
2393 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2394 * with
2395 * d = X1
2396 * P = (X2, Z2)
2397 * Q = (X3, Z3)
2398 * R = (X4, Z4)
2399 * S = (X5, Z5)
2400 * and eliminating temporary variables tO, ..., t4.
2401 *
2402 * Cost: 5M + 4S
2403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2405 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2406 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2407 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002408{
2409 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002411
Janos Follathb0697532016-08-18 12:38:46 +01002412#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002413 if( mbedtls_internal_ecp_grp_capable( grp ) )
2414 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002415#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2418 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2419 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
2422 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
2423 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
2424 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
2425 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
2429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
2430 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
2431 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
2432 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
2433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
2435 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
2436 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
2437 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
2438 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 +01002439
2440cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2442 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2443 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002444
2445 return( ret );
2446}
2447
2448/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002449 * Multiplication with Montgomery ladder in x/z coordinates,
2450 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2453 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002454 int (*f_rng)(void *, unsigned char *, size_t),
2455 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002456{
2457 int ret;
2458 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002459 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_ecp_point RP;
2461 mbedtls_mpi PX;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002462#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2463 ecp_drbg_context drbg_ctx;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002464
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002465 ecp_drbg_init( &drbg_ctx );
2466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002468
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002469#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2470 if( f_rng == NULL )
2471 {
2472 MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) );
2473 f_rng = &ecp_drbg_random;
2474 p_rng = &drbg_ctx;
2475 }
2476#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2477
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002478 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2480 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002481
2482 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2484 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2485 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002486
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002487 /* RP.X might be sligtly larger than P, so reduce it */
2488 MOD_ADD( RP.X );
2489
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002490 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002491#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002492 if( f_rng != NULL )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002495
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002496 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002497 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002498 while( i-- > 0 )
2499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002501 /*
2502 * if (b) R = 2R + P else R = 2R,
2503 * which is:
2504 * if (b) double_add( RP, R, RP, R )
2505 * else double_add( R, RP, R, RP )
2506 * but using safe conditional swaps to avoid leaks
2507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2509 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2510 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2511 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2512 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002513 }
2514
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002515 /*
2516 * Knowledge of the projective coordinates may leak the last few bits of the
2517 * scalar [1], and since our MPI implementation isn't constant-flow,
2518 * inversion (used for coordinate normalization) may leak the full value
2519 * of its input via side-channels [2].
2520 *
2521 * [1] https://eprint.iacr.org/2003/191
2522 * [2] https://eprint.iacr.org/2020/055
2523 *
2524 * Avoid the leak by randomizing coordinates before we normalize them.
2525 */
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002526#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002527 if( f_rng != NULL )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002528#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002529 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
2530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002532
2533cleanup:
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002534#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2535 ecp_drbg_free( &drbg_ctx );
2536#endif
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002539
2540 return( ret );
2541}
2542
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002543#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002544
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002545/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002546 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002547 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002548int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002550 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2551 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002552{
Janos Follathb0697532016-08-18 12:38:46 +01002553 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002554#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2555 char is_grp_capable = 0;
2556#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002557 ECP_VALIDATE_RET( grp != NULL );
2558 ECP_VALIDATE_RET( R != NULL );
2559 ECP_VALIDATE_RET( m != NULL );
2560 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002561
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002562#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002563 /* reset ops count for this call if top-level */
2564 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2565 rs_ctx->ops_done = 0;
2566#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002567
Janos Follathc44ab972016-11-18 16:38:23 +00002568#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002569 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002570 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002571#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002572
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002573#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002574 /* skip argument check when restarting */
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002575 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002576#endif
2577 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002578 /* check_privkey is free */
2579 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2580
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002581 /* Common sanity checks */
2582 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2583 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002584 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002585
2586 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002587#if defined(ECP_MONTGOMERY)
2588 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002589 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002590#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002591#if defined(ECP_SHORTWEIERSTRASS)
2592 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002593 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002594#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002595
Janos Follath6c8ccd52016-11-29 15:37:09 +00002596cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002597
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002598#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002599 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002600 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002601#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002602
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002603#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002604 if( rs_ctx != NULL )
2605 rs_ctx->depth--;
2606#endif
2607
Janos Follathb0697532016-08-18 12:38:46 +01002608 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002609}
2610
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002611/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002612 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002613 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002614int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002615 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002616 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002617{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002618 ECP_VALIDATE_RET( grp != NULL );
2619 ECP_VALIDATE_RET( R != NULL );
2620 ECP_VALIDATE_RET( m != NULL );
2621 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002622 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002623}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002624
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002625#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002626/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002627 * Check that an affine point is valid as a public key,
2628 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002631{
2632 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002634
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002635 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2637 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2638 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2639 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2640 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002643
2644 /*
2645 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002646 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
2649 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002650
2651 /* Special case for A = -3 */
2652 if( grp->A.p == NULL )
2653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002655 }
2656 else
2657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002659 }
2660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
2662 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2665 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002666
2667cleanup:
2668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002670
2671 return( ret );
2672}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002673#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002674
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002675/*
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002676 * R = m * P with shortcuts for m == 1 and m == -1
2677 * NOT constant-time - ONLY for short Weierstrass!
2678 */
2679static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2680 mbedtls_ecp_point *R,
2681 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002682 const mbedtls_ecp_point *P,
2683 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002684{
2685 int ret;
2686
2687 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
2688 {
2689 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2690 }
2691 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2692 {
2693 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2694 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2695 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2696 }
2697 else
2698 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002699 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2700 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002701 }
2702
2703cleanup:
2704 return( ret );
2705}
2706
2707/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002708 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002709 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002710 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002711int mbedtls_ecp_muladd_restartable(
2712 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002713 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002714 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2715 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002716{
2717 int ret;
2718 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002719 mbedtls_ecp_point *pmP = &mP;
2720 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002721#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2722 char is_grp_capable = 0;
2723#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002724 ECP_VALIDATE_RET( grp != NULL );
2725 ECP_VALIDATE_RET( R != NULL );
2726 ECP_VALIDATE_RET( m != NULL );
2727 ECP_VALIDATE_RET( P != NULL );
2728 ECP_VALIDATE_RET( n != NULL );
2729 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002730
2731 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
2732 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2733
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002734 mbedtls_ecp_point_init( &mP );
2735
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002736 ECP_RS_ENTER( ma );
2737
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002738#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002739 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2740 {
2741 /* redirect intermediate results to restart context */
2742 pmP = &rs_ctx->ma->mP;
2743 pR = &rs_ctx->ma->R;
2744
2745 /* jump to next operation */
2746 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2747 goto mul2;
2748 if( rs_ctx->ma->state == ecp_rsma_add )
2749 goto add;
2750 if( rs_ctx->ma->state == ecp_rsma_norm )
2751 goto norm;
2752 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002753#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002754
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002755 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002756#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002757 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002758 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002759
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002760mul2:
2761#endif
2762 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Janos Follathaf6f2692018-12-07 09:55:24 +00002763
2764#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2765 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2766 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2767#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2768
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_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002772
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002773add:
2774#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002775 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002776 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002777#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002778 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002779 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002780
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002781norm:
2782#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002783 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002784 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2785
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002786#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002787 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2788 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2789#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002790
2791cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002792#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002793 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002794 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002795#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002796
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002797 mbedtls_ecp_point_free( &mP );
2798
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002799 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002800
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002801 return( ret );
2802}
2803
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002804/*
2805 * Linear combination
2806 * NOT constant-time
2807 */
2808int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2809 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2810 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2811{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002812 ECP_VALIDATE_RET( grp != NULL );
2813 ECP_VALIDATE_RET( R != NULL );
2814 ECP_VALIDATE_RET( m != NULL );
2815 ECP_VALIDATE_RET( P != NULL );
2816 ECP_VALIDATE_RET( n != NULL );
2817 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002818 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2819}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002820
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002821#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002822/*
2823 * Check validity of a public key for Montgomery curves with x-only schemes
2824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002826{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002827 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002828 /* Allow any public value, if it's too big then we'll just reduce it mod p
2829 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2831 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002832
2833 return( 0 );
2834}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002835#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002836
2837/*
2838 * Check that a point is valid as a public key
2839 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002840int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
2841 const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002842{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002843 ECP_VALIDATE_RET( grp != NULL );
2844 ECP_VALIDATE_RET( pt != NULL );
2845
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002846 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2848 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002849
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002850#if defined(ECP_MONTGOMERY)
2851 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002852 return( ecp_check_pubkey_mx( grp, pt ) );
2853#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002854#if defined(ECP_SHORTWEIERSTRASS)
2855 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002856 return( ecp_check_pubkey_sw( grp, pt ) );
2857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002859}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002860
2861/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002863 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002864int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
2865 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002866{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002867 ECP_VALIDATE_RET( grp != NULL );
2868 ECP_VALIDATE_RET( d != NULL );
2869
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002870#if defined(ECP_MONTGOMERY)
2871 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002872 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002873 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2875 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002876 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002878
2879 /* see [Curve25519] page 5 */
2880 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
2881 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2882
2883 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002884 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002885#endif /* ECP_MONTGOMERY */
2886#if defined(ECP_SHORTWEIERSTRASS)
2887 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002888 {
2889 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2891 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2892 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002893 else
2894 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002895 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002896#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002899}
2900
2901/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002902 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002903 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002904int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
2905 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002906 int (*f_rng)(void *, unsigned char *, size_t),
2907 void *p_rng )
2908{
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002909 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002910 size_t n_size;
2911
2912 ECP_VALIDATE_RET( grp != NULL );
2913 ECP_VALIDATE_RET( d != NULL );
2914 ECP_VALIDATE_RET( f_rng != NULL );
2915
2916 n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002917
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002918#if defined(ECP_MONTGOMERY)
2919 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002920 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002921 /* [M225] page 5 */
2922 size_t b;
2923
Janos Follath98e28a72016-05-31 14:03:54 +01002924 do {
2925 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
2926 } while( mbedtls_mpi_bitlen( d ) == 0);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002927
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002928 /* Make sure the most significant bit is nbits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002929 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002930 if( b > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002934
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002935 /* Make sure the last two bits are unset for Curve448, three bits for
2936 Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
2938 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002939 if( grp->nbits == 254 )
2940 {
2941 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
2942 }
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002943 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002944#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002945
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002946#if defined(ECP_SHORTWEIERSTRASS)
2947 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002948 {
2949 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002950 int count = 0;
Janos Follath867a3ab2019-10-11 14:21:53 +01002951 unsigned cmp = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01002952
2953 /*
2954 * Match the procedure given in RFC 6979 (deterministic ECDSA):
2955 * - use the same byte ordering;
2956 * - keep the leftmost nbits bits of the generated octet string;
2957 * - try until result is in the desired range.
2958 * This also avoids any biais, which is especially important for ECDSA.
2959 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002960 do
2961 {
Hanno Becker7c8cb9c2017-10-17 15:19:38 +01002962 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002964
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01002965 /*
2966 * Each try has at worst a probability 1/2 of failing (the msb has
2967 * a probability 1/2 of being 0, and then the result will be < N),
2968 * so after 30 tries failure probability is a most 2**(-30).
2969 *
2970 * For most curves, 1 try is enough with overwhelming probability,
2971 * since N starts with a lot of 1s in binary, but some curves
2972 * such as secp224k1 are actually very close to the worst case.
2973 */
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02002974 if( ++count > 30 )
2975 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Janos Follath4c3408b2019-09-16 14:27:39 +01002976
Janos Follath867a3ab2019-10-11 14:21:53 +01002977 ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
Janos Follath4c3408b2019-09-16 14:27:39 +01002978 if( ret != 0 )
2979 {
2980 goto cleanup;
2981 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002982 }
Janos Follath867a3ab2019-10-11 14:21:53 +01002983 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002984 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002985#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002986
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002987cleanup:
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002988 return( ret );
2989}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002990
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002991/*
2992 * Generate a keypair with configurable base point
2993 */
2994int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
2995 const mbedtls_ecp_point *G,
2996 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2997 int (*f_rng)(void *, unsigned char *, size_t),
2998 void *p_rng )
2999{
3000 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003001 ECP_VALIDATE_RET( grp != NULL );
3002 ECP_VALIDATE_RET( d != NULL );
3003 ECP_VALIDATE_RET( G != NULL );
3004 ECP_VALIDATE_RET( Q != NULL );
3005 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003006
3007 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
3008 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
3009
3010cleanup:
3011 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003012}
3013
3014/*
3015 * Generate key pair, wrapper for conventional base point
3016 */
3017int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
3018 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3019 int (*f_rng)(void *, unsigned char *, size_t),
3020 void *p_rng )
3021{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003022 ECP_VALIDATE_RET( grp != NULL );
3023 ECP_VALIDATE_RET( d != NULL );
3024 ECP_VALIDATE_RET( Q != NULL );
3025 ECP_VALIDATE_RET( f_rng != NULL );
3026
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003027 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003028}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003029
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003030/*
3031 * Generate a keypair, prettier wrapper
3032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003034 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
3035{
3036 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003037 ECP_VALIDATE_RET( key != NULL );
3038 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003039
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003040 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003041 return( ret );
3042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003044}
3045
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003046/*
3047 * Check a public-private key pair
3048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003050{
3051 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 mbedtls_ecp_point Q;
3053 mbedtls_ecp_group grp;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003054 ECP_VALIDATE_RET( pub != NULL );
3055 ECP_VALIDATE_RET( prv != NULL );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003058 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
3060 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
3061 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003064 }
3065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 mbedtls_ecp_point_init( &Q );
3067 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 /* mbedtls_ecp_mul() needs a non-const group... */
3070 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003071
3072 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
3076 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
3077 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003080 goto cleanup;
3081 }
3082
3083cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 mbedtls_ecp_point_free( &Q );
3085 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003086
3087 return( ret );
3088}
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003091
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003092/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003093 * Checkup routine
3094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003096{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003097 int ret;
3098 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 mbedtls_ecp_group grp;
3100 mbedtls_ecp_point R, P;
3101 mbedtls_mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003102 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003103 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003104 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003105 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003106 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003107 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003108 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003109 "400000000000000000000000000000000000000000000000", /* one and zeros */
3110 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3111 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003112 };
3113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 mbedtls_ecp_group_init( &grp );
3115 mbedtls_ecp_point_init( &R );
3116 mbedtls_ecp_point_init( &P );
3117 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003118
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003119 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003121 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003122#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003123 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003124#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003125
3126 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003128
3129 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
3131 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003132
3133 add_count = 0;
3134 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003135 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3137 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003138
3139 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3140 {
3141 add_c_prev = add_count;
3142 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003143 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003144 add_count = 0;
3145 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003146 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3149 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003150
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003151 if( add_count != add_c_prev ||
3152 dbl_count != dbl_c_prev ||
3153 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003154 {
3155 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003157
3158 ret = 1;
3159 goto cleanup;
3160 }
3161 }
3162
3163 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003165
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003166 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003168 /* We computed P = 2G last time, use it */
3169
3170 add_count = 0;
3171 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003172 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3174 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003175
3176 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3177 {
3178 add_c_prev = add_count;
3179 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003180 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003181 add_count = 0;
3182 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003183 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3186 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003187
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003188 if( add_count != add_c_prev ||
3189 dbl_count != dbl_c_prev ||
3190 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003191 {
3192 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003194
3195 ret = 1;
3196 goto cleanup;
3197 }
3198 }
3199
3200 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003202
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003203cleanup:
3204
3205 if( ret < 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 mbedtls_ecp_group_free( &grp );
3209 mbedtls_ecp_point_free( &R );
3210 mbedtls_ecp_point_free( &P );
3211 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003212
3213 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003215
3216 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003217}
3218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003220
Janos Follathb0697532016-08-18 12:38:46 +01003221#endif /* !MBEDTLS_ECP_ALT */
3222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#endif /* MBEDTLS_ECP_C */