blob: a49cc457bcbba9551bba3f3beea70bca24a2a561 [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"
Paul Bakker6e339b52013-07-03 13:37:05 +020079
Janos Follath8c70e812021-06-24 14:48:38 +010080#include "bn_mul.h"
Gilles Peskine80ba8502021-04-03 20:36:37 +020081#include "ecp_invasive.h"
Jerry Yud3f73342021-09-09 15:42:32 +080082#include "ssl_misc.h"
Gilles Peskine80ba8502021-04-03 20:36:37 +020083
Rich Evans00ab4702015-02-06 13:43:58 +000084#include <string.h>
85
Janos Follathb0697532016-08-18 12:38:46 +010086#if !defined(MBEDTLS_ECP_ALT)
87
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088/* Parameter validation macros based on platform_util.h */
89#define ECP_VALIDATE_RET( cond ) \
90 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
91#define ECP_VALIDATE( cond ) \
92 MBEDTLS_INTERNAL_VALIDATE( cond )
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000095#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020096#else
Rich Evans00ab4702015-02-06 13:43:58 +000097#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000098#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200100#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +0200102#endif
103
Gilles Peskine6a2fb612021-05-24 22:25:04 +0200104#include "ecp_internal_alt.h"
Janos Follathb0697532016-08-18 12:38:46 +0100105
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +0100106#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
107 !defined(inline) && !defined(__cplusplus)
Paul Bakker6a6087e2013-10-28 18:53:08 +0100108#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +0200109#endif
Paul Bakker6a6087e2013-10-28 18:53:08 +0100110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100112/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100113 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200114 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100115 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100116static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100117#endif
118
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200119#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100120/*
121 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200122 *
123 * Default value 0 means that ECC operations will not yield.
124 * Note that regardless of the value of ecp_max_ops, always at
125 * least one step is performed before yielding.
126 *
127 * Setting ecp_max_ops=1 can be suitable for testing purposes
128 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100129 */
130static unsigned ecp_max_ops = 0;
131
132/*
133 * Set ecp_max_ops
134 */
135void mbedtls_ecp_set_max_ops( unsigned max_ops )
136{
137 ecp_max_ops = max_ops;
138}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100139
140/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200141 * Check if restart is enabled
142 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200143int mbedtls_ecp_restart_is_enabled( void )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200144{
145 return( ecp_max_ops != 0 );
146}
147
148/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200149 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100150 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200151struct mbedtls_ecp_restart_mul
152{
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100153 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100154 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100155 mbedtls_ecp_point *T; /* table for precomputed points */
156 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200157 enum { /* what were we doing last time we returned? */
158 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
159 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100160 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
161 ecp_rsm_pre_add, /* precompute remaining points by adding */
162 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200163 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100164 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100165 } state;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100166};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100167
168/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200169 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100170 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200171static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100172{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200173 mbedtls_ecp_point_init( &ctx->R );
174 ctx->i = 0;
175 ctx->T = NULL;
176 ctx->T_size = 0;
177 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100178}
179
180/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200181 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100182 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200183static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100184{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100185 unsigned char i;
186
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100187 if( ctx == NULL )
188 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100189
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100190 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100191
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200192 if( ctx->T != NULL )
193 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100194 for( i = 0; i < ctx->T_size; i++ )
195 mbedtls_ecp_point_free( ctx->T + i );
196 mbedtls_free( ctx->T );
197 }
198
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200199 ecp_restart_rsm_init( ctx );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100200}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100201
202/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200203 * Restart context for ecp_muladd()
204 */
205struct mbedtls_ecp_restart_muladd
206{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200207 mbedtls_ecp_point mP; /* mP value */
208 mbedtls_ecp_point R; /* R intermediate result */
209 enum { /* what should we do next? */
210 ecp_rsma_mul1 = 0, /* first multiplication */
211 ecp_rsma_mul2, /* second multiplication */
212 ecp_rsma_add, /* addition */
213 ecp_rsma_norm, /* normalization */
214 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200215};
216
217/*
218 * Init restart_muladd sub-context
219 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200220static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200221{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200222 mbedtls_ecp_point_init( &ctx->mP );
223 mbedtls_ecp_point_init( &ctx->R );
224 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200225}
226
227/*
228 * Free the components of a restart_muladd sub-context
229 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200230static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200231{
232 if( ctx == NULL )
233 return;
234
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200235 mbedtls_ecp_point_free( &ctx->mP );
236 mbedtls_ecp_point_free( &ctx->R );
237
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200238 ecp_restart_ma_init( ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200239}
240
241/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200242 * Initialize a restart context
243 */
244void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
245{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246 ECP_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200247 ctx->ops_done = 0;
248 ctx->depth = 0;
249 ctx->rsm = NULL;
250 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200251}
252
253/*
254 * Free the components of a restart context
255 */
256void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
257{
258 if( ctx == NULL )
259 return;
260
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200261 ecp_restart_rsm_free( ctx->rsm );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200262 mbedtls_free( ctx->rsm );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200263
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200264 ecp_restart_ma_free( ctx->ma );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200265 mbedtls_free( ctx->ma );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200266
267 mbedtls_ecp_restart_init( ctx );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200268}
269
270/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100271 * Check if we can do the next step
272 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200273int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
274 mbedtls_ecp_restart_ctx *rs_ctx,
275 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100276{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277 ECP_VALIDATE_RET( grp != NULL );
278
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200279 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100280 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100281 /* scale depending on curve size: the chosen reference is 256-bit,
282 * and multiplication is quadratic. Round to the closest integer. */
283 if( grp->pbits >= 512 )
284 ops *= 4;
285 else if( grp->pbits >= 384 )
286 ops *= 2;
287
Hanno Beckerb10c6602018-10-26 13:50:13 +0100288 /* Avoid infinite loops: always allow first step.
289 * Because of that, however, it's not generally true
290 * that ops_done <= ecp_max_ops, so the check
291 * ops_done > ecp_max_ops below is mandatory. */
292 if( ( rs_ctx->ops_done != 0 ) &&
293 ( rs_ctx->ops_done > ecp_max_ops ||
294 ops > ecp_max_ops - rs_ctx->ops_done ) )
295 {
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100296 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
Hanno Beckerb10c6602018-10-26 13:50:13 +0100297 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100298
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100299 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200300 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100301 }
302
303 return( 0 );
304}
305
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200306/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200307#define ECP_RS_ENTER( SUB ) do { \
308 /* reset ops count for this call if top-level */ \
309 if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
310 rs_ctx->ops_done = 0; \
311 \
312 /* set up our own sub-context if needed */ \
313 if( mbedtls_ecp_restart_is_enabled() && \
314 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
315 { \
316 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
317 if( rs_ctx->SUB == NULL ) \
318 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
319 \
320 ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
321 } \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200322} while( 0 )
323
324/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200325#define ECP_RS_LEAVE( SUB ) do { \
326 /* clear our sub-context when not in progress (done or error) */ \
327 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
328 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
329 { \
330 ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
331 mbedtls_free( rs_ctx->SUB ); \
332 rs_ctx->SUB = NULL; \
333 } \
334 \
335 if( rs_ctx != NULL ) \
336 rs_ctx->depth--; \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200337} while( 0 )
338
339#else /* MBEDTLS_ECP_RESTARTABLE */
340
341#define ECP_RS_ENTER( sub ) (void) rs_ctx;
342#define ECP_RS_LEAVE( sub ) (void) rs_ctx;
343
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200344#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100345
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100346/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200347 * List of supported curves:
348 * - internal ID
Christoph M. Wintersteigercb310732019-02-15 15:50:38 +0000349 * - 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 +0200350 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200351 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100352 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100353 * Curves are listed in order: largest curves first, and for a given size,
Gilles Peskineae270bf2021-06-02 00:05:29 +0200354 * fastest curves first.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200355 *
Gilles Peskineae270bf2021-06-02 00:05:29 +0200356 * Reminder: update profiles in x509_crt.c and ssl_tls.c when adding a new curve!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
361 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
364 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
367 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
370 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
373 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
376 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100377#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
379 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
382 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
385 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
388 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
391 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100392#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200393#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Christoph M. Wintersteiger86e36c42018-12-06 17:27:31 +0000394 { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100395#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200396#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
397 { MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200400};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100401
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200402#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
403 sizeof( ecp_supported_curves[0] )
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200406
407/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200408 * List of supported curves and associated info
409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200411{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200412 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200413}
414
415/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100416 * List of supported curves, group ID only
417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100419{
420 static int init_done = 0;
421
422 if( ! init_done )
423 {
424 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 for( curve_info = mbedtls_ecp_curve_list();
428 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100429 curve_info++ )
430 {
431 ecp_supported_grp_id[i++] = curve_info->grp_id;
432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100434
435 init_done = 1;
436 }
437
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200438 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100439}
440
441/*
442 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444const 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 +0200445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 for( curve_info = mbedtls_ecp_curve_list();
449 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200450 curve_info++ )
451 {
452 if( curve_info->grp_id == grp_id )
453 return( curve_info );
454 }
455
456 return( NULL );
457}
458
459/*
460 * Get the curve info from the TLS identifier
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 for( curve_info = mbedtls_ecp_curve_list();
467 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200468 curve_info++ )
469 {
470 if( curve_info->tls_id == tls_id )
471 return( curve_info );
472 }
473
474 return( NULL );
475}
476
477/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100478 * Get the curve info from the name
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100483
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 if( name == NULL )
485 return( NULL );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 for( curve_info = mbedtls_ecp_curve_list();
488 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100489 curve_info++ )
490 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200491 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100492 return( curve_info );
493 }
494
495 return( NULL );
496}
497
498/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100499 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100500 */
Janos Follathdf9295b2019-02-26 12:36:52 +0000501mbedtls_ecp_curve_type mbedtls_ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100502{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100503 if( grp->G.X.p == NULL )
Janos Follathdf9295b2019-02-26 12:36:52 +0000504 return( MBEDTLS_ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100505
506 if( grp->G.Y.p == NULL )
Janos Follathdf9295b2019-02-26 12:36:52 +0000507 return( MBEDTLS_ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100508 else
Janos Follathdf9295b2019-02-26 12:36:52 +0000509 return( MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100510}
511
512/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100513 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100516{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500517 ECP_VALIDATE( pt != NULL );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_mpi_init( &pt->X );
520 mbedtls_mpi_init( &pt->Y );
521 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100522}
523
524/*
525 * Initialize (the components of) a group
526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100528{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500529 ECP_VALIDATE( grp != NULL );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100530
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200531 grp->id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200532 mbedtls_mpi_init( &grp->P );
533 mbedtls_mpi_init( &grp->A );
534 mbedtls_mpi_init( &grp->B );
535 mbedtls_ecp_point_init( &grp->G );
536 mbedtls_mpi_init( &grp->N );
537 grp->pbits = 0;
538 grp->nbits = 0;
539 grp->h = 0;
540 grp->modp = NULL;
541 grp->t_pre = NULL;
542 grp->t_post = NULL;
543 grp->t_data = NULL;
544 grp->T = NULL;
545 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100546}
547
548/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200549 * Initialize (the components of) a key pair
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200552{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500553 ECP_VALIDATE( key != NULL );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_ecp_group_init( &key->grp );
556 mbedtls_mpi_init( &key->d );
557 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200558}
559
560/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100561 * Unallocate (the components of) a point
562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100564{
565 if( pt == NULL )
566 return;
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_free( &( pt->X ) );
569 mbedtls_mpi_free( &( pt->Y ) );
570 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100571}
572
573/*
kXuanba9cb762021-04-08 14:32:06 +0800574 * Check that the comb table (grp->T) is static initialized.
575 */
576static int ecp_group_is_static_comb_table( const mbedtls_ecp_group *grp ) {
577#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
578 return grp->T != NULL && grp->T_size == 0;
579#else
580 (void) grp;
581 return 0;
582#endif
583}
584
585/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100586 * Unallocate (the components of) a group
587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100589{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200590 size_t i;
591
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100592 if( grp == NULL )
593 return;
594
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100595 if( grp->h != 1 )
596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_mpi_free( &grp->P );
598 mbedtls_mpi_free( &grp->A );
599 mbedtls_mpi_free( &grp->B );
600 mbedtls_ecp_point_free( &grp->G );
601 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100602 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200603
kXuanba9cb762021-04-08 14:32:06 +0800604 if( !ecp_group_is_static_comb_table(grp) && grp->T != NULL )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200605 {
606 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_ecp_point_free( &grp->T[i] );
608 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200609 }
610
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500611 mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100612}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100613
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100614/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200615 * Unallocate (the components of) a key pair
616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200618{
Paul Bakker66d5d072014-06-17 16:39:18 +0200619 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200620 return;
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_ecp_group_free( &key->grp );
623 mbedtls_mpi_free( &key->d );
624 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200625}
626
627/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200628 * Copy the contents of a point
629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200631{
Janos Follath24eed8d2019-11-22 13:21:35 +0000632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500633 ECP_VALIDATE_RET( P != NULL );
634 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
637 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
638 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200639
640cleanup:
641 return( ret );
642}
643
644/*
645 * Copy the contents of a group object
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200648{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500649 ECP_VALIDATE_RET( dst != NULL );
650 ECP_VALIDATE_RET( src != NULL );
651
652 return( mbedtls_ecp_group_load( dst, src->id ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200653}
654
655/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100656 * Set point to zero
657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100659{
Janos Follath24eed8d2019-11-22 13:21:35 +0000660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500661 ECP_VALIDATE_RET( pt != NULL );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
664 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
665 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100666
667cleanup:
668 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100669}
670
671/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100672 * Tell if a point is zero
673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100675{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500676 ECP_VALIDATE_RET( pt != NULL );
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100679}
680
681/*
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500682 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200683 */
684int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
685 const mbedtls_ecp_point *Q )
686{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500687 ECP_VALIDATE_RET( P != NULL );
688 ECP_VALIDATE_RET( Q != NULL );
689
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200690 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
691 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
692 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
693 {
694 return( 0 );
695 }
696
697 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
698}
699
700/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100701 * Import a non-zero point from ASCII strings
702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100704 const char *x, const char *y )
705{
Janos Follath24eed8d2019-11-22 13:21:35 +0000706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500707 ECP_VALIDATE_RET( P != NULL );
708 ECP_VALIDATE_RET( x != NULL );
709 ECP_VALIDATE_RET( y != NULL );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
712 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
713 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100714
715cleanup:
716 return( ret );
717}
718
719/*
Janos Follath7caf8e42019-02-20 12:00:22 +0000720 * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100721 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500722int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp,
723 const mbedtls_ecp_point *P,
724 int format, size_t *olen,
725 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100726{
Janos Follath28eb06d2019-02-26 10:53:34 +0000727 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100728 size_t plen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500729 ECP_VALIDATE_RET( grp != NULL );
730 ECP_VALIDATE_RET( P != NULL );
731 ECP_VALIDATE_RET( olen != NULL );
732 ECP_VALIDATE_RET( buf != NULL );
733 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
734 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100737
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200738#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine59970052019-02-28 13:12:06 +0100739 (void) format; /* Montgomery curves always use the same point format */
Janos Follathdf9295b2019-02-26 12:36:52 +0000740 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100741 {
Janos Follath7caf8e42019-02-20 12:00:22 +0000742 *olen = plen;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100743 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100745
Janos Follath7caf8e42019-02-20 12:00:22 +0000746 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary_le( &P->X, buf, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100747 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000748#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200749#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +0000750 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100751 {
Janos Follath7caf8e42019-02-20 12:00:22 +0000752 /*
753 * Common case: P == 0
754 */
755 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
756 {
757 if( buflen < 1 )
758 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100759
Janos Follath7caf8e42019-02-20 12:00:22 +0000760 buf[0] = 0x00;
761 *olen = 1;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100762
Janos Follath7caf8e42019-02-20 12:00:22 +0000763 return( 0 );
764 }
765
766 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
767 {
768 *olen = 2 * plen + 1;
769
770 if( buflen < *olen )
771 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
772
773 buf[0] = 0x04;
774 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
775 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
776 }
777 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
778 {
779 *olen = plen + 1;
780
781 if( buflen < *olen )
782 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
783
784 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
785 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
786 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100787 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000788#endif
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100789
790cleanup:
791 return( ret );
792}
793
794/*
Janos Follath59b813c2019-02-13 10:44:06 +0000795 * Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100796 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500797int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
798 mbedtls_ecp_point *pt,
799 const unsigned char *buf, size_t ilen )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100800{
Janos Follath28eb06d2019-02-26 10:53:34 +0000801 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100802 size_t plen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500803 ECP_VALIDATE_RET( grp != NULL );
804 ECP_VALIDATE_RET( pt != NULL );
805 ECP_VALIDATE_RET( buf != NULL );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100806
Paul Bakker82788fb2014-10-20 13:59:19 +0200807 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100811
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200812#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +0000813 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follath59b813c2019-02-13 10:44:06 +0000814 {
815 if( plen != ilen )
816 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100817
Janos Follath59b813c2019-02-13 10:44:06 +0000818 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary_le( &pt->X, buf, plen ) );
819 mbedtls_mpi_free( &pt->Y );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100820
Janos Follath59b813c2019-02-13 10:44:06 +0000821 if( grp->id == MBEDTLS_ECP_DP_CURVE25519 )
Janos Follathffbd7e82019-02-25 11:35:20 +0000822 /* Set most significant bit to 0 as prescribed in RFC7748 §5 */
Janos Follath59b813c2019-02-13 10:44:06 +0000823 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &pt->X, plen * 8 - 1, 0 ) );
Janos Follath28eb06d2019-02-26 10:53:34 +0000824
825 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Janos Follath59b813c2019-02-13 10:44:06 +0000826 }
827#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200828#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +0000829 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Janos Follath59b813c2019-02-13 10:44:06 +0000830 {
831 if( buf[0] == 0x00 )
832 {
833 if( ilen == 1 )
834 return( mbedtls_ecp_set_zero( pt ) );
835 else
836 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
837 }
838
839 if( buf[0] != 0x04 )
840 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
841
842 if( ilen != 2 * plen + 1 )
843 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
844
845 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y,
847 buf + 1 + plen, plen ) );
Janos Follath28eb06d2019-02-26 10:53:34 +0000848 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Janos Follath59b813c2019-02-13 10:44:06 +0000849 }
850#endif
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100851
852cleanup:
853 return( ret );
854}
855
856/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100857 * Import a point from a TLS ECPoint record (RFC 4492)
858 * struct {
859 * opaque point <1..2^8-1>;
860 * } ECPoint;
861 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500862int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
863 mbedtls_ecp_point *pt,
864 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100865{
866 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100867 const unsigned char *buf_start;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500868 ECP_VALIDATE_RET( grp != NULL );
869 ECP_VALIDATE_RET( pt != NULL );
870 ECP_VALIDATE_RET( buf != NULL );
871 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100872
873 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200874 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100875 */
876 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100878
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100879 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100880 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100882
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100883 /*
884 * Save buffer start for read_binary and update buf
885 */
886 buf_start = *buf;
887 *buf += data_len;
888
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500889 return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100890}
891
892/*
893 * Export a point as a TLS ECPoint record (RFC 4492)
894 * struct {
895 * opaque point <1..2^8-1>;
896 * } ECPoint;
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100899 int format, size_t *olen,
900 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100901{
Janos Follath24eed8d2019-11-22 13:21:35 +0000902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500903 ECP_VALIDATE_RET( grp != NULL );
904 ECP_VALIDATE_RET( pt != NULL );
905 ECP_VALIDATE_RET( olen != NULL );
906 ECP_VALIDATE_RET( buf != NULL );
907 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
908 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100909
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100910 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100911 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100912 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100913 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100917 olen, buf + 1, blen - 1) ) != 0 )
918 return( ret );
919
920 /*
921 * write length to the first byte and update total length
922 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200923 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100924 ++*olen;
925
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200926 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100927}
928
929/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100930 * Set a group from an ECParameters record (RFC 4492)
931 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500932int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
933 const unsigned char **buf, size_t len )
934{
Janos Follath24eed8d2019-11-22 13:21:35 +0000935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500936 mbedtls_ecp_group_id grp_id;
937 ECP_VALIDATE_RET( grp != NULL );
938 ECP_VALIDATE_RET( buf != NULL );
939 ECP_VALIDATE_RET( *buf != NULL );
940
941 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 )
942 return( ret );
943
944 return( mbedtls_ecp_group_load( grp, grp_id ) );
945}
946
947/*
948 * Read a group id from an ECParameters record (RFC 4492) and convert it to
949 * mbedtls_ecp_group_id.
950 */
951int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
952 const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100953{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200954 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500956 ECP_VALIDATE_RET( grp != NULL );
957 ECP_VALIDATE_RET( buf != NULL );
958 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100959
960 /*
961 * We expect at least three bytes (see below)
962 */
963 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100965
966 /*
967 * First byte is curve_type; only named_curve is handled
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
970 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100971
972 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100973 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100974 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200975 tls_id = *(*buf)++;
976 tls_id <<= 8;
977 tls_id |= *(*buf)++;
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
980 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200981
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500982 *grp = curve_info->grp_id;
983
984 return( 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100985}
986
987/*
988 * Write the ECParameters record corresponding to a group (RFC 4492)
989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100991 unsigned char *buf, size_t blen )
992{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500994 ECP_VALIDATE_RET( grp != NULL );
995 ECP_VALIDATE_RET( buf != NULL );
996 ECP_VALIDATE_RET( olen != NULL );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
999 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001000
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001001 /*
1002 * We are going to write 3 bytes (see below)
1003 */
1004 *olen = 3;
1005 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001007
1008 /*
1009 * First byte is curve_type, always named_curve
1010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001012
1013 /*
1014 * Next two bytes are the namedcurve value
1015 */
Joe Subbianid0687852021-07-21 15:22:47 +01001016 MBEDTLS_PUT_UINT16_BE( curve_info->tls_id, buf, 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001017
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001018 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001019}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001020
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001021/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1023 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001024 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 * 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 +02001026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001028{
Janos Follath24eed8d2019-11-22 13:21:35 +00001029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001030
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001031 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001033
1034 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001036 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001039 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001042
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001043 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
1045 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001048 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001050
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001051cleanup:
1052 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001053}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001054
Jerry Yud3f73342021-09-09 15:42:32 +08001055#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1056
1057int mbedtls_ecp_tls13_read_point( const mbedtls_ecp_group *grp,
1058 mbedtls_ecp_point *pt,
1059 const unsigned char **buf, size_t buf_len )
1060{
1061 unsigned char data_len;
1062 const unsigned char *buf_start;
1063 ECP_VALIDATE_RET( grp != NULL );
1064 ECP_VALIDATE_RET( pt != NULL );
1065 ECP_VALIDATE_RET( buf != NULL );
1066 ECP_VALIDATE_RET( *buf != NULL );
1067
1068 if( buf_len < 3 )
1069 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
1070
1071 data_len = ( *( *buf ) << 8 ) | *( *buf+1 );
1072 *buf += 2;
1073
1074 if( data_len < 1 || data_len > buf_len - 2 )
1075 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
1076
1077 /*
1078 * Save buffer start for read_binary and update buf
1079 */
1080 buf_start = *buf;
1081 *buf += data_len;
1082
1083 return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
1084}
1085
1086#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1087
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001088/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001089 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001090 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001091 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 * 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 +01001093 * bring the result back to this range.
1094 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001095 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001096 */
1097
1098/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 * 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 +01001100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001102#define INC_MUL_COUNT mul_count++;
1103#else
1104#define INC_MUL_COUNT
1105#endif
1106
Hanno Becker1eeca412018-10-15 12:01:35 +01001107#define MOD_MUL( N ) \
1108 do \
1109 { \
1110 MBEDTLS_MPI_CHK( ecp_modp( &(N), grp ) ); \
1111 INC_MUL_COUNT \
1112 } while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001113
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001114static inline int mbedtls_mpi_mul_mod( const mbedtls_ecp_group *grp,
1115 mbedtls_mpi *X,
1116 const mbedtls_mpi *A,
1117 const mbedtls_mpi *B )
1118{
Janos Follath24eed8d2019-11-22 13:21:35 +00001119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001120 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( X, A, B ) );
1121 MOD_MUL( *X );
1122cleanup:
1123 return( ret );
1124}
1125
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001126/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001128 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001129 */
Hanno Becker1eeca412018-10-15 12:01:35 +01001130#define MOD_SUB( N ) \
1131 while( (N).s < 0 && mbedtls_mpi_cmp_int( &(N), 0 ) != 0 ) \
1132 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001133
Steven Cooremane5388962021-03-01 14:04:53 +01001134#if ( defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
1135 !( defined(MBEDTLS_ECP_NO_FALLBACK) && \
1136 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1137 defined(MBEDTLS_ECP_ADD_MIXED_ALT) ) ) || \
1138 ( defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) && \
1139 !( defined(MBEDTLS_ECP_NO_FALLBACK) && \
1140 defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) ) )
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001141static inline int mbedtls_mpi_sub_mod( const mbedtls_ecp_group *grp,
1142 mbedtls_mpi *X,
1143 const mbedtls_mpi *A,
1144 const mbedtls_mpi *B )
1145{
Janos Follath24eed8d2019-11-22 13:21:35 +00001146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001147 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( X, A, B ) );
1148 MOD_SUB( *X );
1149cleanup:
1150 return( ret );
1151}
Steven Cooremane5388962021-03-01 14:04:53 +01001152#endif /* All functions referencing mbedtls_mpi_sub_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001153
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001154/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 * 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 +02001156 * We known P, N and the result are positive, so sub_abs is correct, and
1157 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001158 */
Hanno Becker1eeca412018-10-15 12:01:35 +01001159#define MOD_ADD( N ) \
1160 while( mbedtls_mpi_cmp_mpi( &(N), &grp->P ) >= 0 ) \
1161 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001162
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001163static inline int mbedtls_mpi_add_mod( const mbedtls_ecp_group *grp,
1164 mbedtls_mpi *X,
1165 const mbedtls_mpi *A,
1166 const mbedtls_mpi *B )
1167{
Janos Follath24eed8d2019-11-22 13:21:35 +00001168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001169 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, A, B ) );
1170 MOD_ADD( *X );
1171cleanup:
1172 return( ret );
1173}
1174
Steven Cooremane5388962021-03-01 14:04:53 +01001175#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
1176 !( defined(MBEDTLS_ECP_NO_FALLBACK) && \
1177 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1178 defined(MBEDTLS_ECP_ADD_MIXED_ALT) )
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001179static inline int mbedtls_mpi_shift_l_mod( const mbedtls_ecp_group *grp,
1180 mbedtls_mpi *X,
1181 size_t count )
1182{
Janos Follath24eed8d2019-11-22 13:21:35 +00001183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001184 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, count ) );
1185 MOD_ADD( *X );
1186cleanup:
1187 return( ret );
1188}
Steven Cooremane5388962021-03-01 14:04:53 +01001189#endif /* All functions referencing mbedtls_mpi_shift_l_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001190
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02001191#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001192/*
1193 * For curves in short Weierstrass form, we do all the internal operations in
1194 * Jacobian coordinates.
1195 *
1196 * For multiplication, we'll use a comb method with coutermeasueres against
1197 * SPA, hence timing attacks.
1198 */
1199
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001200/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001201 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001202 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001207 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001208
Janos Follathb0697532016-08-18 12:38:46 +01001209#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001210 if( mbedtls_internal_ecp_grp_capable( grp ) )
1211 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +01001212#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001213
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001214#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
1215 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1216#else
1217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1218 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001220
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001221 /*
1222 * X = X / Z^2 mod p
1223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001225 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ZZi, &Zi, &Zi ) );
1226 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->X, &pt->X, &ZZi ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001227
1228 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001229 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001230 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001231 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &ZZi ) );
1232 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &Zi ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001233
1234 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001235 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001238
1239cleanup:
1240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001242
1243 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001244#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001245}
1246
1247/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001248 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001249 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001250 * (See for example Cohen's "A Course in Computational Algebraic Number
1251 * Theory", Algorithm 10.3.4.)
1252 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001253 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001254 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001255 *
1256 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001259 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001260{
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001261 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001262 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001263
Janos Follathb0697532016-08-18 12:38:46 +01001264#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001265 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001266 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +01001267#endif
1268
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001269#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
1270 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1271#else
1272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1273 size_t i;
1274 mbedtls_mpi *c, u, Zi, ZZi;
1275
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001276 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001277 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001278
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001279 for( i = 0; i < T_size; i++ )
1280 mbedtls_mpi_init( &c[i] );
1281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001283
1284 /*
1285 * c[i] = Z_0 * ... * Z_i
1286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001288 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001289 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001290 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 +01001291 }
1292
1293 /*
1294 * u = 1 / (Z_0 * ... * Z_n) mod P
1295 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001296 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001297
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001298 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001299 {
1300 /*
1301 * Zi = 1 / Z_i mod p
1302 * u = 1 / (Z_0 * ... * Z_i) mod P
1303 */
1304 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001306 }
1307 else
1308 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001309 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &Zi, &u, &c[i-1] ) );
1310 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &u, &u, &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001311 }
1312
1313 /*
1314 * proceed as in normalize()
1315 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001316 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ZZi, &Zi, &Zi ) );
1317 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->X, &T[i]->X, &ZZi ) );
1318 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->Y, &T[i]->Y, &ZZi ) );
1319 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->Y, &T[i]->Y, &Zi ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001320
1321 /*
1322 * Post-precessing: reclaim some memory by shrinking coordinates
1323 * - not storing Z (always 1)
1324 * - shrinking other coordinates, but still keeping the same number of
1325 * limbs as P, as otherwise it will too likely be regrown too fast.
1326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1328 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1329 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001330
1331 if( i == 0 )
1332 break;
1333 }
1334
1335cleanup:
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001338 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_mpi_free( &c[i] );
1340 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001341
1342 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001343#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) */
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001344}
1345
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001346/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001347 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1348 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1351 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001352 unsigned char inv )
1353{
Janos Follath24eed8d2019-11-22 13:21:35 +00001354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001355 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001359
1360 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1362 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1363 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001364
1365cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001367
1368 return( ret );
1369}
1370
1371/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001372 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001373 *
Peter Dettmance661b22015-02-07 14:43:51 +07001374 * 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 +01001375 *
Peter Dettmance661b22015-02-07 14:43:51 +07001376 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1377 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1378 *
1379 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1380 *
1381 * Cost: 1D := 3M + 4S (A == 0)
1382 * 4M + 4S (A == -3)
1383 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1386 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001389 dbl_count++;
1390#endif
1391
Janos Follathb0697532016-08-18 12:38:46 +01001392#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001393 if( mbedtls_internal_ecp_grp_capable( grp ) )
1394 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001395#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001396
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001397#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
1398 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1399#else
1400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1401 mbedtls_mpi M, S, T, U;
1402
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001403 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 +01001404
1405 /* Special case for A = -3 */
1406 if( grp->A.p == NULL )
1407 {
Peter Dettmance661b22015-02-07 14:43:51 +07001408 /* M = 3(X + Z^2)(X - Z^2) */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001409 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->Z, &P->Z ) );
1410 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &T, &P->X, &S ) );
1411 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &U, &P->X, &S ) );
1412 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &T, &U ) );
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001413 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001414 }
1415 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001416 {
Peter Dettmance661b22015-02-07 14:43:51 +07001417 /* M = 3.X^2 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001418 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->X, &P->X ) );
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001419 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001420
1421 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001422 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001423 {
1424 /* M += A.Z^4 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001425 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->Z, &P->Z ) );
1426 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &S, &S ) );
1427 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &T, &grp->A ) );
1428 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &M, &M, &S ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001429 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001430 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001431
Peter Dettmance661b22015-02-07 14:43:51 +07001432 /* S = 4.X.Y^2 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &P->Y, &P->Y ) );
1434 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &T, 1 ) );
1435 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->X, &T ) );
1436 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &S, 1 ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001437
Peter Dettmance661b22015-02-07 14:43:51 +07001438 /* U = 8.Y^4 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001439 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &U, &T, &T ) );
1440 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &U, 1 ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001441
1442 /* T = M^2 - 2.S */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001443 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &M, &M ) );
1444 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T, &T, &S ) );
1445 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T, &T, &S ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001446
1447 /* S = M(S - T) - U */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001448 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S, &S, &T ) );
1449 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &S, &M ) );
1450 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S, &S, &U ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001451
1452 /* U = 2.Y.Z */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001453 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &U, &P->Y, &P->Z ) );
1454 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &U, 1 ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001455
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1457 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1458 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001459
1460cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001461 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 +02001462
1463 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001464#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) */
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001465}
1466
1467/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001468 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001469 *
1470 * The coordinates of Q must be normalized (= affine),
1471 * but those of P don't need to. R is not normalized.
1472 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001473 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001474 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001475 * - at each step, P, Q and R are multiples of the base point, the factor
1476 * being less than its order, so none of them is zero;
1477 * - Q is an odd multiple of the base point, P an even multiple,
1478 * due to the choice of precomputed points in the modified comb method.
1479 * So branches for these cases do not leak secret information.
1480 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001481 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1482 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001483 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1486 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001489 add_count++;
1490#endif
1491
Janos Follathb0697532016-08-18 12:38:46 +01001492#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001493 if( mbedtls_internal_ecp_grp_capable( grp ) )
1494 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001495#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001496
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001497#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_ADD_MIXED_ALT)
1498 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1499#else
1500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1501 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
1502
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001503 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001504 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
1507 return( mbedtls_ecp_copy( R, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
1510 return( mbedtls_ecp_copy( R, P ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001511
1512 /*
1513 * Make sure Q coordinates are normalized
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
1516 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
1519 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001520
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001521 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T1, &P->Z, &P->Z ) );
1522 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T2, &T1, &P->Z ) );
1523 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T1, &T1, &Q->X ) );
1524 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T2, &T2, &Q->Y ) );
1525 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T1, &T1, &P->X ) );
1526 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T2, &T2, &P->Y ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001527
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001528 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001532 {
1533 ret = ecp_double_jac( grp, R, P );
1534 goto cleanup;
1535 }
1536 else
1537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 ret = mbedtls_ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001539 goto cleanup;
1540 }
1541 }
1542
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001543 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &Z, &P->Z, &T1 ) );
1544 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T1, &T1 ) );
1545 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T4, &T3, &T1 ) );
1546 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T3, &P->X ) );
1547 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &T3 ) );
1548 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &T1, 1 ) );
1549 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &X, &T2, &T2 ) );
1550 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &X, &X, &T1 ) );
1551 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &X, &X, &T4 ) );
1552 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T3, &T3, &X ) );
1553 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T3, &T2 ) );
1554 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T4, &T4, &P->Y ) );
1555 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &Y, &T3, &T4 ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1558 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1559 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001560
1561cleanup:
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1564 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001565
1566 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001567#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001568}
1569
1570/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001571 * Randomize jacobian coordinates:
1572 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001573 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001574 *
1575 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001578 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1579{
Janos Follathb0697532016-08-18 12:38:46 +01001580#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001581 if( mbedtls_internal_ecp_grp_capable( grp ) )
1582 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001583#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001584
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001585#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
1586 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1587#else
1588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1589 mbedtls_mpi l, ll;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001592
1593 /* Generate l such that 1 < l < p */
Gilles Peskine59215172021-03-29 22:28:50 +02001594 MBEDTLS_MPI_CHK( mbedtls_mpi_random( &l, 2, &grp->P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001595
1596 /* Z = l * Z */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001597 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Z, &pt->Z, &l ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001598
1599 /* X = l^2 * X */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001600 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ll, &l, &l ) );
1601 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->X, &pt->X, &ll ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001602
1603 /* Y = l^3 * Y */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001604 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ll, &ll, &l ) );
1605 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &ll ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001606
1607cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001609
Gilles Peskine59215172021-03-29 22:28:50 +02001610 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
1611 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001612 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001613#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) */
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001614}
1615
1616/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001617 * Check and define parameters used by the comb method (see below for details)
1618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1620#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001621#endif
1622
1623/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001625
1626/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001628
1629/*
1630 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001631 *
1632 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001633 * modified version that provides resistance to SPA by avoiding zero
1634 * digits in the representation as in [3]. We modify the method further by
1635 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001636 * representation uses one more K_i, due to carries, but saves on the size of
1637 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001638 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001639 * Summary of the comb method and its modifications:
1640 *
1641 * - The goal is to compute m*P for some w*d-bit integer m.
1642 *
1643 * - The basic comb method splits m into the w-bit integers
1644 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1645 * index has residue i modulo d, and computes m * P as
1646 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1647 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1648 *
1649 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1650 * .. + 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]] ..,
1651 * thereby successively converting it into a form where all summands
1652 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1653 *
1654 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1655 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1656 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1657 * Performing and iterating this procedure for those x[i] that are even
1658 * (keeping track of carry), we can transform the original sum into one of the form
1659 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1660 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1661 * which is why we are only computing half of it in the first place in
1662 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1663 *
1664 * - For the sake of compactness, only the seven low-order bits of x[i]
1665 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001666 * 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 +02001667 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001668 *
1669 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001670 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001671 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001673 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1674 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001675 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001676static void ecp_comb_recode_core( unsigned char x[], size_t d,
1677 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001678{
1679 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001680 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001681
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001682 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001683
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001684 /* First get the classical comb values (except for x_d = 0) */
1685 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001686 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001688
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001689 /* Now make sure x_1 .. x_d are odd */
1690 c = 0;
1691 for( i = 1; i <= d; i++ )
1692 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001693 /* Add carry and update it */
1694 cc = x[i] & c;
1695 x[i] = x[i] ^ c;
1696 c = cc;
1697
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001698 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001699 adjust = 1 - ( x[i] & 0x01 );
1700 c |= x[i] & ( x[i-1] * adjust );
1701 x[i] = x[i] ^ ( x[i-1] * adjust );
1702 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001703 }
1704}
1705
1706/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001707 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001708 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001709 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001710 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001711 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1712 * 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 +01001713 *
1714 * 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 +02001715 *
1716 * Note: Even comb values (those where P would be omitted from the
1717 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001718 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001719 *
1720 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001721 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001722 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1723 * (3) [add] Computation of all T[i]
1724 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001725 *
1726 * Step 1 can be interrupted but not the others; together with the final
1727 * coordinate normalization they are the largest steps done at once, depending
1728 * on the window size. Here are operation counts for P-256:
1729 *
1730 * step (2) (3) (4)
1731 * w = 5 142 165 208
1732 * w = 4 136 77 160
1733 * w = 3 130 33 136
1734 * w = 2 124 11 124
1735 *
1736 * So if ECC operations are blocking for too long even with a low max_ops
1737 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1738 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1741 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001742 unsigned char w, size_t d,
1743 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001744{
Janos Follath24eed8d2019-11-22 13:21:35 +00001745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001746 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001747 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001748 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001750
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001751#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001752 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001753 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001754 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
1755 goto dbl;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001756 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001757 goto norm_dbl;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001758 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
1759 goto add;
1760 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
1761 goto norm_add;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001762 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001763#else
1764 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001765#endif
1766
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001767#if defined(MBEDTLS_ECP_RESTARTABLE)
1768 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1769 {
1770 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1771
1772 /* initial state for the loop */
1773 rs_ctx->rsm->i = 0;
1774 }
1775
1776dbl:
1777#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001778 /*
1779 * Set T[0] = P and
1780 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001783
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001784#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001785 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
1786 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001787 else
1788#endif
1789 j = 0;
1790
1791 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001792 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001793 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001794
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001795 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001796 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001797
1798 if( j % d == 0 )
1799 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1800
1801 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001802 }
1803
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001804#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001805 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1806 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1807
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001808norm_dbl:
1809#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001810 /*
1811 * Normalize current elements in T. As T has holes,
1812 * use an auxiliary array of pointers to elements in T.
1813 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001814 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001815 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001816 TT[j++] = T + i;
1817
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001818 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001819
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001820 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001821
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001822#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001823 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1824 rs_ctx->rsm->state = ecp_rsm_pre_add;
1825
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001826add:
1827#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001828 /*
1829 * Compute the remaining ones using the minimal number of additions
1830 * Be careful to update T[2^l] only after using it!
1831 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001832 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001833
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001834 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001835 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001836 j = i;
1837 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001839 }
1840
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001841#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001842 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1843 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1844
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001845norm_add:
1846#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001847 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001848 * Normalize final elements in T. Even though there are no holes now, we
1849 * still need the auxiliary array for homogeneity with the previous
1850 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001851 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001852 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001853 TT[j] = T + j + 1;
1854
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001855 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001856
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001857 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001858
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001859cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001860#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001861 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1862 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001863 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001864 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001865 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001866 }
1867#endif
Janos Follathb0697532016-08-18 12:38:46 +01001868
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001869 return( ret );
1870}
1871
1872/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001873 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001874 *
1875 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001878 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001879 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001880{
Janos Follath24eed8d2019-11-22 13:21:35 +00001881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001882 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001883
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001884 /* Ignore the "sign" bit and scale down */
1885 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001886
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001887 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001888 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1891 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001892 }
1893
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001894 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001896
1897cleanup:
1898 return( ret );
1899}
1900
1901/*
1902 * Core multiplication algorithm for the (modified) comb method.
1903 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001904 *
1905 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001908 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001909 const unsigned char x[], size_t d,
1910 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001911 void *p_rng,
1912 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001913{
Janos Follath24eed8d2019-11-22 13:21:35 +00001914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001916 size_t i;
1917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001919
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001920#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001921 (void) rs_ctx;
1922#endif
1923
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001924#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001925 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1926 rs_ctx->rsm->state != ecp_rsm_comb_core )
1927 {
1928 rs_ctx->rsm->i = 0;
1929 rs_ctx->rsm->state = ecp_rsm_comb_core;
1930 }
1931
1932 /* new 'if' instead of nested for the sake of the 'else' branch */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001933 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001934 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001935 /* restore current index (R already pointing to rs_ctx->rsm->R) */
1936 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001937 }
1938 else
1939#endif
1940 {
1941 /* Start with a non-zero point and randomize its coordinates */
1942 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001943 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001944 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
1945 if( f_rng != 0 )
1946 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
1947 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001948
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001949 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001950 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001951 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001952 --i;
1953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001955 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001957 }
1958
1959cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001962
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001963#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001964 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1965 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001966 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001967 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001968 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001969 }
1970#endif
1971
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001972 return( ret );
1973}
1974
1975/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001976 * Recode the scalar to get constant-time comb multiplication
1977 *
1978 * As the actual scalar recoding needs an odd scalar as a starting point,
1979 * this wrapper ensures that by replacing m by N - m if necessary, and
1980 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001981 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001982 * This works because we only support large prime order for Short Weierstrass
1983 * curves, so N is always odd hence either m or N - m is.
1984 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001985 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001986 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001987static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
1988 const mbedtls_mpi *m,
1989 unsigned char k[COMB_MAX_D + 1],
1990 size_t d,
1991 unsigned char w,
1992 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001993{
Janos Follath24eed8d2019-11-22 13:21:35 +00001994 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001995 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001996
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001997 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001998 mbedtls_mpi_init( &mm );
1999
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002000 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002001 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
2002 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2003
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002004 /* do we need the parity trick? */
2005 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
2006
2007 /* execute parity fix in constant time */
2008 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002009 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002010 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
2011
2012 /* actual scalar recoding */
2013 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002014
2015cleanup:
2016 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002017 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002018
2019 return( ret );
2020}
2021
2022/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002023 * Perform comb multiplication (for short Weierstrass curves)
2024 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002025 *
2026 * Scalar recoding may use a parity trick that makes us compute -m * P,
2027 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002028 */
2029static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
2030 mbedtls_ecp_point *R,
2031 const mbedtls_mpi *m,
2032 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002033 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002034 unsigned char w,
2035 size_t d,
2036 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002037 void *p_rng,
2038 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002039{
Janos Follath24eed8d2019-11-22 13:21:35 +00002040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002041 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002042 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002043 mbedtls_ecp_point *RR = R;
2044
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002045#if defined(MBEDTLS_ECP_RESTARTABLE)
2046 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2047 {
2048 RR = &rs_ctx->rsm->R;
2049
2050 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
2051 goto final_norm;
2052 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002053#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002054
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002055 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
2056 &parity_trick ) );
2057 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
2058 f_rng, p_rng, rs_ctx ) );
2059 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
2060
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002061#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002062 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002063 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002064
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002065final_norm:
Manuel Pégourié-Gonnard9b8d34e2020-06-08 09:53:20 +02002066 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002067#endif
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002068 /*
2069 * Knowledge of the jacobian coordinates may leak the last few bits of the
2070 * scalar [1], and since our MPI implementation isn't constant-flow,
2071 * inversion (used for coordinate normalization) may leak the full value
2072 * of its input via side-channels [2].
2073 *
2074 * [1] https://eprint.iacr.org/2003/191
2075 * [2] https://eprint.iacr.org/2020/055
2076 *
2077 * Avoid the leak by randomizing coordinates before we normalize them.
2078 */
2079 if( f_rng != 0 )
2080 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
2081
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002082 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
2083
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002084#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02002085 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2086 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002087#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002088
2089cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002090 return( ret );
2091}
2092
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002093/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002094 * Pick window size based on curve size and whether we optimize for base point
2095 */
2096static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
2097 unsigned char p_eq_g )
2098{
2099 unsigned char w;
2100
2101 /*
2102 * Minimize the number of multiplications, that is minimize
2103 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2104 * (see costs of the various parts, with 1S = 1M)
2105 */
2106 w = grp->nbits >= 384 ? 5 : 4;
2107
2108 /*
2109 * If P == G, pre-compute a bit more, since this may be re-used later.
2110 * Just adding one avoids upping the cost of the first mul too much,
2111 * and the memory cost too.
2112 */
2113 if( p_eq_g )
2114 w++;
2115
2116 /*
kXuanba9cb762021-04-08 14:32:06 +08002117 * If static comb table may not be used (!p_eq_g) or static comb table does
2118 * not exists, make sure w is within bounds.
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002119 * (The last test is useful only for very small curves in the test suite.)
kXuanba9cb762021-04-08 14:32:06 +08002120 *
2121 * The user reduces MBEDTLS_ECP_WINDOW_SIZE does not changes the size of
2122 * static comb table, because the size of static comb table is fixed when
2123 * it is generated.
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002124 */
k-stachowiak653a4a22019-07-03 14:31:09 +02002125#if( MBEDTLS_ECP_WINDOW_SIZE < 6 )
kXuanba9cb762021-04-08 14:32:06 +08002126 if( (!p_eq_g || !ecp_group_is_static_comb_table(grp)) && w > MBEDTLS_ECP_WINDOW_SIZE )
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002127 w = MBEDTLS_ECP_WINDOW_SIZE;
k-stachowiak653a4a22019-07-03 14:31:09 +02002128#endif
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002129 if( w >= grp->nbits )
2130 w = 2;
2131
2132 return( w );
2133}
2134
2135/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002136 * Multiplication using the comb method - for curves in short Weierstrass form
2137 *
2138 * This function is mainly responsible for administrative work:
2139 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002140 * - managing the table of precomputed points (passed between the below two
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002141 * functions): allocation, computation, ownership tranfer, freeing.
2142 *
2143 * It delegates the actual arithmetic work to:
2144 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2145 *
2146 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2149 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002150 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002151 void *p_rng,
2152 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002153{
Janos Follath24eed8d2019-11-22 13:21:35 +00002154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002155 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002156 size_t d;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002157 unsigned char T_size = 0, T_ok = 0;
2158 mbedtls_ecp_point *T = NULL;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002159
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002160 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002161
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002162 /* Is P the base point ? */
2163#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2164 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
2165 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002166#else
2167 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002168#endif
2169
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002170 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002171 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002172 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002173 d = ( grp->nbits + w - 1 ) / w;
2174
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002175 /* Pre-computed table: do we have it already for the base point? */
2176 if( p_eq_g && grp->T != NULL )
2177 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002178 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002179 T = grp->T;
2180 T_ok = 1;
2181 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002182 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002183#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002184 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002185 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002186 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002187 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002188 T = rs_ctx->rsm->T;
2189 rs_ctx->rsm->T = NULL;
2190 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002191
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002192 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002193 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002194 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002195 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002196#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002197 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002198 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002199 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002200 if( T == NULL )
2201 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002202 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002203 goto cleanup;
2204 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002205
2206 for( i = 0; i < T_size; i++ )
2207 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002208
2209 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002210 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002211
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002212 /* Compute table (or finish computing it) if not done already */
2213 if( !T_ok )
2214 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002215 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002216
2217 if( p_eq_g )
2218 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002219 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002220 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002221 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002222 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002223 }
2224 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002225
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002226 /* Actual comb multiplication using precomputed points */
2227 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002228 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002229 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002230
2231cleanup:
2232
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002233 /* does T belong to the group? */
2234 if( T == grp->T )
2235 T = NULL;
2236
2237 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002238#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002239 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 +01002240 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002241 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002242 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002243 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002244 T = NULL;
2245 }
2246#endif
2247
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002248 /* did T belong to us? then let's destroy it! */
2249 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002250 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002251 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 mbedtls_ecp_point_free( &T[i] );
2253 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002254 }
2255
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002256 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002257#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002258 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2259#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002260 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01002261 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002263
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002264 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002265
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002266 return( ret );
2267}
2268
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002269#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002270
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002271#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002272/*
2273 * For Montgomery curves, we do all the internal arithmetic in projective
2274 * coordinates. Import/export of points uses only the x coordinates, which is
2275 * internaly represented as X / Z.
2276 *
2277 * For scalar multiplication, we'll use a Montgomery ladder.
2278 */
2279
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002280/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002281 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2282 * Cost: 1M + 1I
2283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002285{
Janos Follathb0697532016-08-18 12:38:46 +01002286#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002287 if( mbedtls_internal_ecp_grp_capable( grp ) )
2288 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002289#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002290
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002291#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
2292 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2293#else
2294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002296 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->X, &P->X, &P->Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002298
2299cleanup:
2300 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002301#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002302}
2303
2304/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002305 * Randomize projective x/z coordinates:
2306 * (X, Z) -> (l X, l Z) for random l
2307 * This is sort of the reverse operation of ecp_normalize_mxz().
2308 *
2309 * This countermeasure was first suggested in [2].
2310 * Cost: 2M
2311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002313 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2314{
Janos Follathb0697532016-08-18 12:38:46 +01002315#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002316 if( mbedtls_internal_ecp_grp_capable( grp ) )
Steven Cooremand4bfb3e2021-03-11 13:18:29 +01002317 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01002318#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002319
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002320#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
2321 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2322#else
2323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2324 mbedtls_mpi l;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002326
2327 /* Generate l such that 1 < l < p */
Gilles Peskine59215172021-03-29 22:28:50 +02002328 MBEDTLS_MPI_CHK( mbedtls_mpi_random( &l, 2, &grp->P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002329
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002330 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->X, &P->X, &l ) );
2331 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->Z, &P->Z, &l ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002332
2333cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002335
Gilles Peskine59215172021-03-29 22:28:50 +02002336 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2337 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002338 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002339#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002340}
2341
2342/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002343 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2344 * for Montgomery curves in x/z coordinates.
2345 *
2346 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2347 * with
2348 * d = X1
2349 * P = (X2, Z2)
2350 * Q = (X3, Z3)
2351 * R = (X4, Z4)
2352 * S = (X5, Z5)
2353 * and eliminating temporary variables tO, ..., t4.
2354 *
2355 * Cost: 5M + 4S
2356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2358 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2359 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2360 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002361{
Janos Follathb0697532016-08-18 12:38:46 +01002362#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002363 if( mbedtls_internal_ecp_grp_capable( grp ) )
2364 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002365#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002366
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002367#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
2368 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2369#else
2370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2371 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2374 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2375 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002376
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002377 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &A, &P->X, &P->Z ) );
2378 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &AA, &A, &A ) );
2379 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &B, &P->X, &P->Z ) );
2380 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &BB, &B, &B ) );
2381 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &E, &AA, &BB ) );
2382 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &C, &Q->X, &Q->Z ) );
2383 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &D, &Q->X, &Q->Z ) );
2384 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &DA, &D, &A ) );
2385 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &CB, &C, &B ) );
Aurelien Jarno66deb382020-04-20 21:36:48 +02002386 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &S->X, &DA, &CB ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002387 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->X, &S->X, &S->X ) );
2388 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S->Z, &DA, &CB ) );
2389 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->Z, &S->Z, &S->Z ) );
2390 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->Z, d, &S->Z ) );
2391 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->X, &AA, &BB ) );
2392 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->Z, &grp->A, &E ) );
2393 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &R->Z, &BB, &R->Z ) );
2394 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->Z, &E, &R->Z ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002395
2396cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2398 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2399 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002400
2401 return( ret );
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002402#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002403}
2404
2405/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002406 * Multiplication with Montgomery ladder in x/z coordinates,
2407 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2410 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002411 int (*f_rng)(void *, unsigned char *, size_t),
2412 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002413{
Janos Follath24eed8d2019-11-22 13:21:35 +00002414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002415 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002416 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 mbedtls_ecp_point RP;
2418 mbedtls_mpi PX;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002420
Manuel Pégourié-Gonnard02b57052021-06-15 11:29:26 +02002421 if( f_rng == NULL )
2422 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2423
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002424 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2426 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002427
2428 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2430 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2431 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002432
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002433 /* RP.X might be sligtly larger than P, so reduce it */
2434 MOD_ADD( RP.X );
2435
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002436 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard02b57052021-06-15 11:29:26 +02002437 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002438
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002439 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002440 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002441 while( i-- > 0 )
2442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002444 /*
2445 * if (b) R = 2R + P else R = 2R,
2446 * which is:
2447 * if (b) double_add( RP, R, RP, R )
2448 * else double_add( R, RP, R, RP )
2449 * but using safe conditional swaps to avoid leaks
2450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2452 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2453 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2454 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2455 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002456 }
2457
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002458 /*
2459 * Knowledge of the projective coordinates may leak the last few bits of the
2460 * scalar [1], and since our MPI implementation isn't constant-flow,
2461 * inversion (used for coordinate normalization) may leak the full value
2462 * of its input via side-channels [2].
2463 *
2464 * [1] https://eprint.iacr.org/2003/191
2465 * [2] https://eprint.iacr.org/2020/055
2466 *
2467 * Avoid the leak by randomizing coordinates before we normalize them.
2468 */
Manuel Pégourié-Gonnard02b57052021-06-15 11:29:26 +02002469 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002471
2472cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002474
2475 return( ret );
2476}
2477
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002478#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002479
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002480/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002481 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002482 *
2483 * This internal function can be called without an RNG in case where we know
2484 * the inputs are not sensitive.
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002485 */
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002486static int ecp_mul_restartable_internal( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002488 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2489 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002490{
Janos Follathb0697532016-08-18 12:38:46 +01002491 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002492#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2493 char is_grp_capable = 0;
2494#endif
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02002495
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002496#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002497 /* reset ops count for this call if top-level */
2498 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2499 rs_ctx->ops_done = 0;
Gilles Peskine59970052019-02-28 13:12:06 +01002500#else
2501 (void) rs_ctx;
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002502#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002503
Janos Follathc44ab972016-11-18 16:38:23 +00002504#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002505 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002506 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002507#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002508
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002509#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002510 /* skip argument check when restarting */
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002511 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002512#endif
2513 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002514 /* check_privkey is free */
2515 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2516
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002517 /* Common sanity checks */
2518 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2519 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002520 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002521
2522 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002523#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002524 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002525 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002526#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002527#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002528 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002529 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002530#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002531
Janos Follath6c8ccd52016-11-29 15:37:09 +00002532cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002533
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002534#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002535 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002536 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002537#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002538
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002539#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002540 if( rs_ctx != NULL )
2541 rs_ctx->depth--;
2542#endif
2543
Janos Follathb0697532016-08-18 12:38:46 +01002544 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002545}
2546
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002547/*
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002548 * Restartable multiplication R = m * P
2549 */
2550int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2551 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2552 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2553 mbedtls_ecp_restart_ctx *rs_ctx )
2554{
2555 ECP_VALIDATE_RET( grp != NULL );
2556 ECP_VALIDATE_RET( R != NULL );
2557 ECP_VALIDATE_RET( m != NULL );
2558 ECP_VALIDATE_RET( P != NULL );
2559
2560 if( f_rng == NULL )
2561 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2562
2563 return( ecp_mul_restartable_internal( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
2564}
2565
2566/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002567 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002568 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002569int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002570 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002571 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002572{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002573 ECP_VALIDATE_RET( grp != NULL );
2574 ECP_VALIDATE_RET( R != NULL );
2575 ECP_VALIDATE_RET( m != NULL );
2576 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002577 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002578}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002579
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002580#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002581/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002582 * Check that an affine point is valid as a public key,
2583 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002586{
Janos Follath24eed8d2019-11-22 13:21:35 +00002587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002589
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002590 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2592 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2593 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2594 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2595 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002598
2599 /*
2600 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002601 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002602 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002603 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &YY, &pt->Y, &pt->Y ) );
2604 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &RHS, &pt->X, &pt->X ) );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002605
2606 /* Special case for A = -3 */
2607 if( grp->A.p == NULL )
2608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002610 }
2611 else
2612 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002613 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &RHS, &RHS, &grp->A ) );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002614 }
2615
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002616 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &RHS, &RHS, &pt->X ) );
2617 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &RHS, &RHS, &grp->B ) );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2620 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002621
2622cleanup:
2623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002625
2626 return( ret );
2627}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002628#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002629
Gilles Peskine9b99a892018-09-14 18:32:19 +02002630#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002631/*
TRodziewicz9edff742021-03-04 17:59:39 +01002632 * R = m * P with shortcuts for m == 0, m == 1 and m == -1
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002633 * NOT constant-time - ONLY for short Weierstrass!
2634 */
2635static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2636 mbedtls_ecp_point *R,
2637 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002638 const mbedtls_ecp_point *P,
2639 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002640{
Janos Follath24eed8d2019-11-22 13:21:35 +00002641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002642
TRodziewicz782a7ea2021-03-17 11:35:16 +01002643 if( mbedtls_mpi_cmp_int( m, 0 ) == 0 )
TRodziewicz9edff742021-03-04 17:59:39 +01002644 {
2645 MBEDTLS_MPI_CHK( mbedtls_ecp_set_zero( R ) );
2646 }
2647 else if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002648 {
2649 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2650 }
2651 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2652 {
2653 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2654 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2655 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2656 }
2657 else
2658 {
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002659 MBEDTLS_MPI_CHK( ecp_mul_restartable_internal( grp, R, m, P,
2660 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002661 }
2662
2663cleanup:
2664 return( ret );
2665}
2666
2667/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002668 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002669 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002670 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002671int mbedtls_ecp_muladd_restartable(
2672 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002673 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002674 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2675 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002676{
Janos Follath24eed8d2019-11-22 13:21:35 +00002677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002678 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002679 mbedtls_ecp_point *pmP = &mP;
2680 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002681#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2682 char is_grp_capable = 0;
2683#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002684 ECP_VALIDATE_RET( grp != NULL );
2685 ECP_VALIDATE_RET( R != NULL );
2686 ECP_VALIDATE_RET( m != NULL );
2687 ECP_VALIDATE_RET( P != NULL );
2688 ECP_VALIDATE_RET( n != NULL );
2689 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002690
Janos Follathdf9295b2019-02-26 12:36:52 +00002691 if( mbedtls_ecp_get_type( grp ) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002692 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2693
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002694 mbedtls_ecp_point_init( &mP );
2695
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002696 ECP_RS_ENTER( ma );
2697
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002698#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002699 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2700 {
2701 /* redirect intermediate results to restart context */
2702 pmP = &rs_ctx->ma->mP;
2703 pR = &rs_ctx->ma->R;
2704
2705 /* jump to next operation */
2706 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2707 goto mul2;
2708 if( rs_ctx->ma->state == ecp_rsma_add )
2709 goto add;
2710 if( rs_ctx->ma->state == ecp_rsma_norm )
2711 goto norm;
2712 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002713#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002714
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002715 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002716#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002717 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002718 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002719
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002720mul2:
2721#endif
2722 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002723
2724#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2725 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2726 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2727#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2728
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002729#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002730 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002731 rs_ctx->ma->state = ecp_rsma_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002732
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002733add:
2734#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002735 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002736 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002737#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002738 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002739 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002740
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002741norm:
2742#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002743 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002744 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2745
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002746#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002747 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2748 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2749#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002750
2751cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002752#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002753 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002754 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002755#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002756
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002757 mbedtls_ecp_point_free( &mP );
2758
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002759 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002760
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002761 return( ret );
2762}
2763
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002764/*
2765 * Linear combination
2766 * NOT constant-time
2767 */
2768int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2769 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2770 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2771{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002772 ECP_VALIDATE_RET( grp != NULL );
2773 ECP_VALIDATE_RET( R != NULL );
2774 ECP_VALIDATE_RET( m != NULL );
2775 ECP_VALIDATE_RET( P != NULL );
2776 ECP_VALIDATE_RET( n != NULL );
2777 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002778 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2779}
Gilles Peskine9b99a892018-09-14 18:32:19 +02002780#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002781
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002782#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002783#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002784#define ECP_MPI_INIT(s, n, p) {s, (n), (mbedtls_mpi_uint *)(p)}
2785#define ECP_MPI_INIT_ARRAY(x) \
2786 ECP_MPI_INIT(1, sizeof(x) / sizeof(mbedtls_mpi_uint), x)
2787/*
2788 * Constants for the two points other than 0, 1, -1 (mod p) in
2789 * https://cr.yp.to/ecdh.html#validate
2790 * See ecp_check_pubkey_x25519().
2791 */
2792static const mbedtls_mpi_uint x25519_bad_point_1[] = {
Janos Follath1107ee42021-06-25 12:43:26 +01002793 MBEDTLS_BYTES_TO_T_UINT_8( 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae ),
2794 MBEDTLS_BYTES_TO_T_UINT_8( 0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a ),
2795 MBEDTLS_BYTES_TO_T_UINT_8( 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd ),
2796 MBEDTLS_BYTES_TO_T_UINT_8( 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00 ),
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002797};
2798static const mbedtls_mpi_uint x25519_bad_point_2[] = {
Janos Follath1107ee42021-06-25 12:43:26 +01002799 MBEDTLS_BYTES_TO_T_UINT_8( 0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24 ),
2800 MBEDTLS_BYTES_TO_T_UINT_8( 0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b ),
2801 MBEDTLS_BYTES_TO_T_UINT_8( 0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86 ),
2802 MBEDTLS_BYTES_TO_T_UINT_8( 0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57 ),
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002803};
2804static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
2805 x25519_bad_point_1 );
2806static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
2807 x25519_bad_point_2 );
Janos Follath865a75e2021-06-24 15:34:59 +01002808#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002809
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002810/*
2811 * Check that the input point is not one of the low-order points.
2812 * This is recommended by the "May the Fourth" paper:
2813 * https://eprint.iacr.org/2017/806.pdf
2814 * Those points are never sent by an honest peer.
2815 */
Janos Follath865a75e2021-06-24 15:34:59 +01002816static int ecp_check_bad_points_mx( const mbedtls_mpi *X, const mbedtls_mpi *P,
2817 const mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002818{
2819 int ret;
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002820 mbedtls_mpi XmP;
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002821
2822 mbedtls_mpi_init( &XmP );
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002823
2824 /* Reduce X mod P so that we only need to check values less than P.
2825 * We know X < 2^256 so we can proceed by subtraction. */
2826 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &XmP, X ) );
2827 while( mbedtls_mpi_cmp_mpi( &XmP, P ) >= 0 )
2828 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &XmP, &XmP, P ) );
2829
Janos Follath865a75e2021-06-24 15:34:59 +01002830 /* Check against the known bad values that are less than P. For Curve448
2831 * these are 0, 1 and -1. For Curve25519 we check the values less than P
2832 * from the following list: https://cr.yp.to/ecdh.html#validate */
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002833 if( mbedtls_mpi_cmp_int( &XmP, 1 ) <= 0 ) /* takes care of 0 and 1 */
Janos Follath8081ced2021-06-24 14:24:13 +01002834 {
2835 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2836 goto cleanup;
2837 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002838
Janos Follath865a75e2021-06-24 15:34:59 +01002839#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
2840 if( grp_id == MBEDTLS_ECP_DP_CURVE25519 )
Janos Follath8081ced2021-06-24 14:24:13 +01002841 {
Janos Follath865a75e2021-06-24 15:34:59 +01002842 if( mbedtls_mpi_cmp_mpi( &XmP, &ecp_x25519_bad_point_1 ) == 0 )
2843 {
2844 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2845 goto cleanup;
2846 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002847
Janos Follath865a75e2021-06-24 15:34:59 +01002848 if( mbedtls_mpi_cmp_mpi( &XmP, &ecp_x25519_bad_point_2 ) == 0 )
2849 {
2850 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2851 goto cleanup;
2852 }
Janos Follath8081ced2021-06-24 14:24:13 +01002853 }
Janos Follath83e384d2021-06-25 15:29:56 +01002854#else
2855 (void) grp_id;
Janos Follath865a75e2021-06-24 15:34:59 +01002856#endif
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002857
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002858 /* Final check: check if XmP + 1 is P (final because it changes XmP!) */
2859 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &XmP, &XmP, 1 ) );
2860 if( mbedtls_mpi_cmp_mpi( &XmP, P ) == 0 )
Janos Follath8081ced2021-06-24 14:24:13 +01002861 {
2862 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2863 goto cleanup;
2864 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002865
2866 ret = 0;
2867
2868cleanup:
2869 mbedtls_mpi_free( &XmP );
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002870
2871 return( ret );
2872}
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002873
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002874/*
2875 * Check validity of a public key for Montgomery curves with x-only schemes
2876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002878{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002879 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002880 /* Allow any public value, if it's too big then we'll just reduce it mod p
2881 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2883 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002884
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002885 /* Implicit in all standards (as they don't consider negative numbers):
2886 * X must be non-negative. This is normally ensured by the way it's
2887 * encoded for transmission, but let's be extra sure. */
2888 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 )
2889 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2890
Janos Follath865a75e2021-06-24 15:34:59 +01002891 return( ecp_check_bad_points_mx( &pt->X, &grp->P, grp->id ) );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002892}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002893#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002894
2895/*
2896 * Check that a point is valid as a public key
2897 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002898int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
2899 const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002900{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002901 ECP_VALIDATE_RET( grp != NULL );
2902 ECP_VALIDATE_RET( pt != NULL );
2903
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002904 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2906 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002907
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002908#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002909 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002910 return( ecp_check_pubkey_mx( grp, pt ) );
2911#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002912#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002913 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002914 return( ecp_check_pubkey_sw( grp, pt ) );
2915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002917}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002918
2919/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002921 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002922int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
2923 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002924{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002925 ECP_VALIDATE_RET( grp != NULL );
2926 ECP_VALIDATE_RET( d != NULL );
2927
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002928#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002929 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002930 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002931 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2933 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002934 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002936
2937 /* see [Curve25519] page 5 */
2938 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
2939 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2940
2941 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002942 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002943#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2944#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00002945 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002946 {
2947 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2949 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2950 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002951 else
2952 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002953 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002954#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002957}
2958
Gilles Peskine72fcc982021-03-23 22:31:31 +01002959#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2960MBEDTLS_STATIC_TESTABLE
Gilles Peskine55c46042021-03-24 12:34:40 +01002961int mbedtls_ecp_gen_privkey_mx( size_t high_bit,
Gilles Peskine72fcc982021-03-23 22:31:31 +01002962 mbedtls_mpi *d,
2963 int (*f_rng)(void *, unsigned char *, size_t),
2964 void *p_rng )
2965{
2966 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskine61f1f5f2021-03-24 12:46:46 +01002967 size_t n_random_bytes = high_bit / 8 + 1;
Gilles Peskine72fcc982021-03-23 22:31:31 +01002968
2969 /* [Curve25519] page 5 */
Gilles Peskine61f1f5f2021-03-24 12:46:46 +01002970 /* Generate a (high_bit+1)-bit random number by generating just enough
2971 * random bytes, then shifting out extra bits from the top (necessary
2972 * when (high_bit+1) is not a multiple of 8). */
2973 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_random_bytes,
2974 f_rng, p_rng ) );
2975 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_random_bytes - high_bit - 1 ) );
Gilles Peskine72fcc982021-03-23 22:31:31 +01002976
Gilles Peskine67986d02021-03-24 12:25:59 +01002977 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, high_bit, 1 ) );
Gilles Peskine72fcc982021-03-23 22:31:31 +01002978
2979 /* Make sure the last two bits are unset for Curve448, three bits for
2980 Curve25519 */
2981 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
2982 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Gilles Peskine55c46042021-03-24 12:34:40 +01002983 if( high_bit == 254 )
Gilles Peskine72fcc982021-03-23 22:31:31 +01002984 {
2985 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
2986 }
2987
2988cleanup:
2989 return( ret );
2990}
2991#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2992
Gilles Peskine60d8b982021-03-29 22:28:21 +02002993#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2994static int mbedtls_ecp_gen_privkey_sw(
2995 const mbedtls_mpi *N, mbedtls_mpi *d,
2996 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2997{
2998 int ret = mbedtls_mpi_random( d, 1, N, f_rng, p_rng );
2999 switch( ret )
3000 {
3001 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
3002 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
3003 default:
3004 return( ret );
3005 }
3006}
3007#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3008
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003009/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003010 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003011 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003012int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
3013 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003014 int (*f_rng)(void *, unsigned char *, size_t),
3015 void *p_rng )
3016{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003017 ECP_VALIDATE_RET( grp != NULL );
3018 ECP_VALIDATE_RET( d != NULL );
3019 ECP_VALIDATE_RET( f_rng != NULL );
3020
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003021#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003022 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Gilles Peskine72fcc982021-03-23 22:31:31 +01003023 return( mbedtls_ecp_gen_privkey_mx( grp->nbits, d, f_rng, p_rng ) );
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003024#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003025
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003026#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003027 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Gilles Peskine60d8b982021-03-29 22:28:21 +02003028 return( mbedtls_ecp_gen_privkey_sw( &grp->N, d, f_rng, p_rng ) );
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003029#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003030
Gilles Peskine72fcc982021-03-23 22:31:31 +01003031 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003032}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01003033
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003034/*
3035 * Generate a keypair with configurable base point
3036 */
3037int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
3038 const mbedtls_ecp_point *G,
3039 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3040 int (*f_rng)(void *, unsigned char *, size_t),
3041 void *p_rng )
3042{
Janos Follath24eed8d2019-11-22 13:21:35 +00003043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003044 ECP_VALIDATE_RET( grp != NULL );
3045 ECP_VALIDATE_RET( d != NULL );
3046 ECP_VALIDATE_RET( G != NULL );
3047 ECP_VALIDATE_RET( Q != NULL );
3048 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003049
3050 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
3051 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
3052
3053cleanup:
3054 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003055}
3056
3057/*
3058 * Generate key pair, wrapper for conventional base point
3059 */
3060int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
3061 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3062 int (*f_rng)(void *, unsigned char *, size_t),
3063 void *p_rng )
3064{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003065 ECP_VALIDATE_RET( grp != NULL );
3066 ECP_VALIDATE_RET( d != NULL );
3067 ECP_VALIDATE_RET( Q != NULL );
3068 ECP_VALIDATE_RET( f_rng != NULL );
3069
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003070 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003071}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003072
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003073/*
3074 * Generate a keypair, prettier wrapper
3075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003077 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
3078{
Janos Follath24eed8d2019-11-22 13:21:35 +00003079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003080 ECP_VALIDATE_RET( key != NULL );
3081 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003082
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003083 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003084 return( ret );
3085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003087}
3088
Janos Follath77800962019-02-25 16:32:08 +00003089#define ECP_CURVE25519_KEY_SIZE 32
Archana1d2e2bb2021-06-07 06:13:16 +05303090#define ECP_CURVE448_KEY_SIZE 56
Janos Follath171a7ef2019-02-15 16:17:45 +00003091/*
3092 * Read a private key.
3093 */
3094int mbedtls_ecp_read_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3095 const unsigned char *buf, size_t buflen )
3096{
3097 int ret = 0;
3098
Janos Follath28eb06d2019-02-26 10:53:34 +00003099 ECP_VALIDATE_RET( key != NULL );
3100 ECP_VALIDATE_RET( buf != NULL );
3101
Janos Follath171a7ef2019-02-15 16:17:45 +00003102 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
3103 return( ret );
3104
Janos Follath28eb06d2019-02-26 10:53:34 +00003105 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3106
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003107#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003108 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follath171a7ef2019-02-15 16:17:45 +00003109 {
Janos Follath28eb06d2019-02-26 10:53:34 +00003110 /*
Archana1d2e2bb2021-06-07 06:13:16 +05303111 * Mask the key as mandated by RFC7748 for Curve25519 and Curve448.
Janos Follath28eb06d2019-02-26 10:53:34 +00003112 */
3113 if( grp_id == MBEDTLS_ECP_DP_CURVE25519 )
3114 {
3115 if( buflen != ECP_CURVE25519_KEY_SIZE )
Archana277572f2021-07-12 09:00:57 +05303116 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Janos Follath171a7ef2019-02-15 16:17:45 +00003117
Janos Follath28eb06d2019-02-26 10:53:34 +00003118 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary_le( &key->d, buf, buflen ) );
Janos Follath171a7ef2019-02-15 16:17:45 +00003119
Janos Follath28eb06d2019-02-26 10:53:34 +00003120 /* Set the three least significant bits to 0 */
3121 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 0, 0 ) );
3122 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 1, 0 ) );
3123 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 2, 0 ) );
Janos Follath171a7ef2019-02-15 16:17:45 +00003124
Janos Follath28eb06d2019-02-26 10:53:34 +00003125 /* Set the most significant bit to 0 */
3126 MBEDTLS_MPI_CHK(
3127 mbedtls_mpi_set_bit( &key->d,
3128 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0 )
3129 );
Janos Follath171a7ef2019-02-15 16:17:45 +00003130
Janos Follath28eb06d2019-02-26 10:53:34 +00003131 /* Set the second most significant bit to 1 */
3132 MBEDTLS_MPI_CHK(
3133 mbedtls_mpi_set_bit( &key->d,
3134 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1 )
3135 );
3136 }
Archana1d2e2bb2021-06-07 06:13:16 +05303137 else if( grp_id == MBEDTLS_ECP_DP_CURVE448 )
3138 {
3139 if( buflen != ECP_CURVE448_KEY_SIZE )
Archana277572f2021-07-12 09:00:57 +05303140 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Archana1d2e2bb2021-06-07 06:13:16 +05303141
3142 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary_le( &key->d, buf, buflen ) );
3143
3144 /* Set the two least significant bits to 0 */
3145 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 0, 0 ) );
3146 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 1, 0 ) );
3147
3148 /* Set the most significant bit to 1 */
3149 MBEDTLS_MPI_CHK(
3150 mbedtls_mpi_set_bit( &key->d,
3151 ECP_CURVE448_KEY_SIZE * 8 - 1, 1 )
3152 );
3153 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003154 }
3155
3156#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003157#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathdf9295b2019-02-26 12:36:52 +00003158 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Janos Follath171a7ef2019-02-15 16:17:45 +00003159 {
3160 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &key->d, buf, buflen ) );
3161
3162 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( &key->grp, &key->d ) );
3163 }
3164
3165#endif
3166cleanup:
3167
3168 if( ret != 0 )
3169 mbedtls_mpi_free( &key->d );
3170
3171 return( ret );
3172}
3173
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003174/*
Steven Cooremande8593f2020-06-09 19:55:26 +02003175 * Write a private key.
3176 */
Steven Cooreman0024df62020-07-13 10:59:40 +02003177int mbedtls_ecp_write_key( mbedtls_ecp_keypair *key,
Steven Cooremanc9b7f782020-06-11 17:00:36 +02003178 unsigned char *buf, size_t buflen )
Steven Cooremande8593f2020-06-09 19:55:26 +02003179{
Steven Cooreman0024df62020-07-13 10:59:40 +02003180 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooremande8593f2020-06-09 19:55:26 +02003181
Steven Cooreman0024df62020-07-13 10:59:40 +02003182 ECP_VALIDATE_RET( key != NULL );
3183 ECP_VALIDATE_RET( buf != NULL );
Steven Cooremande8593f2020-06-09 19:55:26 +02003184
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003185#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Steven Cooremande8593f2020-06-09 19:55:26 +02003186 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
3187 {
Steven Cooreman0024df62020-07-13 10:59:40 +02003188 if( key->grp.id == MBEDTLS_ECP_DP_CURVE25519 )
Steven Cooremande8593f2020-06-09 19:55:26 +02003189 {
3190 if( buflen < ECP_CURVE25519_KEY_SIZE )
Archana277572f2021-07-12 09:00:57 +05303191 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Steven Cooremande8593f2020-06-09 19:55:26 +02003192
Steven Cooremande8593f2020-06-09 19:55:26 +02003193 }
Archana277572f2021-07-12 09:00:57 +05303194 else if( key->grp.id == MBEDTLS_ECP_DP_CURVE448 )
Archana1d2e2bb2021-06-07 06:13:16 +05303195 {
3196 if( buflen < ECP_CURVE448_KEY_SIZE )
Archana277572f2021-07-12 09:00:57 +05303197 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Archana1d2e2bb2021-06-07 06:13:16 +05303198 }
3199 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary_le( &key->d, buf, buflen ) );
Steven Cooremande8593f2020-06-09 19:55:26 +02003200 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003201#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003202#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Steven Cooremande8593f2020-06-09 19:55:26 +02003203 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
3204 {
3205 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &key->d, buf, buflen ) );
Steven Cooremande8593f2020-06-09 19:55:26 +02003206 }
3207
3208#endif
3209cleanup:
3210
3211 return( ret );
3212}
3213
3214
3215/*
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003216 * Check a public-private key pair
3217 */
Manuel Pégourié-Gonnardf8c24bf2021-06-15 11:29:26 +02003218int mbedtls_ecp_check_pub_priv(
3219 const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
3220 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003221{
Janos Follath24eed8d2019-11-22 13:21:35 +00003222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 mbedtls_ecp_point Q;
3224 mbedtls_ecp_group grp;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003225 ECP_VALIDATE_RET( pub != NULL );
3226 ECP_VALIDATE_RET( prv != NULL );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003229 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
3231 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
3232 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003235 }
3236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 mbedtls_ecp_point_init( &Q );
3238 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 /* mbedtls_ecp_mul() needs a non-const group... */
3241 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003242
3243 /* Also checks d is valid */
Manuel Pégourié-Gonnardf8c24bf2021-06-15 11:29:26 +02003244 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
3247 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
3248 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003251 goto cleanup;
3252 }
3253
3254cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 mbedtls_ecp_point_free( &Q );
3256 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003257
3258 return( ret );
3259}
3260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003262
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003263/*
3264 * PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!!
3265 *
3266 * This is the linear congruential generator from numerical recipes,
3267 * except we only use the low byte as the output. See
3268 * https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
3269 */
3270static int self_test_rng( void *ctx, unsigned char *out, size_t len )
3271{
3272 static uint32_t state = 42;
3273
3274 (void) ctx;
3275
3276 for( size_t i = 0; i < len; i++ )
3277 {
3278 state = state * 1664525u + 1013904223u;
3279 out[i] = (unsigned char) state;
3280 }
3281
3282 return( 0 );
3283}
3284
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003285/* Adjust the exponent to be a valid private point for the specified curve.
3286 * This is sometimes necessary because we use a single set of exponents
3287 * for all curves but the validity of values depends on the curve. */
Gilles Peskinea088c812018-09-17 18:31:15 +02003288static int self_test_adjust_exponent( const mbedtls_ecp_group *grp,
3289 mbedtls_mpi *m )
3290{
3291 int ret = 0;
3292 switch( grp->id )
3293 {
3294 /* If Curve25519 is available, then that's what we use for the
3295 * Montgomery test, so we don't need the adjustment code. */
3296#if ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
3297#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3298 case MBEDTLS_ECP_DP_CURVE448:
3299 /* Move highest bit from 254 to N-1. Setting bit N-1 is
3300 * necessary to enforce the highest-bit-set constraint. */
3301 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( m, 254, 0 ) );
3302 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( m, grp->nbits, 1 ) );
3303 /* Copy second-highest bit from 253 to N-2. This is not
3304 * necessary but improves the test variety a bit. */
3305 MBEDTLS_MPI_CHK(
3306 mbedtls_mpi_set_bit( m, grp->nbits - 1,
3307 mbedtls_mpi_get_bit( m, 253 ) ) );
3308 break;
3309#endif
3310#endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */
3311 default:
3312 /* Non-Montgomery curves and Curve25519 need no adjustment. */
3313 (void) grp;
3314 (void) m;
3315 goto cleanup;
3316 }
3317cleanup:
3318 return( ret );
3319}
3320
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003321/* Calculate R = m.P for each m in exponents. Check that the number of
3322 * basic operations doesn't depend on the value of m. */
Gilles Peskinec95696f2018-09-17 15:59:01 +02003323static int self_test_point( int verbose,
3324 mbedtls_ecp_group *grp,
3325 mbedtls_ecp_point *R,
3326 mbedtls_mpi *m,
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003327 const mbedtls_ecp_point *P,
Gilles Peskinec95696f2018-09-17 15:59:01 +02003328 const char *const *exponents,
3329 size_t n_exponents )
3330{
3331 int ret = 0;
Gilles Peskine24666792018-09-17 18:29:49 +02003332 size_t i = 0;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003333 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
3334 add_count = 0;
3335 dbl_count = 0;
3336 mul_count = 0;
Gilles Peskinea088c812018-09-17 18:31:15 +02003337
Gilles Peskinec95696f2018-09-17 15:59:01 +02003338 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( m, 16, exponents[0] ) );
Gilles Peskinea088c812018-09-17 18:31:15 +02003339 MBEDTLS_MPI_CHK( self_test_adjust_exponent( grp, m ) );
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003340 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, self_test_rng, NULL ) );
Gilles Peskinec95696f2018-09-17 15:59:01 +02003341
3342 for( i = 1; i < n_exponents; i++ )
3343 {
3344 add_c_prev = add_count;
3345 dbl_c_prev = dbl_count;
3346 mul_c_prev = mul_count;
3347 add_count = 0;
3348 dbl_count = 0;
3349 mul_count = 0;
3350
3351 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( m, 16, exponents[i] ) );
Gilles Peskinea088c812018-09-17 18:31:15 +02003352 MBEDTLS_MPI_CHK( self_test_adjust_exponent( grp, m ) );
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003353 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, self_test_rng, NULL ) );
Gilles Peskinec95696f2018-09-17 15:59:01 +02003354
3355 if( add_count != add_c_prev ||
3356 dbl_count != dbl_c_prev ||
3357 mul_count != mul_c_prev )
3358 {
3359 ret = 1;
3360 break;
3361 }
3362 }
3363
3364cleanup:
3365 if( verbose != 0 )
3366 {
3367 if( ret != 0 )
3368 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
3369 else
3370 mbedtls_printf( "passed\n" );
3371 }
3372 return( ret );
3373}
3374
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003375/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003376 * Checkup routine
3377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003379{
Janos Follath24eed8d2019-11-22 13:21:35 +00003380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 mbedtls_ecp_group grp;
3382 mbedtls_ecp_point R, P;
3383 mbedtls_mpi m;
Gilles Peskine24666792018-09-17 18:29:49 +02003384
3385#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskined9767a52018-09-14 19:29:47 +02003386 /* Exponents especially adapted for secp192k1, which has the lowest
3387 * order n of all supported curves (secp192r1 is in a slightly larger
3388 * field but the order of its base point is slightly smaller). */
Gilles Peskine24666792018-09-17 18:29:49 +02003389 const char *sw_exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003390 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003391 "000000000000000000000000000000000000000000000001", /* one */
Gilles Peskined9767a52018-09-14 19:29:47 +02003392 "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003393 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003394 "400000000000000000000000000000000000000000000000", /* one and zeros */
3395 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3396 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003397 };
Gilles Peskine24666792018-09-17 18:29:49 +02003398#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3399#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3400 const char *m_exponents[] =
3401 {
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003402 /* Valid private values for Curve25519. In a build with Curve448
3403 * but not Curve25519, they will be adjusted in
3404 * self_test_adjust_exponent(). */
Gilles Peskine24666792018-09-17 18:29:49 +02003405 "4000000000000000000000000000000000000000000000000000000000000000",
3406 "5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
3407 "5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
3408 "41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
3409 "5555555555555555555555555555555555555555555555555555555555555550",
3410 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
3411 };
3412#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 mbedtls_ecp_group_init( &grp );
3415 mbedtls_ecp_point_init( &R );
3416 mbedtls_ecp_point_init( &P );
3417 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003418
Gilles Peskine24666792018-09-17 18:29:49 +02003419#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003420 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003422 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003423#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003424 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003425#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003426
3427 if( verbose != 0 )
Gilles Peskine24666792018-09-17 18:29:49 +02003428 mbedtls_printf( " ECP SW test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003429 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003431 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, self_test_rng, NULL ) );
Gilles Peskinec95696f2018-09-17 15:59:01 +02003432 ret = self_test_point( verbose,
3433 &grp, &R, &m, &grp.G,
Gilles Peskine24666792018-09-17 18:29:49 +02003434 sw_exponents,
3435 sizeof( sw_exponents ) / sizeof( sw_exponents[0] ));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003436 if( ret != 0 )
3437 goto cleanup;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003438
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003439 if( verbose != 0 )
Gilles Peskine24666792018-09-17 18:29:49 +02003440 mbedtls_printf( " ECP SW test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003441 /* We computed P = 2G last time, use it */
Gilles Peskinec95696f2018-09-17 15:59:01 +02003442 ret = self_test_point( verbose,
3443 &grp, &R, &m, &P,
Gilles Peskine24666792018-09-17 18:29:49 +02003444 sw_exponents,
3445 sizeof( sw_exponents ) / sizeof( sw_exponents[0] ));
3446 if( ret != 0 )
3447 goto cleanup;
3448
3449 mbedtls_ecp_group_free( &grp );
3450 mbedtls_ecp_point_free( &R );
3451#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3452
3453#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3454 if( verbose != 0 )
3455 mbedtls_printf( " ECP Montgomery test (constant op_count): " );
3456#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
3457 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_CURVE25519 ) );
3458#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3459 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_CURVE448 ) );
3460#else
3461#error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
3462#endif
3463 ret = self_test_point( verbose,
3464 &grp, &R, &m, &grp.G,
3465 m_exponents,
3466 sizeof( m_exponents ) / sizeof( m_exponents[0] ));
3467 if( ret != 0 )
3468 goto cleanup;
3469#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003470
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003471cleanup:
3472
3473 if( ret < 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003474 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 mbedtls_ecp_group_free( &grp );
3477 mbedtls_ecp_point_free( &R );
3478 mbedtls_ecp_point_free( &P );
3479 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003480
3481 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003483
3484 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003485}
3486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003488
Janos Follathb0697532016-08-18 12:38:46 +01003489#endif /* !MBEDTLS_ECP_ALT */
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491#endif /* MBEDTLS_ECP_C */