blob: ad19e05fb28d799692f7fb06118f32f9aed02d20 [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
20/*
21 * References:
22 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010023 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010024 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010025 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010026 * RFC 4492 for the related TLS structures and constants
Nicholas Wilson08f3ef12015-11-10 13:10:01 +000027 * RFC 7748 for the Curve448 and Curve25519 curve definitions
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020028 *
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020029 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010030 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020031 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020032 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
33 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
34 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010035 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020036 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010037 * render ECC resistant against Side Channel Attacks. IACR Cryptology
38 * ePrint Archive, 2004, vol. 2004, p. 342.
39 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010040 */
41
Gilles Peskinedb09ef62020-06-03 01:43:33 +020042#include "common.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010043
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044/**
45 * \brief Function level alternative implementation.
46 *
47 * The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
48 * replace certain functions in this module. The alternative implementations are
49 * typically hardware accelerators and need to activate the hardware before the
50 * computation starts and deactivate it after it finishes. The
51 * mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
52 * this purpose.
53 *
54 * To preserve the correct functionality the following conditions must hold:
55 *
56 * - The alternative implementation must be activated by
57 * mbedtls_internal_ecp_init() before any of the replaceable functions is
58 * called.
59 * - mbedtls_internal_ecp_free() must \b only be called when the alternative
60 * implementation is activated.
61 * - mbedtls_internal_ecp_init() must \b not be called when the alternative
62 * implementation is activated.
63 * - Public functions must not return while the alternative implementation is
64 * activated.
65 * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
66 * before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
67 * \endcode ensures that the alternative implementation supports the current
68 * group.
69 */
70#if defined(MBEDTLS_ECP_INTERNAL_ALT)
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/ecp.h"
Janos Follath430d3372016-11-03 14:25:37 +000076#include "mbedtls/threading.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Janos Follathbc96a792021-06-24 14:48:38 +010079#include "mbedtls/bn_mul.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020080
Gilles Peskine80ba8502021-04-03 20:36:37 +020081#include "ecp_invasive.h"
82
Rich Evans00ab4702015-02-06 13:43:58 +000083#include <string.h>
84
Janos Follathb0697532016-08-18 12:38:46 +010085#if !defined(MBEDTLS_ECP_ALT)
86
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087/* Parameter validation macros based on platform_util.h */
88#define ECP_VALIDATE_RET( cond ) \
89 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
90#define ECP_VALIDATE( cond ) \
91 MBEDTLS_INTERNAL_VALIDATE( cond )
92
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000093#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020094
Janos Follath47d28f02016-11-01 13:22:05 +000095#include "mbedtls/ecp_internal.h"
Janos Follathb0697532016-08-18 12:38:46 +010096
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +020097#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +020098#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +020099#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200100#elif defined(MBEDTLS_CTR_DRBG_C)
101#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200102#else
103#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
104#endif
105#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100108/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100109 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200110 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100111 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100112static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100113#endif
114
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200115#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
116/*
117 * Currently ecp_mul() takes a RNG function as an argument, used for
Manuel Pégourié-Gonnard25705e62020-06-10 09:18:25 +0200118 * side-channel protection, but it can be NULL. The initial reasoning was
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200119 * that people will pass non-NULL RNG when they care about side-channels, but
120 * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with
121 * no opportunity for the user to do anything about it.
122 *
123 * The obvious strategies for addressing that include:
124 * - change those APIs so that they take RNG arguments;
125 * - require a global RNG to be available to all crypto modules.
126 *
127 * Unfortunately those would break compatibility. So what we do instead is
128 * have our own internal DRBG instance, seeded from the secret scalar.
129 *
130 * The following is a light-weight abstraction layer for doing that with
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200131 * HMAC_DRBG (first choice) or CTR_DRBG.
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200132 */
133
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200134#if defined(MBEDTLS_HMAC_DRBG_C)
135
136/* DRBG context type */
137typedef mbedtls_hmac_drbg_context ecp_drbg_context;
138
139/* DRBG context init */
140static inline void ecp_drbg_init( ecp_drbg_context *ctx )
141{
142 mbedtls_hmac_drbg_init( ctx );
143}
144
145/* DRBG context free */
146static inline void ecp_drbg_free( ecp_drbg_context *ctx )
147{
148 mbedtls_hmac_drbg_free( ctx );
149}
150
151/* DRBG function */
152static inline int ecp_drbg_random( void *p_rng,
153 unsigned char *output, size_t output_len )
154{
155 return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
156}
157
158/* DRBG context seeding */
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200159static int ecp_drbg_seed( ecp_drbg_context *ctx,
160 const mbedtls_mpi *secret, size_t secret_len )
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200161{
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200162 int ret;
163 unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200164 /* The list starts with strong hashes */
165 const mbedtls_md_type_t md_type = mbedtls_md_list()[0];
166 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type );
167
Manuel Pégourié-Gonnard1215c542020-06-19 11:59:49 +0200168 if( secret_len > MBEDTLS_ECP_MAX_BYTES )
169 {
170 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
171 goto cleanup;
172 }
173
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200174 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret,
175 secret_bytes, secret_len ) );
176
177 ret = mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_bytes, secret_len );
178
179cleanup:
180 mbedtls_platform_zeroize( secret_bytes, secret_len );
181
182 return( ret );
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200183}
184
185#elif defined(MBEDTLS_CTR_DRBG_C)
186
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200187/* DRBG context type */
188typedef mbedtls_ctr_drbg_context ecp_drbg_context;
189
190/* DRBG context init */
191static inline void ecp_drbg_init( ecp_drbg_context *ctx )
192{
193 mbedtls_ctr_drbg_init( ctx );
194}
195
196/* DRBG context free */
197static inline void ecp_drbg_free( ecp_drbg_context *ctx )
198{
199 mbedtls_ctr_drbg_free( ctx );
200}
201
202/* DRBG function */
203static inline int ecp_drbg_random( void *p_rng,
204 unsigned char *output, size_t output_len )
205{
206 return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
207}
208
209/*
210 * Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does,
211 * we need to pass an entropy function when seeding. So we use a dummy
212 * function for that, and pass the actual entropy as customisation string.
213 * (During seeding of CTR_DRBG the entropy input and customisation string are
214 * concatenated before being used to update the secret state.)
215 */
216static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
217{
218 (void) ctx;
219 memset( out, 0, len );
220 return( 0 );
221}
222
223/* DRBG context seeding */
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200224static int ecp_drbg_seed( ecp_drbg_context *ctx,
225 const mbedtls_mpi *secret, size_t secret_len )
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200226{
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200227 int ret;
228 unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200229
Manuel Pégourié-Gonnard1215c542020-06-19 11:59:49 +0200230 if( secret_len > MBEDTLS_ECP_MAX_BYTES )
231 {
232 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
233 goto cleanup;
234 }
235
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200236 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret,
237 secret_bytes, secret_len ) );
238
239 ret = mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL,
240 secret_bytes, secret_len );
241
242cleanup:
243 mbedtls_platform_zeroize( secret_bytes, secret_len );
244
245 return( ret );
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200246}
247
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200248#else
249#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
250#endif /* DRBG modules */
251#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
252
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200253#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100254/*
255 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200256 *
257 * Default value 0 means that ECC operations will not yield.
258 * Note that regardless of the value of ecp_max_ops, always at
259 * least one step is performed before yielding.
260 *
261 * Setting ecp_max_ops=1 can be suitable for testing purposes
262 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100263 */
264static unsigned ecp_max_ops = 0;
265
266/*
267 * Set ecp_max_ops
268 */
269void mbedtls_ecp_set_max_ops( unsigned max_ops )
270{
271 ecp_max_ops = max_ops;
272}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100273
274/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200275 * Check if restart is enabled
276 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200277int mbedtls_ecp_restart_is_enabled( void )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200278{
279 return( ecp_max_ops != 0 );
280}
281
282/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200283 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100284 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200285struct mbedtls_ecp_restart_mul
286{
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100287 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100288 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100289 mbedtls_ecp_point *T; /* table for precomputed points */
290 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200291 enum { /* what were we doing last time we returned? */
292 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
293 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100294 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
295 ecp_rsm_pre_add, /* precompute remaining points by adding */
296 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200297 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100298 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100299 } state;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200300#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
301 ecp_drbg_context drbg_ctx;
302 unsigned char drbg_seeded;
303#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100304};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100305
306/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200307 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100308 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200309static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100310{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200311 mbedtls_ecp_point_init( &ctx->R );
312 ctx->i = 0;
313 ctx->T = NULL;
314 ctx->T_size = 0;
315 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200316#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
317 ecp_drbg_init( &ctx->drbg_ctx );
318 ctx->drbg_seeded = 0;
319#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100320}
321
322/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200323 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100324 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200325static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100326{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100327 unsigned char i;
328
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100329 if( ctx == NULL )
330 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100331
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100332 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100333
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200334 if( ctx->T != NULL )
335 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100336 for( i = 0; i < ctx->T_size; i++ )
337 mbedtls_ecp_point_free( ctx->T + i );
338 mbedtls_free( ctx->T );
339 }
340
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200341#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
342 ecp_drbg_free( &ctx->drbg_ctx );
343#endif
344
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200345 ecp_restart_rsm_init( ctx );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100346}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100347
348/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200349 * Restart context for ecp_muladd()
350 */
351struct mbedtls_ecp_restart_muladd
352{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200353 mbedtls_ecp_point mP; /* mP value */
354 mbedtls_ecp_point R; /* R intermediate result */
355 enum { /* what should we do next? */
356 ecp_rsma_mul1 = 0, /* first multiplication */
357 ecp_rsma_mul2, /* second multiplication */
358 ecp_rsma_add, /* addition */
359 ecp_rsma_norm, /* normalization */
360 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200361};
362
363/*
364 * Init restart_muladd sub-context
365 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200366static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200367{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200368 mbedtls_ecp_point_init( &ctx->mP );
369 mbedtls_ecp_point_init( &ctx->R );
370 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200371}
372
373/*
374 * Free the components of a restart_muladd sub-context
375 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200376static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200377{
378 if( ctx == NULL )
379 return;
380
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200381 mbedtls_ecp_point_free( &ctx->mP );
382 mbedtls_ecp_point_free( &ctx->R );
383
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200384 ecp_restart_ma_init( ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200385}
386
387/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200388 * Initialize a restart context
389 */
390void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
391{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500392 ECP_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200393 ctx->ops_done = 0;
394 ctx->depth = 0;
395 ctx->rsm = NULL;
396 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200397}
398
399/*
400 * Free the components of a restart context
401 */
402void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
403{
404 if( ctx == NULL )
405 return;
406
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200407 ecp_restart_rsm_free( ctx->rsm );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200408 mbedtls_free( ctx->rsm );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200409
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200410 ecp_restart_ma_free( ctx->ma );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200411 mbedtls_free( ctx->ma );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200412
413 mbedtls_ecp_restart_init( ctx );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200414}
415
416/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100417 * Check if we can do the next step
418 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200419int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
420 mbedtls_ecp_restart_ctx *rs_ctx,
421 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100422{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 ECP_VALIDATE_RET( grp != NULL );
424
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200425 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100426 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100427 /* scale depending on curve size: the chosen reference is 256-bit,
428 * and multiplication is quadratic. Round to the closest integer. */
429 if( grp->pbits >= 512 )
430 ops *= 4;
431 else if( grp->pbits >= 384 )
432 ops *= 2;
433
Hanno Beckerb10c6602018-10-26 13:50:13 +0100434 /* Avoid infinite loops: always allow first step.
435 * Because of that, however, it's not generally true
436 * that ops_done <= ecp_max_ops, so the check
437 * ops_done > ecp_max_ops below is mandatory. */
438 if( ( rs_ctx->ops_done != 0 ) &&
439 ( rs_ctx->ops_done > ecp_max_ops ||
440 ops > ecp_max_ops - rs_ctx->ops_done ) )
441 {
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100442 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
Hanno Beckerb10c6602018-10-26 13:50:13 +0100443 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100444
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100445 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200446 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100447 }
448
449 return( 0 );
450}
451
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200452/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200453#define ECP_RS_ENTER( SUB ) do { \
454 /* reset ops count for this call if top-level */ \
455 if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
456 rs_ctx->ops_done = 0; \
457 \
458 /* set up our own sub-context if needed */ \
459 if( mbedtls_ecp_restart_is_enabled() && \
460 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
461 { \
462 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
463 if( rs_ctx->SUB == NULL ) \
464 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
465 \
466 ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
467 } \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200468} while( 0 )
469
470/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200471#define ECP_RS_LEAVE( SUB ) do { \
472 /* clear our sub-context when not in progress (done or error) */ \
473 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
474 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
475 { \
476 ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
477 mbedtls_free( rs_ctx->SUB ); \
478 rs_ctx->SUB = NULL; \
479 } \
480 \
481 if( rs_ctx != NULL ) \
482 rs_ctx->depth--; \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200483} while( 0 )
484
485#else /* MBEDTLS_ECP_RESTARTABLE */
486
487#define ECP_RS_ENTER( sub ) (void) rs_ctx;
488#define ECP_RS_LEAVE( sub ) (void) rs_ctx;
489
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200490#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100491
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100492/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200493 * List of supported curves:
494 * - internal ID
Christoph M. Wintersteigercb310732019-02-15 15:50:38 +0000495 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200496 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200497 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100498 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100499 * Curves are listed in order: largest curves first, and for a given size,
500 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200501 *
Gilles Peskine1174db52020-02-26 19:52:06 +0100502 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
507 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
510 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
513 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
516 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
519 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
522 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
525 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
528 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
531 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
534 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
537 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100538#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200539#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Christoph M. Wintersteiger86e36c42018-12-06 17:27:31 +0000540 { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100541#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200542#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
543 { MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200546};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100547
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200548#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
549 sizeof( ecp_supported_curves[0] )
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200552
553/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200554 * List of supported curves and associated info
555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200557{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200558 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200559}
560
561/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100562 * List of supported curves, group ID only
563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100565{
566 static int init_done = 0;
567
568 if( ! init_done )
569 {
570 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 for( curve_info = mbedtls_ecp_curve_list();
574 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100575 curve_info++ )
576 {
577 ecp_supported_grp_id[i++] = curve_info->grp_id;
578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100580
581 init_done = 1;
582 }
583
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200584 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100585}
586
587/*
588 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590const 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 +0200591{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 for( curve_info = mbedtls_ecp_curve_list();
595 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200596 curve_info++ )
597 {
598 if( curve_info->grp_id == grp_id )
599 return( curve_info );
600 }
601
602 return( NULL );
603}
604
605/*
606 * Get the curve info from the TLS identifier
607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 for( curve_info = mbedtls_ecp_curve_list();
613 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200614 curve_info++ )
615 {
616 if( curve_info->tls_id == tls_id )
617 return( curve_info );
618 }
619
620 return( NULL );
621}
622
623/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100624 * Get the curve info from the name
625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100629
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500630 if( name == NULL )
631 return( NULL );
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 for( curve_info = mbedtls_ecp_curve_list();
634 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100635 curve_info++ )
636 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200637 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100638 return( curve_info );
639 }
640
641 return( NULL );
642}
643
644/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100645 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100646 */
Janos Follathdf9295b2019-02-26 12:36:52 +0000647mbedtls_ecp_curve_type mbedtls_ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100648{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100649 if( grp->G.X.p == NULL )
Janos Follathdf9295b2019-02-26 12:36:52 +0000650 return( MBEDTLS_ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100651
652 if( grp->G.Y.p == NULL )
Janos Follathdf9295b2019-02-26 12:36:52 +0000653 return( MBEDTLS_ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100654 else
Janos Follathdf9295b2019-02-26 12:36:52 +0000655 return( MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100656}
657
658/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100659 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100662{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500663 ECP_VALIDATE( pt != NULL );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_mpi_init( &pt->X );
666 mbedtls_mpi_init( &pt->Y );
667 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100668}
669
670/*
671 * Initialize (the components of) a group
672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100674{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500675 ECP_VALIDATE( grp != NULL );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100676
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200677 grp->id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200678 mbedtls_mpi_init( &grp->P );
679 mbedtls_mpi_init( &grp->A );
680 mbedtls_mpi_init( &grp->B );
681 mbedtls_ecp_point_init( &grp->G );
682 mbedtls_mpi_init( &grp->N );
683 grp->pbits = 0;
684 grp->nbits = 0;
685 grp->h = 0;
686 grp->modp = NULL;
687 grp->t_pre = NULL;
688 grp->t_post = NULL;
689 grp->t_data = NULL;
690 grp->T = NULL;
691 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100692}
693
694/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200695 * Initialize (the components of) a key pair
696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200698{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500699 ECP_VALIDATE( key != NULL );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_ecp_group_init( &key->grp );
702 mbedtls_mpi_init( &key->d );
703 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200704}
705
706/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100707 * Unallocate (the components of) a point
708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100710{
711 if( pt == NULL )
712 return;
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_mpi_free( &( pt->X ) );
715 mbedtls_mpi_free( &( pt->Y ) );
716 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100717}
718
719/*
720 * Unallocate (the components of) a group
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100723{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200724 size_t i;
725
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100726 if( grp == NULL )
727 return;
728
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100729 if( grp->h != 1 )
730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_mpi_free( &grp->P );
732 mbedtls_mpi_free( &grp->A );
733 mbedtls_mpi_free( &grp->B );
734 mbedtls_ecp_point_free( &grp->G );
735 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100736 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200737
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200738 if( grp->T != NULL )
739 {
740 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_ecp_point_free( &grp->T[i] );
742 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200743 }
744
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500745 mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100746}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100747
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100748/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200749 * Unallocate (the components of) a key pair
750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200752{
Paul Bakker66d5d072014-06-17 16:39:18 +0200753 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200754 return;
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_ecp_group_free( &key->grp );
757 mbedtls_mpi_free( &key->d );
758 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200759}
760
761/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200762 * Copy the contents of a point
763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200765{
Janos Follath24eed8d2019-11-22 13:21:35 +0000766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500767 ECP_VALIDATE_RET( P != NULL );
768 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
771 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
772 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200773
774cleanup:
775 return( ret );
776}
777
778/*
779 * Copy the contents of a group object
780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200782{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500783 ECP_VALIDATE_RET( dst != NULL );
784 ECP_VALIDATE_RET( src != NULL );
785
786 return( mbedtls_ecp_group_load( dst, src->id ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200787}
788
789/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100790 * Set point to zero
791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100793{
Janos Follath24eed8d2019-11-22 13:21:35 +0000794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500795 ECP_VALIDATE_RET( pt != NULL );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
798 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
799 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100800
801cleanup:
802 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100803}
804
805/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100806 * Tell if a point is zero
807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100809{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500810 ECP_VALIDATE_RET( pt != NULL );
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100813}
814
815/*
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500816 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200817 */
818int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
819 const mbedtls_ecp_point *Q )
820{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500821 ECP_VALIDATE_RET( P != NULL );
822 ECP_VALIDATE_RET( Q != NULL );
823
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200824 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
825 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
826 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
827 {
828 return( 0 );
829 }
830
831 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
832}
833
834/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100835 * Import a non-zero point from ASCII strings
836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100838 const char *x, const char *y )
839{
Janos Follath24eed8d2019-11-22 13:21:35 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500841 ECP_VALIDATE_RET( P != NULL );
842 ECP_VALIDATE_RET( x != NULL );
843 ECP_VALIDATE_RET( y != NULL );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
847 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100848
849cleanup:
850 return( ret );
851}
852
853/*
Janos Follath7caf8e42019-02-20 12:00:22 +0000854 * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100855 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500856int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp,
857 const mbedtls_ecp_point *P,
858 int format, size_t *olen,
859 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100860{
Janos Follath28eb06d2019-02-26 10:53:34 +0000861 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100862 size_t plen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500863 ECP_VALIDATE_RET( grp != NULL );
864 ECP_VALIDATE_RET( P != NULL );
865 ECP_VALIDATE_RET( olen != NULL );
866 ECP_VALIDATE_RET( buf != NULL );
867 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
868 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100871
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200872#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine59970052019-02-28 13:12:06 +0100873 (void) format; /* Montgomery curves always use the same point format */
Janos Follathdf9295b2019-02-26 12:36:52 +0000874 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100875 {
Janos Follath7caf8e42019-02-20 12:00:22 +0000876 *olen = plen;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100877 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100879
Janos Follath7caf8e42019-02-20 12:00:22 +0000880 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary_le( &P->X, buf, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100881 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000882#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200883#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +0000884 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100885 {
Janos Follath7caf8e42019-02-20 12:00:22 +0000886 /*
887 * Common case: P == 0
888 */
889 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
890 {
891 if( buflen < 1 )
892 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100893
Janos Follath7caf8e42019-02-20 12:00:22 +0000894 buf[0] = 0x00;
895 *olen = 1;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100896
Janos Follath7caf8e42019-02-20 12:00:22 +0000897 return( 0 );
898 }
899
900 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
901 {
902 *olen = 2 * plen + 1;
903
904 if( buflen < *olen )
905 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
906
907 buf[0] = 0x04;
908 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
909 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
910 }
911 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
912 {
913 *olen = plen + 1;
914
915 if( buflen < *olen )
916 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
917
918 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
919 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
920 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100921 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000922#endif
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100923
924cleanup:
925 return( ret );
926}
927
928/*
Janos Follath59b813c2019-02-13 10:44:06 +0000929 * Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100930 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500931int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
932 mbedtls_ecp_point *pt,
933 const unsigned char *buf, size_t ilen )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100934{
Janos Follath28eb06d2019-02-26 10:53:34 +0000935 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100936 size_t plen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500937 ECP_VALIDATE_RET( grp != NULL );
938 ECP_VALIDATE_RET( pt != NULL );
939 ECP_VALIDATE_RET( buf != NULL );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100940
Paul Bakker82788fb2014-10-20 13:59:19 +0200941 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100945
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200946#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +0000947 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follath59b813c2019-02-13 10:44:06 +0000948 {
949 if( plen != ilen )
950 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100951
Janos Follath59b813c2019-02-13 10:44:06 +0000952 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary_le( &pt->X, buf, plen ) );
953 mbedtls_mpi_free( &pt->Y );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100954
Janos Follath59b813c2019-02-13 10:44:06 +0000955 if( grp->id == MBEDTLS_ECP_DP_CURVE25519 )
Janos Follathffbd7e82019-02-25 11:35:20 +0000956 /* Set most significant bit to 0 as prescribed in RFC7748 §5 */
Janos Follath59b813c2019-02-13 10:44:06 +0000957 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &pt->X, plen * 8 - 1, 0 ) );
Janos Follath28eb06d2019-02-26 10:53:34 +0000958
959 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Janos Follath59b813c2019-02-13 10:44:06 +0000960 }
961#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200962#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +0000963 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Janos Follath59b813c2019-02-13 10:44:06 +0000964 {
965 if( buf[0] == 0x00 )
966 {
967 if( ilen == 1 )
968 return( mbedtls_ecp_set_zero( pt ) );
969 else
970 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
971 }
972
973 if( buf[0] != 0x04 )
974 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
975
976 if( ilen != 2 * plen + 1 )
977 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
978
979 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
980 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y,
981 buf + 1 + plen, plen ) );
Janos Follath28eb06d2019-02-26 10:53:34 +0000982 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Janos Follath59b813c2019-02-13 10:44:06 +0000983 }
984#endif
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100985
986cleanup:
987 return( ret );
988}
989
990/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100991 * Import a point from a TLS ECPoint record (RFC 4492)
992 * struct {
993 * opaque point <1..2^8-1>;
994 * } ECPoint;
995 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500996int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
997 mbedtls_ecp_point *pt,
998 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100999{
1000 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001001 const unsigned char *buf_start;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001002 ECP_VALIDATE_RET( grp != NULL );
1003 ECP_VALIDATE_RET( pt != NULL );
1004 ECP_VALIDATE_RET( buf != NULL );
1005 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001006
1007 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +02001008 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001009 */
1010 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001012
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001013 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001014 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001016
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001017 /*
1018 * Save buffer start for read_binary and update buf
1019 */
1020 buf_start = *buf;
1021 *buf += data_len;
1022
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001023 return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001024}
1025
1026/*
1027 * Export a point as a TLS ECPoint record (RFC 4492)
1028 * struct {
1029 * opaque point <1..2^8-1>;
1030 * } ECPoint;
1031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001033 int format, size_t *olen,
1034 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001035{
Janos Follath24eed8d2019-11-22 13:21:35 +00001036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001037 ECP_VALIDATE_RET( grp != NULL );
1038 ECP_VALIDATE_RET( pt != NULL );
1039 ECP_VALIDATE_RET( olen != NULL );
1040 ECP_VALIDATE_RET( buf != NULL );
1041 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1042 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001043
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001044 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001045 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001046 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001047 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001051 olen, buf + 1, blen - 1) ) != 0 )
1052 return( ret );
1053
1054 /*
1055 * write length to the first byte and update total length
1056 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001057 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001058 ++*olen;
1059
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001060 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001061}
1062
1063/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001064 * Set a group from an ECParameters record (RFC 4492)
1065 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001066int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
1067 const unsigned char **buf, size_t len )
1068{
Janos Follath24eed8d2019-11-22 13:21:35 +00001069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001070 mbedtls_ecp_group_id grp_id;
1071 ECP_VALIDATE_RET( grp != NULL );
1072 ECP_VALIDATE_RET( buf != NULL );
1073 ECP_VALIDATE_RET( *buf != NULL );
1074
1075 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 )
1076 return( ret );
1077
1078 return( mbedtls_ecp_group_load( grp, grp_id ) );
1079}
1080
1081/*
1082 * Read a group id from an ECParameters record (RFC 4492) and convert it to
1083 * mbedtls_ecp_group_id.
1084 */
1085int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
1086 const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001087{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001088 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001090 ECP_VALIDATE_RET( grp != NULL );
1091 ECP_VALIDATE_RET( buf != NULL );
1092 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001093
1094 /*
1095 * We expect at least three bytes (see below)
1096 */
1097 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001099
1100 /*
1101 * First byte is curve_type; only named_curve is handled
1102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
1104 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001105
1106 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001107 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001108 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001109 tls_id = *(*buf)++;
1110 tls_id <<= 8;
1111 tls_id |= *(*buf)++;
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
1114 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001115
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001116 *grp = curve_info->grp_id;
1117
1118 return( 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001119}
1120
1121/*
1122 * Write the ECParameters record corresponding to a group (RFC 4492)
1123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001125 unsigned char *buf, size_t blen )
1126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001128 ECP_VALIDATE_RET( grp != NULL );
1129 ECP_VALIDATE_RET( buf != NULL );
1130 ECP_VALIDATE_RET( olen != NULL );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
1133 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001134
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001135 /*
1136 * We are going to write 3 bytes (see below)
1137 */
1138 *olen = 3;
1139 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001141
1142 /*
1143 * First byte is curve_type, always named_curve
1144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001146
1147 /*
1148 * Next two bytes are the namedcurve value
1149 */
Joe Subbianid3a3f212021-07-21 15:22:47 +01001150 MBEDTLS_PUT_UINT16_BE( curve_info->tls_id, buf, 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001151
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001152 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001153}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001154
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001155/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1157 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001158 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 * 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 +02001160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001162{
Janos Follath24eed8d2019-11-22 13:21:35 +00001163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001164
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001165 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001167
1168 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001170 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001173 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001176
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001177 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
1179 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001182 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001184
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001185cleanup:
1186 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001187}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001188
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001189/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001190 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001191 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001192 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 * 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 +01001194 * bring the result back to this range.
1195 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001196 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001197 */
1198
1199/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 * 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 +01001201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001203#define INC_MUL_COUNT mul_count++;
1204#else
1205#define INC_MUL_COUNT
1206#endif
1207
Hanno Becker1eeca412018-10-15 12:01:35 +01001208#define MOD_MUL( N ) \
1209 do \
1210 { \
1211 MBEDTLS_MPI_CHK( ecp_modp( &(N), grp ) ); \
1212 INC_MUL_COUNT \
1213 } while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001214
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001215static inline int mbedtls_mpi_mul_mod( const mbedtls_ecp_group *grp,
1216 mbedtls_mpi *X,
1217 const mbedtls_mpi *A,
1218 const mbedtls_mpi *B )
1219{
Janos Follath24eed8d2019-11-22 13:21:35 +00001220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001221 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( X, A, B ) );
1222 MOD_MUL( *X );
1223cleanup:
1224 return( ret );
1225}
1226
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001227/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001229 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001230 */
Hanno Becker1eeca412018-10-15 12:01:35 +01001231#define MOD_SUB( N ) \
1232 while( (N).s < 0 && mbedtls_mpi_cmp_int( &(N), 0 ) != 0 ) \
1233 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001234
Steven Cooremane5388962021-03-01 14:04:53 +01001235#if ( defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
1236 !( defined(MBEDTLS_ECP_NO_FALLBACK) && \
1237 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1238 defined(MBEDTLS_ECP_ADD_MIXED_ALT) ) ) || \
1239 ( defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) && \
1240 !( defined(MBEDTLS_ECP_NO_FALLBACK) && \
1241 defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) ) )
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001242static inline int mbedtls_mpi_sub_mod( const mbedtls_ecp_group *grp,
1243 mbedtls_mpi *X,
1244 const mbedtls_mpi *A,
1245 const mbedtls_mpi *B )
1246{
Janos Follath24eed8d2019-11-22 13:21:35 +00001247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001248 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( X, A, B ) );
1249 MOD_SUB( *X );
1250cleanup:
1251 return( ret );
1252}
Steven Cooremane5388962021-03-01 14:04:53 +01001253#endif /* All functions referencing mbedtls_mpi_sub_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001254
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001255/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 * 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 +02001257 * We known P, N and the result are positive, so sub_abs is correct, and
1258 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001259 */
Hanno Becker1eeca412018-10-15 12:01:35 +01001260#define MOD_ADD( N ) \
1261 while( mbedtls_mpi_cmp_mpi( &(N), &grp->P ) >= 0 ) \
1262 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001263
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001264static inline int mbedtls_mpi_add_mod( const mbedtls_ecp_group *grp,
1265 mbedtls_mpi *X,
1266 const mbedtls_mpi *A,
1267 const mbedtls_mpi *B )
1268{
Janos Follath24eed8d2019-11-22 13:21:35 +00001269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001270 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, A, B ) );
1271 MOD_ADD( *X );
1272cleanup:
1273 return( ret );
1274}
1275
Steven Cooremane5388962021-03-01 14:04:53 +01001276#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
1277 !( defined(MBEDTLS_ECP_NO_FALLBACK) && \
1278 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1279 defined(MBEDTLS_ECP_ADD_MIXED_ALT) )
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001280static inline int mbedtls_mpi_shift_l_mod( const mbedtls_ecp_group *grp,
1281 mbedtls_mpi *X,
1282 size_t count )
1283{
Janos Follath24eed8d2019-11-22 13:21:35 +00001284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001285 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, count ) );
1286 MOD_ADD( *X );
1287cleanup:
1288 return( ret );
1289}
Steven Cooremane5388962021-03-01 14:04:53 +01001290#endif /* All functions referencing mbedtls_mpi_shift_l_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001291
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02001292#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001293/*
1294 * For curves in short Weierstrass form, we do all the internal operations in
1295 * Jacobian coordinates.
1296 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001297 * For multiplication, we'll use a comb method with countermeasures against
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001298 * SPA, hence timing attacks.
1299 */
1300
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001301/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001302 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001303 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001308 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001309
Janos Follathb0697532016-08-18 12:38:46 +01001310#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001311 if( mbedtls_internal_ecp_grp_capable( grp ) )
1312 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +01001313#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001314
1315#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
1316 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1317#else
1318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1319 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001321
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001322 /*
1323 * X = X / Z^2 mod p
1324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001326 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ZZi, &Zi, &Zi ) );
1327 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->X, &pt->X, &ZZi ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001328
1329 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001330 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001331 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001332 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &ZZi ) );
1333 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &Zi ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001334
1335 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001336 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001339
1340cleanup:
1341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001343
1344 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001345#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001346}
1347
1348/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001349 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001350 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001351 * (See for example Cohen's "A Course in Computational Algebraic Number
1352 * Theory", Algorithm 10.3.4.)
1353 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001354 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001355 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001356 *
1357 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001360 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001361{
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001362 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001363 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001364
Janos Follathb0697532016-08-18 12:38:46 +01001365#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001366 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001367 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +01001368#endif
1369
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001370#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
1371 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1372#else
1373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1374 size_t i;
1375 mbedtls_mpi *c, u, Zi, ZZi;
1376
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001377 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001378 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001379
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001380 for( i = 0; i < T_size; i++ )
1381 mbedtls_mpi_init( &c[i] );
1382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001384
1385 /*
1386 * c[i] = Z_0 * ... * Z_i
1387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001389 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001390 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001391 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001392 }
1393
1394 /*
1395 * u = 1 / (Z_0 * ... * Z_n) mod P
1396 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001397 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001398
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001399 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001400 {
1401 /*
1402 * Zi = 1 / Z_i mod p
1403 * u = 1 / (Z_0 * ... * Z_i) mod P
1404 */
1405 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001407 }
1408 else
1409 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001410 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &Zi, &u, &c[i-1] ) );
1411 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &u, &u, &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001412 }
1413
1414 /*
1415 * proceed as in normalize()
1416 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001417 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ZZi, &Zi, &Zi ) );
1418 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->X, &T[i]->X, &ZZi ) );
1419 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->Y, &T[i]->Y, &ZZi ) );
1420 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->Y, &T[i]->Y, &Zi ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001421
1422 /*
1423 * Post-precessing: reclaim some memory by shrinking coordinates
1424 * - not storing Z (always 1)
1425 * - shrinking other coordinates, but still keeping the same number of
1426 * limbs as P, as otherwise it will too likely be regrown too fast.
1427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1429 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1430 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001431
1432 if( i == 0 )
1433 break;
1434 }
1435
1436cleanup:
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001439 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_mpi_free( &c[i] );
1441 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001442
1443 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001444#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) */
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001445}
1446
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001447/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001448 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1449 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1452 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001453 unsigned char inv )
1454{
Janos Follath24eed8d2019-11-22 13:21:35 +00001455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001456 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001460
1461 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1463 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1464 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001465
1466cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001468
1469 return( ret );
1470}
1471
1472/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001473 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001474 *
Peter Dettmance661b22015-02-07 14:43:51 +07001475 * 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 +01001476 *
Peter Dettmance661b22015-02-07 14:43:51 +07001477 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1478 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1479 *
1480 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1481 *
1482 * Cost: 1D := 3M + 4S (A == 0)
1483 * 4M + 4S (A == -3)
1484 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1487 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001488{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001490 dbl_count++;
1491#endif
1492
Janos Follathb0697532016-08-18 12:38:46 +01001493#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001494 if( mbedtls_internal_ecp_grp_capable( grp ) )
1495 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001496#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001497
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001498#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
1499 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1500#else
1501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1502 mbedtls_mpi M, S, T, U;
1503
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001504 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 +01001505
1506 /* Special case for A = -3 */
1507 if( grp->A.p == NULL )
1508 {
Peter Dettmance661b22015-02-07 14:43:51 +07001509 /* M = 3(X + Z^2)(X - Z^2) */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001510 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->Z, &P->Z ) );
1511 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &T, &P->X, &S ) );
1512 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &U, &P->X, &S ) );
1513 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &T, &U ) );
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001514 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001515 }
1516 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001517 {
Peter Dettmance661b22015-02-07 14:43:51 +07001518 /* M = 3.X^2 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001519 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->X, &P->X ) );
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001520 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001521
1522 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001523 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001524 {
1525 /* M += A.Z^4 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001526 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->Z, &P->Z ) );
1527 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &S, &S ) );
1528 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &T, &grp->A ) );
1529 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &M, &M, &S ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001530 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001531 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001532
Peter Dettmance661b22015-02-07 14:43:51 +07001533 /* S = 4.X.Y^2 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001534 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &P->Y, &P->Y ) );
1535 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &T, 1 ) );
1536 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->X, &T ) );
1537 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &S, 1 ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001538
Peter Dettmance661b22015-02-07 14:43:51 +07001539 /* U = 8.Y^4 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001540 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &U, &T, &T ) );
1541 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &U, 1 ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001542
1543 /* T = M^2 - 2.S */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001544 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &M, &M ) );
1545 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T, &T, &S ) );
1546 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T, &T, &S ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001547
1548 /* S = M(S - T) - U */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001549 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S, &S, &T ) );
1550 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &S, &M ) );
1551 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S, &S, &U ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001552
1553 /* U = 2.Y.Z */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001554 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &U, &P->Y, &P->Z ) );
1555 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &U, 1 ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001556
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001557 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1558 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1559 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001560
1561cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001562 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 +02001563
1564 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001565#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) */
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001566}
1567
1568/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001569 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001570 *
1571 * The coordinates of Q must be normalized (= affine),
1572 * but those of P don't need to. R is not normalized.
1573 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001574 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001575 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001576 * - at each step, P, Q and R are multiples of the base point, the factor
1577 * being less than its order, so none of them is zero;
1578 * - Q is an odd multiple of the base point, P an even multiple,
1579 * due to the choice of precomputed points in the modified comb method.
1580 * So branches for these cases do not leak secret information.
1581 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001582 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1583 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001584 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1587 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001590 add_count++;
1591#endif
1592
Janos Follathb0697532016-08-18 12:38:46 +01001593#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001594 if( mbedtls_internal_ecp_grp_capable( grp ) )
1595 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001596#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001597
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001598#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_ADD_MIXED_ALT)
1599 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1600#else
1601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1602 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
1603
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001604 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001605 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
1608 return( mbedtls_ecp_copy( R, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
1611 return( mbedtls_ecp_copy( R, P ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001612
1613 /*
1614 * Make sure Q coordinates are normalized
1615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
1617 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
1620 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001621
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001622 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T1, &P->Z, &P->Z ) );
1623 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T2, &T1, &P->Z ) );
1624 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T1, &T1, &Q->X ) );
1625 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T2, &T2, &Q->Y ) );
1626 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T1, &T1, &P->X ) );
1627 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T2, &T2, &P->Y ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001628
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001629 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001633 {
1634 ret = ecp_double_jac( grp, R, P );
1635 goto cleanup;
1636 }
1637 else
1638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 ret = mbedtls_ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001640 goto cleanup;
1641 }
1642 }
1643
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001644 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &Z, &P->Z, &T1 ) );
1645 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T1, &T1 ) );
1646 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T4, &T3, &T1 ) );
1647 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T3, &P->X ) );
1648 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &T3 ) );
1649 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &T1, 1 ) );
1650 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &X, &T2, &T2 ) );
1651 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &X, &X, &T1 ) );
1652 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &X, &X, &T4 ) );
1653 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T3, &T3, &X ) );
1654 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T3, &T2 ) );
1655 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T4, &T4, &P->Y ) );
1656 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &Y, &T3, &T4 ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1659 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1660 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001661
1662cleanup:
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1665 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001666
1667 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001668#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001669}
1670
1671/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001672 * Randomize jacobian coordinates:
1673 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001674 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001675 *
1676 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001679 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1680{
Janos Follathb0697532016-08-18 12:38:46 +01001681#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001682 if( mbedtls_internal_ecp_grp_capable( grp ) )
1683 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001684#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001685
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001686#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
1687 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1688#else
1689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1690 mbedtls_mpi l, ll;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001693
1694 /* Generate l such that 1 < l < p */
Gilles Peskine6466d342021-03-29 22:28:50 +02001695 MBEDTLS_MPI_CHK( mbedtls_mpi_random( &l, 2, &grp->P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001696
1697 /* Z = l * Z */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001698 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Z, &pt->Z, &l ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001699
1700 /* X = l^2 * X */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001701 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ll, &l, &l ) );
1702 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->X, &pt->X, &ll ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001703
1704 /* Y = l^3 * Y */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001705 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ll, &ll, &l ) );
1706 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &ll ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001707
1708cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001710
Gilles Peskine6466d342021-03-29 22:28:50 +02001711 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
1712 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001713 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001714#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) */
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001715}
1716
1717/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001718 * Check and define parameters used by the comb method (see below for details)
1719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1721#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001722#endif
1723
1724/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001726
1727/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001729
1730/*
1731 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001732 *
1733 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001734 * modified version that provides resistance to SPA by avoiding zero
1735 * digits in the representation as in [3]. We modify the method further by
1736 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001737 * representation uses one more K_i, due to carries, but saves on the size of
1738 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001739 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001740 * Summary of the comb method and its modifications:
1741 *
1742 * - The goal is to compute m*P for some w*d-bit integer m.
1743 *
1744 * - The basic comb method splits m into the w-bit integers
1745 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1746 * index has residue i modulo d, and computes m * P as
1747 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1748 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1749 *
1750 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1751 * .. + 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]] ..,
1752 * thereby successively converting it into a form where all summands
1753 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1754 *
1755 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1756 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1757 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1758 * Performing and iterating this procedure for those x[i] that are even
1759 * (keeping track of carry), we can transform the original sum into one of the form
1760 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1761 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1762 * which is why we are only computing half of it in the first place in
1763 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1764 *
1765 * - For the sake of compactness, only the seven low-order bits of x[i]
1766 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001767 * 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 +02001768 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001769 *
1770 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001771 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001772 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001774 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1775 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001776 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001777static void ecp_comb_recode_core( unsigned char x[], size_t d,
1778 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001779{
1780 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001781 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001782
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001783 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001784
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001785 /* First get the classical comb values (except for x_d = 0) */
1786 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001787 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001789
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001790 /* Now make sure x_1 .. x_d are odd */
1791 c = 0;
1792 for( i = 1; i <= d; i++ )
1793 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001794 /* Add carry and update it */
1795 cc = x[i] & c;
1796 x[i] = x[i] ^ c;
1797 c = cc;
1798
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001799 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001800 adjust = 1 - ( x[i] & 0x01 );
1801 c |= x[i] & ( x[i-1] * adjust );
1802 x[i] = x[i] ^ ( x[i-1] * adjust );
1803 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001804 }
1805}
1806
1807/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001808 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001809 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001810 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001811 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001812 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1813 * 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 +01001814 *
1815 * 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 +02001816 *
1817 * Note: Even comb values (those where P would be omitted from the
1818 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001819 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001820 *
1821 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001822 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001823 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1824 * (3) [add] Computation of all T[i]
1825 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001826 *
1827 * Step 1 can be interrupted but not the others; together with the final
1828 * coordinate normalization they are the largest steps done at once, depending
1829 * on the window size. Here are operation counts for P-256:
1830 *
1831 * step (2) (3) (4)
1832 * w = 5 142 165 208
1833 * w = 4 136 77 160
1834 * w = 3 130 33 136
1835 * w = 2 124 11 124
1836 *
1837 * So if ECC operations are blocking for too long even with a low max_ops
1838 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1839 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1842 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001843 unsigned char w, size_t d,
1844 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001845{
Janos Follath24eed8d2019-11-22 13:21:35 +00001846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001847 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001848 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001849 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001851
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001852#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001853 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001854 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001855 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
1856 goto dbl;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001857 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001858 goto norm_dbl;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001859 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
1860 goto add;
1861 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
1862 goto norm_add;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001863 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001864#else
1865 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001866#endif
1867
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001868#if defined(MBEDTLS_ECP_RESTARTABLE)
1869 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1870 {
1871 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1872
1873 /* initial state for the loop */
1874 rs_ctx->rsm->i = 0;
1875 }
1876
1877dbl:
1878#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001879 /*
1880 * Set T[0] = P and
1881 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001884
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001885#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001886 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
1887 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001888 else
1889#endif
1890 j = 0;
1891
1892 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001893 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001894 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001895
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001896 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001897 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001898
1899 if( j % d == 0 )
1900 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1901
1902 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001903 }
1904
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001905#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001906 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1907 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1908
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001909norm_dbl:
1910#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001911 /*
1912 * Normalize current elements in T. As T has holes,
1913 * use an auxiliary array of pointers to elements in T.
1914 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001915 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001916 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001917 TT[j++] = T + i;
1918
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001919 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001920
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001921 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001922
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001923#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001924 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1925 rs_ctx->rsm->state = ecp_rsm_pre_add;
1926
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001927add:
1928#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001929 /*
1930 * Compute the remaining ones using the minimal number of additions
1931 * Be careful to update T[2^l] only after using it!
1932 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001933 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001934
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001935 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001936 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001937 j = i;
1938 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001940 }
1941
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001942#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001943 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1944 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1945
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001946norm_add:
1947#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001948 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001949 * Normalize final elements in T. Even though there are no holes now, we
1950 * still need the auxiliary array for homogeneity with the previous
1951 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001952 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001953 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001954 TT[j] = T + j + 1;
1955
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001956 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001957
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001958 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001959
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001960cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001961#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001962 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1963 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001964 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001965 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001966 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001967 }
1968#endif
Janos Follathb0697532016-08-18 12:38:46 +01001969
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001970 return( ret );
1971}
1972
1973/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001974 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001975 *
1976 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001979 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001980 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001981{
Janos Follath24eed8d2019-11-22 13:21:35 +00001982 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001983 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001984
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001985 /* Ignore the "sign" bit and scale down */
1986 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001987
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001988 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001989 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1992 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001993 }
1994
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001995 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001997
1998cleanup:
1999 return( ret );
2000}
2001
2002/*
2003 * Core multiplication algorithm for the (modified) comb method.
2004 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01002005 *
2006 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002009 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01002010 const unsigned char x[], size_t d,
2011 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002012 void *p_rng,
2013 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002014{
Janos Follath24eed8d2019-11-22 13:21:35 +00002015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002017 size_t i;
2018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002020
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002021#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002022 (void) rs_ctx;
2023#endif
2024
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002025#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002026 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
2027 rs_ctx->rsm->state != ecp_rsm_comb_core )
2028 {
2029 rs_ctx->rsm->i = 0;
2030 rs_ctx->rsm->state = ecp_rsm_comb_core;
2031 }
2032
2033 /* new 'if' instead of nested for the sake of the 'else' branch */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002034 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002035 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002036 /* restore current index (R already pointing to rs_ctx->rsm->R) */
2037 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002038 }
2039 else
2040#endif
2041 {
2042 /* Start with a non-zero point and randomize its coordinates */
2043 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002044 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002045 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
David Horstmannef661c52022-10-26 17:55:53 +01002046
2047 int have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002048#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmann9fc2f952022-11-07 14:28:30 +00002049 if( f_rng == NULL )
David Horstmannef661c52022-10-26 17:55:53 +01002050 have_rng = 0;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002051#endif
David Horstmannef661c52022-10-26 17:55:53 +01002052 if( have_rng )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002053 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
2054 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002055
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002056 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002057 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002058 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002059 --i;
2060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002062 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002064 }
2065
2066cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002069
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002070#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002071 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
2072 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002073 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002074 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002075 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002076 }
2077#endif
2078
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002079 return( ret );
2080}
2081
2082/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002083 * Recode the scalar to get constant-time comb multiplication
2084 *
2085 * As the actual scalar recoding needs an odd scalar as a starting point,
2086 * this wrapper ensures that by replacing m by N - m if necessary, and
2087 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002088 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002089 * This works because we only support large prime order for Short Weierstrass
2090 * curves, so N is always odd hence either m or N - m is.
2091 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002092 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002093 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002094static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
2095 const mbedtls_mpi *m,
2096 unsigned char k[COMB_MAX_D + 1],
2097 size_t d,
2098 unsigned char w,
2099 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002100{
Janos Follath24eed8d2019-11-22 13:21:35 +00002101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002102 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002103
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002104 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002105 mbedtls_mpi_init( &mm );
2106
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002107 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002108 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
2109 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2110
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002111 /* do we need the parity trick? */
2112 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
2113
2114 /* execute parity fix in constant time */
2115 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002116 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002117 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
2118
2119 /* actual scalar recoding */
2120 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002121
2122cleanup:
2123 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002124 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002125
2126 return( ret );
2127}
2128
2129/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002130 * Perform comb multiplication (for short Weierstrass curves)
2131 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002132 *
2133 * Scalar recoding may use a parity trick that makes us compute -m * P,
2134 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002135 */
2136static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
2137 mbedtls_ecp_point *R,
2138 const mbedtls_mpi *m,
2139 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002140 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002141 unsigned char w,
2142 size_t d,
2143 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002144 void *p_rng,
2145 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002146{
Janos Follath24eed8d2019-11-22 13:21:35 +00002147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002148 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002149 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002150 mbedtls_ecp_point *RR = R;
2151
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002152#if defined(MBEDTLS_ECP_RESTARTABLE)
2153 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2154 {
2155 RR = &rs_ctx->rsm->R;
2156
2157 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
2158 goto final_norm;
2159 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002160#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002161
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002162 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
2163 &parity_trick ) );
2164 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
2165 f_rng, p_rng, rs_ctx ) );
2166 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
2167
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002168#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002169 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002170 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002171
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002172final_norm:
Manuel Pégourié-Gonnard9b8d34e2020-06-08 09:53:20 +02002173 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002174#endif
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002175 /*
2176 * Knowledge of the jacobian coordinates may leak the last few bits of the
2177 * scalar [1], and since our MPI implementation isn't constant-flow,
2178 * inversion (used for coordinate normalization) may leak the full value
2179 * of its input via side-channels [2].
2180 *
2181 * [1] https://eprint.iacr.org/2003/191
2182 * [2] https://eprint.iacr.org/2020/055
2183 *
2184 * Avoid the leak by randomizing coordinates before we normalize them.
2185 */
David Horstmannef661c52022-10-26 17:55:53 +01002186 int have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002187#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmann9fc2f952022-11-07 14:28:30 +00002188 if( f_rng == NULL )
David Horstmannef661c52022-10-26 17:55:53 +01002189 have_rng = 0;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002190#endif
David Horstmannef661c52022-10-26 17:55:53 +01002191 if( have_rng )
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002192 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
2193
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002194 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
2195
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002196#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02002197 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2198 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002199#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002200
2201cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002202 return( ret );
2203}
2204
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002205/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002206 * Pick window size based on curve size and whether we optimize for base point
2207 */
2208static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
2209 unsigned char p_eq_g )
2210{
2211 unsigned char w;
2212
2213 /*
2214 * Minimize the number of multiplications, that is minimize
2215 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2216 * (see costs of the various parts, with 1S = 1M)
2217 */
2218 w = grp->nbits >= 384 ? 5 : 4;
2219
2220 /*
2221 * If P == G, pre-compute a bit more, since this may be re-used later.
2222 * Just adding one avoids upping the cost of the first mul too much,
2223 * and the memory cost too.
2224 */
2225 if( p_eq_g )
2226 w++;
2227
2228 /*
2229 * Make sure w is within bounds.
2230 * (The last test is useful only for very small curves in the test suite.)
2231 */
k-stachowiak653a4a22019-07-03 14:31:09 +02002232#if( MBEDTLS_ECP_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002233 if( w > MBEDTLS_ECP_WINDOW_SIZE )
2234 w = MBEDTLS_ECP_WINDOW_SIZE;
k-stachowiak653a4a22019-07-03 14:31:09 +02002235#endif
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002236 if( w >= grp->nbits )
2237 w = 2;
2238
2239 return( w );
2240}
2241
2242/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002243 * Multiplication using the comb method - for curves in short Weierstrass form
2244 *
2245 * This function is mainly responsible for administrative work:
2246 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002247 * - managing the table of precomputed points (passed between the below two
Shaun Case0e7791f2021-12-20 21:14:10 -08002248 * functions): allocation, computation, ownership transfer, freeing.
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002249 *
2250 * It delegates the actual arithmetic work to:
2251 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2252 *
2253 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2256 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002257 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002258 void *p_rng,
2259 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002260{
Janos Follath24eed8d2019-11-22 13:21:35 +00002261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002262 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002263 size_t d;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002264 unsigned char T_size = 0, T_ok = 0;
2265 mbedtls_ecp_point *T = NULL;
2266#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2267 ecp_drbg_context drbg_ctx;
2268
2269 ecp_drbg_init( &drbg_ctx );
2270#endif
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002271
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002272 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002273
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002274#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2275 if( f_rng == NULL )
2276 {
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002277 /* Adjust pointers */
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002278 f_rng = &ecp_drbg_random;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002279#if defined(MBEDTLS_ECP_RESTARTABLE)
2280 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2281 p_rng = &rs_ctx->rsm->drbg_ctx;
2282 else
2283#endif
2284 p_rng = &drbg_ctx;
2285
2286 /* Initialize internal DRBG if necessary */
2287#if defined(MBEDTLS_ECP_RESTARTABLE)
2288 if( rs_ctx == NULL || rs_ctx->rsm == NULL ||
2289 rs_ctx->rsm->drbg_seeded == 0 )
2290#endif
2291 {
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +02002292 const size_t m_len = ( grp->nbits + 7 ) / 8;
2293 MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m, m_len ) );
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002294 }
2295#if defined(MBEDTLS_ECP_RESTARTABLE)
2296 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2297 rs_ctx->rsm->drbg_seeded = 1;
2298#endif
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002299 }
2300#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2301
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002302 /* Is P the base point ? */
2303#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2304 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
2305 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002306#else
2307 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002308#endif
2309
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002310 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002311 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002312 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002313 d = ( grp->nbits + w - 1 ) / w;
2314
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002315 /* Pre-computed table: do we have it already for the base point? */
2316 if( p_eq_g && grp->T != NULL )
2317 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002318 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002319 T = grp->T;
2320 T_ok = 1;
2321 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002322 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002323#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002324 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002325 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002326 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002327 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002328 T = rs_ctx->rsm->T;
2329 rs_ctx->rsm->T = NULL;
2330 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002331
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002332 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002333 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002334 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002335 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002336#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002337 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002338 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002339 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002340 if( T == NULL )
2341 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002342 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002343 goto cleanup;
2344 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002345
2346 for( i = 0; i < T_size; i++ )
2347 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002348
2349 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002350 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002351
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002352 /* Compute table (or finish computing it) if not done already */
2353 if( !T_ok )
2354 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002355 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002356
2357 if( p_eq_g )
2358 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002359 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002360 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002361 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002362 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002363 }
2364 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002365
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002366 /* Actual comb multiplication using precomputed points */
2367 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002368 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002369 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002370
2371cleanup:
2372
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002373#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2374 ecp_drbg_free( &drbg_ctx );
2375#endif
2376
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002377 /* does T belong to the group? */
2378 if( T == grp->T )
2379 T = NULL;
2380
2381 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002382#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002383 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 +01002384 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002385 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002386 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002387 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002388 T = NULL;
2389 }
2390#endif
2391
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002392 /* did T belong to us? then let's destroy it! */
2393 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002394 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002395 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_ecp_point_free( &T[i] );
2397 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002398 }
2399
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002400 /* prevent caller from using invalid value */
David Horstmanne9af9e32022-10-25 10:32:08 +01002401 int should_free_R = ( ret != 0 );
David Horstmannb95ee002022-10-06 19:11:04 +01002402#if defined(MBEDTLS_ECP_RESTARTABLE)
2403 /* don't free R while in progress in case R == P */
David Horstmanne9af9e32022-10-25 10:32:08 +01002404 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2405 should_free_R = 0;
David Horstmannb95ee002022-10-06 19:11:04 +01002406#endif
2407 if( should_free_R )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002409
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002410 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002411
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002412 return( ret );
2413}
2414
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002415#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002416
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002417#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002418/*
2419 * For Montgomery curves, we do all the internal arithmetic in projective
2420 * coordinates. Import/export of points uses only the x coordinates, which is
Shaun Case0e7791f2021-12-20 21:14:10 -08002421 * internally represented as X / Z.
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002422 *
2423 * For scalar multiplication, we'll use a Montgomery ladder.
2424 */
2425
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002426/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002427 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2428 * Cost: 1M + 1I
2429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002431{
Janos Follathb0697532016-08-18 12:38:46 +01002432#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002433 if( mbedtls_internal_ecp_grp_capable( grp ) )
2434 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002435#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002436
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002437#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
2438 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2439#else
2440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002442 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->X, &P->X, &P->Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002444
2445cleanup:
2446 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002447#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002448}
2449
2450/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002451 * Randomize projective x/z coordinates:
2452 * (X, Z) -> (l X, l Z) for random l
2453 * This is sort of the reverse operation of ecp_normalize_mxz().
2454 *
2455 * This countermeasure was first suggested in [2].
2456 * Cost: 2M
2457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002459 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2460{
Janos Follathb0697532016-08-18 12:38:46 +01002461#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002462 if( mbedtls_internal_ecp_grp_capable( grp ) )
Steven Cooremanb74b5602021-03-11 13:18:29 +01002463 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01002464#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002465
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002466#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
2467 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2468#else
2469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2470 mbedtls_mpi l;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002472
2473 /* Generate l such that 1 < l < p */
Gilles Peskine6466d342021-03-29 22:28:50 +02002474 MBEDTLS_MPI_CHK( mbedtls_mpi_random( &l, 2, &grp->P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002475
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002476 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->X, &P->X, &l ) );
2477 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->Z, &P->Z, &l ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002478
2479cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002481
Gilles Peskine6466d342021-03-29 22:28:50 +02002482 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2483 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002484 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002485#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002486}
2487
2488/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002489 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2490 * for Montgomery curves in x/z coordinates.
2491 *
2492 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2493 * with
2494 * d = X1
2495 * P = (X2, Z2)
2496 * Q = (X3, Z3)
2497 * R = (X4, Z4)
2498 * S = (X5, Z5)
2499 * and eliminating temporary variables tO, ..., t4.
2500 *
2501 * Cost: 5M + 4S
2502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2504 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2505 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2506 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002507{
Janos Follathb0697532016-08-18 12:38:46 +01002508#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002509 if( mbedtls_internal_ecp_grp_capable( grp ) )
2510 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002511#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002512
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002513#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
2514 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2515#else
2516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2517 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2520 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2521 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002522
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002523 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &A, &P->X, &P->Z ) );
2524 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &AA, &A, &A ) );
2525 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &B, &P->X, &P->Z ) );
2526 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &BB, &B, &B ) );
2527 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &E, &AA, &BB ) );
2528 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &C, &Q->X, &Q->Z ) );
2529 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &D, &Q->X, &Q->Z ) );
2530 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &DA, &D, &A ) );
2531 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &CB, &C, &B ) );
Aurelien Jarno66deb382020-04-20 21:36:48 +02002532 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &S->X, &DA, &CB ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002533 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->X, &S->X, &S->X ) );
2534 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S->Z, &DA, &CB ) );
2535 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->Z, &S->Z, &S->Z ) );
2536 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->Z, d, &S->Z ) );
2537 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->X, &AA, &BB ) );
2538 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->Z, &grp->A, &E ) );
2539 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &R->Z, &BB, &R->Z ) );
2540 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->Z, &E, &R->Z ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002541
2542cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2544 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2545 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002546
2547 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002548#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002549}
2550
2551/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002552 * Multiplication with Montgomery ladder in x/z coordinates,
2553 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2556 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002557 int (*f_rng)(void *, unsigned char *, size_t),
2558 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002559{
Janos Follath24eed8d2019-11-22 13:21:35 +00002560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002561 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002562 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 mbedtls_ecp_point RP;
2564 mbedtls_mpi PX;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002565#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2566 ecp_drbg_context drbg_ctx;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002567
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002568 ecp_drbg_init( &drbg_ctx );
2569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002571
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002572#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2573 if( f_rng == NULL )
2574 {
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +02002575 const size_t m_len = ( grp->nbits + 7 ) / 8;
2576 MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m, m_len ) );
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002577 f_rng = &ecp_drbg_random;
2578 p_rng = &drbg_ctx;
2579 }
2580#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2581
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002582 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2584 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002585
2586 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2588 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2589 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002590
Shaun Case0e7791f2021-12-20 21:14:10 -08002591 /* RP.X might be slightly larger than P, so reduce it */
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002592 MOD_ADD( RP.X );
2593
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002594 /* Randomize coordinates of the starting point */
David Horstmannef661c52022-10-26 17:55:53 +01002595 int have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002596#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannef661c52022-10-26 17:55:53 +01002597 if( f_rng == NULL )
2598 have_rng = 0;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002599#endif
David Horstmannef661c52022-10-26 17:55:53 +01002600 if( have_rng )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002602
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002603 /* Loop invariant: R = result so far, RP = R + P */
Aurelien Jarnoedc110d2022-05-15 13:24:05 +02002604 i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002605 while( i-- > 0 )
2606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002608 /*
2609 * if (b) R = 2R + P else R = 2R,
2610 * which is:
2611 * if (b) double_add( RP, R, RP, R )
2612 * else double_add( R, RP, R, RP )
2613 * but using safe conditional swaps to avoid leaks
2614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2616 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2617 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2618 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2619 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002620 }
2621
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002622 /*
2623 * Knowledge of the projective coordinates may leak the last few bits of the
2624 * scalar [1], and since our MPI implementation isn't constant-flow,
2625 * inversion (used for coordinate normalization) may leak the full value
2626 * of its input via side-channels [2].
2627 *
2628 * [1] https://eprint.iacr.org/2003/191
2629 * [2] https://eprint.iacr.org/2020/055
2630 *
2631 * Avoid the leak by randomizing coordinates before we normalize them.
2632 */
David Horstmannef661c52022-10-26 17:55:53 +01002633 have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002634#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannef661c52022-10-26 17:55:53 +01002635 if( f_rng == NULL )
2636 have_rng = 0;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002637#endif
David Horstmannef661c52022-10-26 17:55:53 +01002638 if( have_rng )
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002639 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
2640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002642
2643cleanup:
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002644#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2645 ecp_drbg_free( &drbg_ctx );
2646#endif
2647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002649
2650 return( ret );
2651}
2652
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002653#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002654
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002655/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002656 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002657 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002658int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002660 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2661 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002662{
Janos Follathb0697532016-08-18 12:38:46 +01002663 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002664#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2665 char is_grp_capable = 0;
2666#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002667 ECP_VALIDATE_RET( grp != NULL );
2668 ECP_VALIDATE_RET( R != NULL );
2669 ECP_VALIDATE_RET( m != NULL );
2670 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002671
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002672#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002673 /* reset ops count for this call if top-level */
2674 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2675 rs_ctx->ops_done = 0;
Gilles Peskine59970052019-02-28 13:12:06 +01002676#else
2677 (void) rs_ctx;
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002678#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002679
Janos Follathc44ab972016-11-18 16:38:23 +00002680#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002681 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002682 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002683#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002684
David Horstmannb95ee002022-10-06 19:11:04 +01002685 int restarting = 0;
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002686#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannb95ee002022-10-06 19:11:04 +01002687 restarting = ( rs_ctx != NULL && rs_ctx->rsm != NULL );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002688#endif
David Horstmannb95ee002022-10-06 19:11:04 +01002689 /* skip argument check when restarting */
2690 if( !restarting )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002691 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002692 /* check_privkey is free */
2693 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2694
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002695 /* Common sanity checks */
2696 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2697 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002698 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002699
2700 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002701#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002702 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002703 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002704#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002705#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002706 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002707 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002708#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002709
Janos Follath6c8ccd52016-11-29 15:37:09 +00002710cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002711
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002712#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002713 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002714 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002715#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002716
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002717#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002718 if( rs_ctx != NULL )
2719 rs_ctx->depth--;
2720#endif
2721
Janos Follathb0697532016-08-18 12:38:46 +01002722 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002723}
2724
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002725/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002726 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002727 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002728int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002729 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002730 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002731{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002732 ECP_VALIDATE_RET( grp != NULL );
2733 ECP_VALIDATE_RET( R != NULL );
2734 ECP_VALIDATE_RET( m != NULL );
2735 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002736 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002737}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002738
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002739#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002740/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002741 * Check that an affine point is valid as a public key,
2742 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002745{
Janos Follath24eed8d2019-11-22 13:21:35 +00002746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002748
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002749 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2751 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2752 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2753 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2754 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002757
2758 /*
2759 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002760 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002761 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002762 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &YY, &pt->Y, &pt->Y ) );
2763 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &RHS, &pt->X, &pt->X ) );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002764
2765 /* Special case for A = -3 */
2766 if( grp->A.p == NULL )
2767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002769 }
2770 else
2771 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002772 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &RHS, &RHS, &grp->A ) );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002773 }
2774
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002775 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &RHS, &RHS, &pt->X ) );
2776 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &RHS, &RHS, &grp->B ) );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2779 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002780
2781cleanup:
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002784
2785 return( ret );
2786}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002787#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002788
Gilles Peskine9b99a892018-09-14 18:32:19 +02002789#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002790/*
TRodziewicz9edff742021-03-04 17:59:39 +01002791 * R = m * P with shortcuts for m == 0, m == 1 and m == -1
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002792 * NOT constant-time - ONLY for short Weierstrass!
2793 */
2794static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2795 mbedtls_ecp_point *R,
2796 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002797 const mbedtls_ecp_point *P,
2798 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002799{
Janos Follath24eed8d2019-11-22 13:21:35 +00002800 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002801
TRodziewicz782a7ea2021-03-17 11:35:16 +01002802 if( mbedtls_mpi_cmp_int( m, 0 ) == 0 )
TRodziewicz9edff742021-03-04 17:59:39 +01002803 {
Dave Rodgman42687312022-08-10 11:26:24 +01002804 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
TRodziewicz9edff742021-03-04 17:59:39 +01002805 MBEDTLS_MPI_CHK( mbedtls_ecp_set_zero( R ) );
2806 }
2807 else if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002808 {
Dave Rodgman42687312022-08-10 11:26:24 +01002809 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002810 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2811 }
2812 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2813 {
Dave Rodgman42687312022-08-10 11:26:24 +01002814 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002815 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2816 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2817 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2818 }
2819 else
2820 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002821 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2822 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002823 }
2824
2825cleanup:
2826 return( ret );
2827}
2828
2829/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002830 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002831 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002832 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002833int mbedtls_ecp_muladd_restartable(
2834 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002835 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002836 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2837 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002838{
Janos Follath24eed8d2019-11-22 13:21:35 +00002839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002840 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002841 mbedtls_ecp_point *pmP = &mP;
2842 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002843#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2844 char is_grp_capable = 0;
2845#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002846 ECP_VALIDATE_RET( grp != NULL );
2847 ECP_VALIDATE_RET( R != NULL );
2848 ECP_VALIDATE_RET( m != NULL );
2849 ECP_VALIDATE_RET( P != NULL );
2850 ECP_VALIDATE_RET( n != NULL );
2851 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002852
Janos Follathdf9295b2019-02-26 12:36:52 +00002853 if( mbedtls_ecp_get_type( grp ) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002854 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2855
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002856 mbedtls_ecp_point_init( &mP );
2857
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002858 ECP_RS_ENTER( ma );
2859
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002860#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002861 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2862 {
2863 /* redirect intermediate results to restart context */
2864 pmP = &rs_ctx->ma->mP;
2865 pR = &rs_ctx->ma->R;
2866
2867 /* jump to next operation */
2868 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2869 goto mul2;
2870 if( rs_ctx->ma->state == ecp_rsma_add )
2871 goto add;
2872 if( rs_ctx->ma->state == ecp_rsma_norm )
2873 goto norm;
2874 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002875#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002876
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002877 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002878#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002879 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002880 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002881
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002882mul2:
2883#endif
2884 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002885
2886#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2887 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2888 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2889#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2890
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002891#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002892 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002893 rs_ctx->ma->state = ecp_rsma_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002894
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002895add:
2896#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002897 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002898 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002899#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002900 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002901 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002902
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002903norm:
2904#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002905 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002906 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2907
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002908#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002909 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2910 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2911#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002912
2913cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002914#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002915 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002916 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002917#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002918
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002919 mbedtls_ecp_point_free( &mP );
2920
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002921 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002922
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002923 return( ret );
2924}
2925
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002926/*
2927 * Linear combination
2928 * NOT constant-time
2929 */
2930int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2931 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2932 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2933{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002934 ECP_VALIDATE_RET( grp != NULL );
2935 ECP_VALIDATE_RET( R != NULL );
2936 ECP_VALIDATE_RET( m != NULL );
2937 ECP_VALIDATE_RET( P != NULL );
2938 ECP_VALIDATE_RET( n != NULL );
2939 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002940 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2941}
Gilles Peskine9b99a892018-09-14 18:32:19 +02002942#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002943
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002944#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02002945#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard520f0a02021-06-23 12:53:18 +02002946#define ECP_MPI_INIT(s, n, p) {s, (n), (mbedtls_mpi_uint *)(p)}
2947#define ECP_MPI_INIT_ARRAY(x) \
2948 ECP_MPI_INIT(1, sizeof(x) / sizeof(mbedtls_mpi_uint), x)
2949/*
2950 * Constants for the two points other than 0, 1, -1 (mod p) in
2951 * https://cr.yp.to/ecdh.html#validate
2952 * See ecp_check_pubkey_x25519().
2953 */
2954static const mbedtls_mpi_uint x25519_bad_point_1[] = {
Janos Follathbc589022021-06-25 12:43:26 +01002955 MBEDTLS_BYTES_TO_T_UINT_8( 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae ),
2956 MBEDTLS_BYTES_TO_T_UINT_8( 0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a ),
2957 MBEDTLS_BYTES_TO_T_UINT_8( 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd ),
2958 MBEDTLS_BYTES_TO_T_UINT_8( 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00 ),
Manuel Pégourié-Gonnard520f0a02021-06-23 12:53:18 +02002959};
2960static const mbedtls_mpi_uint x25519_bad_point_2[] = {
Janos Follathbc589022021-06-25 12:43:26 +01002961 MBEDTLS_BYTES_TO_T_UINT_8( 0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24 ),
2962 MBEDTLS_BYTES_TO_T_UINT_8( 0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b ),
2963 MBEDTLS_BYTES_TO_T_UINT_8( 0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86 ),
2964 MBEDTLS_BYTES_TO_T_UINT_8( 0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57 ),
Manuel Pégourié-Gonnard520f0a02021-06-23 12:53:18 +02002965};
2966static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
2967 x25519_bad_point_1 );
2968static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
2969 x25519_bad_point_2 );
Janos Follath7d4ebdd2021-06-24 15:34:59 +01002970#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
Manuel Pégourié-Gonnard10b8e5a2021-06-23 12:25:48 +02002971
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02002972/*
2973 * Check that the input point is not one of the low-order points.
2974 * This is recommended by the "May the Fourth" paper:
2975 * https://eprint.iacr.org/2017/806.pdf
2976 * Those points are never sent by an honest peer.
2977 */
Janos Follath7d4ebdd2021-06-24 15:34:59 +01002978static int ecp_check_bad_points_mx( const mbedtls_mpi *X, const mbedtls_mpi *P,
2979 const mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02002980{
2981 int ret;
Manuel Pégourié-Gonnard10b8e5a2021-06-23 12:25:48 +02002982 mbedtls_mpi XmP;
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02002983
2984 mbedtls_mpi_init( &XmP );
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02002985
2986 /* Reduce X mod P so that we only need to check values less than P.
2987 * We know X < 2^256 so we can proceed by subtraction. */
2988 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &XmP, X ) );
2989 while( mbedtls_mpi_cmp_mpi( &XmP, P ) >= 0 )
2990 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &XmP, &XmP, P ) );
2991
Janos Follath7d4ebdd2021-06-24 15:34:59 +01002992 /* Check against the known bad values that are less than P. For Curve448
2993 * these are 0, 1 and -1. For Curve25519 we check the values less than P
2994 * from the following list: https://cr.yp.to/ecdh.html#validate */
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02002995 if( mbedtls_mpi_cmp_int( &XmP, 1 ) <= 0 ) /* takes care of 0 and 1 */
Janos Follathb4c676e2021-06-24 14:24:13 +01002996 {
2997 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2998 goto cleanup;
2999 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003000
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003001#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
3002 if( grp_id == MBEDTLS_ECP_DP_CURVE25519 )
Janos Follathb4c676e2021-06-24 14:24:13 +01003003 {
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003004 if( mbedtls_mpi_cmp_mpi( &XmP, &ecp_x25519_bad_point_1 ) == 0 )
3005 {
3006 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3007 goto cleanup;
3008 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003009
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003010 if( mbedtls_mpi_cmp_mpi( &XmP, &ecp_x25519_bad_point_2 ) == 0 )
3011 {
3012 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3013 goto cleanup;
3014 }
Janos Follathb4c676e2021-06-24 14:24:13 +01003015 }
Janos Follath2667fb72021-06-25 15:29:56 +01003016#else
3017 (void) grp_id;
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003018#endif
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003019
Manuel Pégourié-Gonnard10b8e5a2021-06-23 12:25:48 +02003020 /* Final check: check if XmP + 1 is P (final because it changes XmP!) */
3021 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &XmP, &XmP, 1 ) );
3022 if( mbedtls_mpi_cmp_mpi( &XmP, P ) == 0 )
Janos Follathb4c676e2021-06-24 14:24:13 +01003023 {
3024 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3025 goto cleanup;
3026 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003027
3028 ret = 0;
3029
3030cleanup:
3031 mbedtls_mpi_free( &XmP );
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003032
3033 return( ret );
3034}
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003035
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003036/*
3037 * Check validity of a public key for Montgomery curves with x-only schemes
3038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003040{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02003041 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003042 /* Allow any public value, if it's too big then we'll just reduce it mod p
3043 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
3045 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003046
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003047 /* Implicit in all standards (as they don't consider negative numbers):
3048 * X must be non-negative. This is normally ensured by the way it's
3049 * encoded for transmission, but let's be extra sure. */
3050 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 )
3051 return( MBEDTLS_ERR_ECP_INVALID_KEY );
3052
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003053 return( ecp_check_bad_points_mx( &pt->X, &grp->P, grp->id ) );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003054}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003055#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003056
3057/*
3058 * Check that a point is valid as a public key
3059 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003060int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
3061 const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003062{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003063 ECP_VALIDATE_RET( grp != NULL );
3064 ECP_VALIDATE_RET( pt != NULL );
3065
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003066 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
3068 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003069
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003070#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003071 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003072 return( ecp_check_pubkey_mx( grp, pt ) );
3073#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003074#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003075 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003076 return( ecp_check_pubkey_sw( grp, pt ) );
3077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003079}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003080
3081/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003083 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003084int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
3085 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003086{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003087 ECP_VALIDATE_RET( grp != NULL );
3088 ECP_VALIDATE_RET( d != NULL );
3089
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003090#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003091 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003092 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003093 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
3095 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02003096 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003098
3099 /* see [Curve25519] page 5 */
3100 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
3101 return( MBEDTLS_ERR_ECP_INVALID_KEY );
3102
3103 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003104 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003105#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3106#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003107 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003108 {
3109 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
3111 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
3112 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003113 else
3114 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003115 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003116#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003119}
3120
Gilles Peskinede332132021-03-23 22:31:31 +01003121#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3122MBEDTLS_STATIC_TESTABLE
Gilles Peskine3838f282021-03-24 12:34:40 +01003123int mbedtls_ecp_gen_privkey_mx( size_t high_bit,
Gilles Peskinede332132021-03-23 22:31:31 +01003124 mbedtls_mpi *d,
3125 int (*f_rng)(void *, unsigned char *, size_t),
3126 void *p_rng )
3127{
3128 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskineeadf31d2021-03-24 12:46:46 +01003129 size_t n_random_bytes = high_bit / 8 + 1;
Gilles Peskinede332132021-03-23 22:31:31 +01003130
3131 /* [Curve25519] page 5 */
Gilles Peskineeadf31d2021-03-24 12:46:46 +01003132 /* Generate a (high_bit+1)-bit random number by generating just enough
3133 * random bytes, then shifting out extra bits from the top (necessary
3134 * when (high_bit+1) is not a multiple of 8). */
3135 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_random_bytes,
3136 f_rng, p_rng ) );
3137 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_random_bytes - high_bit - 1 ) );
Gilles Peskinede332132021-03-23 22:31:31 +01003138
Gilles Peskine4f776742021-03-24 12:25:59 +01003139 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, high_bit, 1 ) );
Gilles Peskinede332132021-03-23 22:31:31 +01003140
3141 /* Make sure the last two bits are unset for Curve448, three bits for
3142 Curve25519 */
3143 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
3144 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Gilles Peskine3838f282021-03-24 12:34:40 +01003145 if( high_bit == 254 )
Gilles Peskinede332132021-03-23 22:31:31 +01003146 {
3147 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
3148 }
3149
3150cleanup:
3151 return( ret );
3152}
3153#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3154
Gilles Peskineaeab0fb2021-03-29 22:28:21 +02003155#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3156static int mbedtls_ecp_gen_privkey_sw(
3157 const mbedtls_mpi *N, mbedtls_mpi *d,
3158 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
3159{
3160 int ret = mbedtls_mpi_random( d, 1, N, f_rng, p_rng );
3161 switch( ret )
3162 {
3163 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
3164 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
3165 default:
3166 return( ret );
3167 }
3168}
3169#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3170
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003171/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003172 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003173 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003174int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
3175 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003176 int (*f_rng)(void *, unsigned char *, size_t),
3177 void *p_rng )
3178{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003179 ECP_VALIDATE_RET( grp != NULL );
3180 ECP_VALIDATE_RET( d != NULL );
3181 ECP_VALIDATE_RET( f_rng != NULL );
3182
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003183#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003184 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Gilles Peskinede332132021-03-23 22:31:31 +01003185 return( mbedtls_ecp_gen_privkey_mx( grp->nbits, d, f_rng, p_rng ) );
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003186#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003187
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003188#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003189 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Gilles Peskineaeab0fb2021-03-29 22:28:21 +02003190 return( mbedtls_ecp_gen_privkey_sw( &grp->N, d, f_rng, p_rng ) );
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003191#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003192
Gilles Peskinede332132021-03-23 22:31:31 +01003193 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003194}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01003195
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003196/*
3197 * Generate a keypair with configurable base point
3198 */
3199int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
3200 const mbedtls_ecp_point *G,
3201 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3202 int (*f_rng)(void *, unsigned char *, size_t),
3203 void *p_rng )
3204{
Janos Follath24eed8d2019-11-22 13:21:35 +00003205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003206 ECP_VALIDATE_RET( grp != NULL );
3207 ECP_VALIDATE_RET( d != NULL );
3208 ECP_VALIDATE_RET( G != NULL );
3209 ECP_VALIDATE_RET( Q != NULL );
3210 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003211
3212 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
3213 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
3214
3215cleanup:
3216 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003217}
3218
3219/*
3220 * Generate key pair, wrapper for conventional base point
3221 */
3222int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
3223 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3224 int (*f_rng)(void *, unsigned char *, size_t),
3225 void *p_rng )
3226{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003227 ECP_VALIDATE_RET( grp != NULL );
3228 ECP_VALIDATE_RET( d != NULL );
3229 ECP_VALIDATE_RET( Q != NULL );
3230 ECP_VALIDATE_RET( f_rng != NULL );
3231
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003232 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003233}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003234
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003235/*
3236 * Generate a keypair, prettier wrapper
3237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003239 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
3240{
Janos Follath24eed8d2019-11-22 13:21:35 +00003241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003242 ECP_VALIDATE_RET( key != NULL );
3243 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003244
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003245 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003246 return( ret );
3247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003249}
3250
Janos Follath77800962019-02-25 16:32:08 +00003251#define ECP_CURVE25519_KEY_SIZE 32
Janos Follath171a7ef2019-02-15 16:17:45 +00003252/*
3253 * Read a private key.
3254 */
3255int mbedtls_ecp_read_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3256 const unsigned char *buf, size_t buflen )
3257{
3258 int ret = 0;
3259
Janos Follath28eb06d2019-02-26 10:53:34 +00003260 ECP_VALIDATE_RET( key != NULL );
3261 ECP_VALIDATE_RET( buf != NULL );
3262
Janos Follath171a7ef2019-02-15 16:17:45 +00003263 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
3264 return( ret );
3265
Janos Follath28eb06d2019-02-26 10:53:34 +00003266 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3267
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003268#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003269 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follath171a7ef2019-02-15 16:17:45 +00003270 {
Janos Follath28eb06d2019-02-26 10:53:34 +00003271 /*
3272 * If it is Curve25519 curve then mask the key as mandated by RFC7748
3273 */
3274 if( grp_id == MBEDTLS_ECP_DP_CURVE25519 )
3275 {
3276 if( buflen != ECP_CURVE25519_KEY_SIZE )
3277 return MBEDTLS_ERR_ECP_INVALID_KEY;
Janos Follath171a7ef2019-02-15 16:17:45 +00003278
Janos Follath28eb06d2019-02-26 10:53:34 +00003279 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary_le( &key->d, buf, buflen ) );
Janos Follath171a7ef2019-02-15 16:17:45 +00003280
Janos Follath28eb06d2019-02-26 10:53:34 +00003281 /* Set the three least significant bits to 0 */
3282 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 0, 0 ) );
3283 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 1, 0 ) );
3284 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 2, 0 ) );
Janos Follath171a7ef2019-02-15 16:17:45 +00003285
Janos Follath28eb06d2019-02-26 10:53:34 +00003286 /* Set the most significant bit to 0 */
3287 MBEDTLS_MPI_CHK(
3288 mbedtls_mpi_set_bit( &key->d,
3289 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0 )
3290 );
Janos Follath171a7ef2019-02-15 16:17:45 +00003291
Janos Follath28eb06d2019-02-26 10:53:34 +00003292 /* Set the second most significant bit to 1 */
3293 MBEDTLS_MPI_CHK(
3294 mbedtls_mpi_set_bit( &key->d,
3295 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1 )
3296 );
3297 }
3298 else
3299 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Janos Follath171a7ef2019-02-15 16:17:45 +00003300 }
3301
3302#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003303#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003304 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Janos Follath171a7ef2019-02-15 16:17:45 +00003305 {
3306 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &key->d, buf, buflen ) );
3307
3308 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( &key->grp, &key->d ) );
3309 }
3310
3311#endif
3312cleanup:
3313
3314 if( ret != 0 )
3315 mbedtls_mpi_free( &key->d );
3316
3317 return( ret );
3318}
3319
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003320/*
Steven Cooremande8593f2020-06-09 19:55:26 +02003321 * Write a private key.
3322 */
Steven Cooreman0024df62020-07-13 10:59:40 +02003323int mbedtls_ecp_write_key( mbedtls_ecp_keypair *key,
Steven Cooremanc9b7f782020-06-11 17:00:36 +02003324 unsigned char *buf, size_t buflen )
Steven Cooremande8593f2020-06-09 19:55:26 +02003325{
Steven Cooreman0024df62020-07-13 10:59:40 +02003326 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooremande8593f2020-06-09 19:55:26 +02003327
Steven Cooreman0024df62020-07-13 10:59:40 +02003328 ECP_VALIDATE_RET( key != NULL );
3329 ECP_VALIDATE_RET( buf != NULL );
Steven Cooremande8593f2020-06-09 19:55:26 +02003330
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003331#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Steven Cooremande8593f2020-06-09 19:55:26 +02003332 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
3333 {
Steven Cooreman0024df62020-07-13 10:59:40 +02003334 if( key->grp.id == MBEDTLS_ECP_DP_CURVE25519 )
Steven Cooremande8593f2020-06-09 19:55:26 +02003335 {
3336 if( buflen < ECP_CURVE25519_KEY_SIZE )
3337 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3338
3339 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary_le( &key->d, buf, buflen ) );
Steven Cooremande8593f2020-06-09 19:55:26 +02003340 }
3341 else
3342 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3343 }
3344
3345#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003346#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Steven Cooremande8593f2020-06-09 19:55:26 +02003347 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
3348 {
3349 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &key->d, buf, buflen ) );
Steven Cooremande8593f2020-06-09 19:55:26 +02003350 }
3351
3352#endif
3353cleanup:
3354
3355 return( ret );
3356}
3357
3358
3359/*
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003360 * Check a public-private key pair
3361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003363{
Janos Follath24eed8d2019-11-22 13:21:35 +00003364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 mbedtls_ecp_point Q;
3366 mbedtls_ecp_group grp;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003367 ECP_VALIDATE_RET( pub != NULL );
3368 ECP_VALIDATE_RET( prv != NULL );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003371 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
3373 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
3374 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003377 }
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 mbedtls_ecp_point_init( &Q );
3380 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 /* mbedtls_ecp_mul() needs a non-const group... */
3383 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003384
3385 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
3389 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
3390 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003393 goto cleanup;
3394 }
3395
3396cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 mbedtls_ecp_point_free( &Q );
3398 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003399
3400 return( ret );
3401}
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003404
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003405/* Adjust the exponent to be a valid private point for the specified curve.
3406 * This is sometimes necessary because we use a single set of exponents
3407 * for all curves but the validity of values depends on the curve. */
Gilles Peskinea088c812018-09-17 18:31:15 +02003408static int self_test_adjust_exponent( const mbedtls_ecp_group *grp,
3409 mbedtls_mpi *m )
3410{
3411 int ret = 0;
3412 switch( grp->id )
3413 {
3414 /* If Curve25519 is available, then that's what we use for the
3415 * Montgomery test, so we don't need the adjustment code. */
3416#if ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
3417#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3418 case MBEDTLS_ECP_DP_CURVE448:
3419 /* Move highest bit from 254 to N-1. Setting bit N-1 is
3420 * necessary to enforce the highest-bit-set constraint. */
3421 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( m, 254, 0 ) );
3422 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( m, grp->nbits, 1 ) );
3423 /* Copy second-highest bit from 253 to N-2. This is not
3424 * necessary but improves the test variety a bit. */
3425 MBEDTLS_MPI_CHK(
3426 mbedtls_mpi_set_bit( m, grp->nbits - 1,
3427 mbedtls_mpi_get_bit( m, 253 ) ) );
3428 break;
3429#endif
3430#endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */
3431 default:
3432 /* Non-Montgomery curves and Curve25519 need no adjustment. */
3433 (void) grp;
3434 (void) m;
3435 goto cleanup;
3436 }
3437cleanup:
3438 return( ret );
3439}
3440
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003441/* Calculate R = m.P for each m in exponents. Check that the number of
3442 * basic operations doesn't depend on the value of m. */
Gilles Peskinec95696f2018-09-17 15:59:01 +02003443static int self_test_point( int verbose,
3444 mbedtls_ecp_group *grp,
3445 mbedtls_ecp_point *R,
3446 mbedtls_mpi *m,
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003447 const mbedtls_ecp_point *P,
Gilles Peskinec95696f2018-09-17 15:59:01 +02003448 const char *const *exponents,
3449 size_t n_exponents )
3450{
3451 int ret = 0;
Gilles Peskine24666792018-09-17 18:29:49 +02003452 size_t i = 0;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003453 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
3454 add_count = 0;
3455 dbl_count = 0;
3456 mul_count = 0;
Gilles Peskinea088c812018-09-17 18:31:15 +02003457
Gilles Peskinec95696f2018-09-17 15:59:01 +02003458 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( m, 16, exponents[0] ) );
Gilles Peskinea088c812018-09-17 18:31:15 +02003459 MBEDTLS_MPI_CHK( self_test_adjust_exponent( grp, m ) );
Gilles Peskinec95696f2018-09-17 15:59:01 +02003460 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) );
3461
3462 for( i = 1; i < n_exponents; i++ )
3463 {
3464 add_c_prev = add_count;
3465 dbl_c_prev = dbl_count;
3466 mul_c_prev = mul_count;
3467 add_count = 0;
3468 dbl_count = 0;
3469 mul_count = 0;
3470
3471 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( m, 16, exponents[i] ) );
Gilles Peskinea088c812018-09-17 18:31:15 +02003472 MBEDTLS_MPI_CHK( self_test_adjust_exponent( grp, m ) );
Gilles Peskinec95696f2018-09-17 15:59:01 +02003473 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) );
3474
3475 if( add_count != add_c_prev ||
3476 dbl_count != dbl_c_prev ||
3477 mul_count != mul_c_prev )
3478 {
3479 ret = 1;
3480 break;
3481 }
3482 }
3483
3484cleanup:
3485 if( verbose != 0 )
3486 {
3487 if( ret != 0 )
3488 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
3489 else
3490 mbedtls_printf( "passed\n" );
3491 }
3492 return( ret );
3493}
3494
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003495/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003496 * Checkup routine
3497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003499{
Janos Follath24eed8d2019-11-22 13:21:35 +00003500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 mbedtls_ecp_group grp;
3502 mbedtls_ecp_point R, P;
3503 mbedtls_mpi m;
Gilles Peskine24666792018-09-17 18:29:49 +02003504
3505#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskined9767a52018-09-14 19:29:47 +02003506 /* Exponents especially adapted for secp192k1, which has the lowest
3507 * order n of all supported curves (secp192r1 is in a slightly larger
3508 * field but the order of its base point is slightly smaller). */
Gilles Peskine24666792018-09-17 18:29:49 +02003509 const char *sw_exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003510 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003511 "000000000000000000000000000000000000000000000001", /* one */
Gilles Peskined9767a52018-09-14 19:29:47 +02003512 "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003513 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003514 "400000000000000000000000000000000000000000000000", /* one and zeros */
3515 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3516 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003517 };
Gilles Peskine24666792018-09-17 18:29:49 +02003518#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3519#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3520 const char *m_exponents[] =
3521 {
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003522 /* Valid private values for Curve25519. In a build with Curve448
3523 * but not Curve25519, they will be adjusted in
3524 * self_test_adjust_exponent(). */
Gilles Peskine24666792018-09-17 18:29:49 +02003525 "4000000000000000000000000000000000000000000000000000000000000000",
3526 "5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
3527 "5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
3528 "41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
3529 "5555555555555555555555555555555555555555555555555555555555555550",
3530 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
3531 };
3532#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 mbedtls_ecp_group_init( &grp );
3535 mbedtls_ecp_point_init( &R );
3536 mbedtls_ecp_point_init( &P );
3537 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003538
Gilles Peskine24666792018-09-17 18:29:49 +02003539#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003540 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003542 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003543#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003544 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003545#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003546
3547 if( verbose != 0 )
Gilles Peskine24666792018-09-17 18:29:49 +02003548 mbedtls_printf( " ECP SW test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003549 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
3551 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Gilles Peskinec95696f2018-09-17 15:59:01 +02003552 ret = self_test_point( verbose,
3553 &grp, &R, &m, &grp.G,
Gilles Peskine24666792018-09-17 18:29:49 +02003554 sw_exponents,
3555 sizeof( sw_exponents ) / sizeof( sw_exponents[0] ));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003556 if( ret != 0 )
3557 goto cleanup;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003558
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003559 if( verbose != 0 )
Gilles Peskine24666792018-09-17 18:29:49 +02003560 mbedtls_printf( " ECP SW test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003561 /* We computed P = 2G last time, use it */
Gilles Peskinec95696f2018-09-17 15:59:01 +02003562 ret = self_test_point( verbose,
3563 &grp, &R, &m, &P,
Gilles Peskine24666792018-09-17 18:29:49 +02003564 sw_exponents,
3565 sizeof( sw_exponents ) / sizeof( sw_exponents[0] ));
3566 if( ret != 0 )
3567 goto cleanup;
3568
3569 mbedtls_ecp_group_free( &grp );
3570 mbedtls_ecp_point_free( &R );
3571#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3572
3573#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3574 if( verbose != 0 )
3575 mbedtls_printf( " ECP Montgomery test (constant op_count): " );
3576#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
3577 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_CURVE25519 ) );
3578#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3579 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_CURVE448 ) );
3580#else
3581#error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
3582#endif
3583 ret = self_test_point( verbose,
3584 &grp, &R, &m, &grp.G,
3585 m_exponents,
3586 sizeof( m_exponents ) / sizeof( m_exponents[0] ));
3587 if( ret != 0 )
3588 goto cleanup;
3589#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003590
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003591cleanup:
3592
3593 if( ret < 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003594 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 mbedtls_ecp_group_free( &grp );
3597 mbedtls_ecp_point_free( &R );
3598 mbedtls_ecp_point_free( &P );
3599 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003600
3601 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003603
3604 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003605}
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003608
Janos Follathb0697532016-08-18 12:38:46 +01003609#endif /* !MBEDTLS_ECP_ALT */
3610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#endif /* MBEDTLS_ECP_C */