blob: 82c931f5eaaa53fe4b146319ae37165057dec9af [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010020 */
21
22/*
23 * References:
24 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010025 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010026 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010027 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010028 * RFC 4492 for the related TLS structures and constants
Nicholas Wilson08f3ef12015-11-10 13:10:01 +000029 * RFC 7748 for the Curve448 and Curve25519 curve definitions
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020030 *
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020031 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010032 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020033 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020034 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
35 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
36 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010037 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020038 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010039 * render ECC resistant against Side Channel Attacks. IACR Cryptology
40 * ePrint Archive, 2004, vol. 2004, p. 342.
41 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010042 */
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020046#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020048#endif
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010049
Janos Follath683c5822018-12-07 12:09:25 +000050/**
51 * \brief Function level alternative implementation.
52 *
53 * The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
54 * replace certain functions in this module. The alternative implementations are
55 * typically hardware accelerators and need to activate the hardware before the
56 * computation starts and deactivate it after it finishes. The
57 * mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
58 * this purpose.
59 *
60 * To preserve the correct functionality the following conditions must hold:
61 *
62 * - The alternative implementation must be activated by
63 * mbedtls_internal_ecp_init() before any of the replaceable functions is
64 * called.
65 * - mbedtls_internal_ecp_free() must \b only be called when the alternative
66 * implementation is activated.
67 * - mbedtls_internal_ecp_init() must \b not be called when the alternative
68 * implementation is activated.
69 * - Public functions must not return while the alternative implementation is
70 * activated.
71 * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
72 * before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
73 * \endcode ensures that the alternative implementation supports the current
74 * group.
75 */
76#if defined(MBEDTLS_ECP_INTERNAL_ALT)
77#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010080
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/ecp.h"
Janos Follath430d3372016-11-03 14:25:37 +000082#include "mbedtls/threading.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050083#include "mbedtls/platform_util.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020084
Rich Evans00ab4702015-02-06 13:43:58 +000085#include <string.h>
86
Janos Follathb0697532016-08-18 12:38:46 +010087#if !defined(MBEDTLS_ECP_ALT)
88
Hanno Becker4f8e8e52018-12-14 15:08:03 +000089/* Parameter validation macros based on platform_util.h */
90#define ECP_VALIDATE_RET( cond ) \
91 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
92#define ECP_VALIDATE( cond ) \
93 MBEDTLS_INTERNAL_VALIDATE( cond )
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000096#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020097#else
Rich Evans00ab4702015-02-06 13:43:58 +000098#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000099#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200101#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +0200103#endif
104
Janos Follath47d28f02016-11-01 13:22:05 +0000105#include "mbedtls/ecp_internal.h"
Janos Follathb0697532016-08-18 12:38:46 +0100106
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200107#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardc7295f52020-06-04 12:32:14 +0200108#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200109#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnardc7295f52020-06-04 12:32:14 +0200110#elif defined(MBEDTLS_CTR_DRBG_C)
111#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200112#else
113#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
114#endif
115#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
116
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +0100117#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
118 !defined(inline) && !defined(__cplusplus)
Paul Bakker6a6087e2013-10-28 18:53:08 +0100119#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +0200120#endif
Paul Bakker6a6087e2013-10-28 18:53:08 +0100121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100123/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100124 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200125 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100126 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100127static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100128#endif
129
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200130#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
131/*
132 * Currently ecp_mul() takes a RNG function as an argument, used for
Manuel Pégourié-Gonnard0defc572020-06-10 09:18:25 +0200133 * side-channel protection, but it can be NULL. The initial reasoning was
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200134 * that people will pass non-NULL RNG when they care about side-channels, but
135 * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with
136 * no opportunity for the user to do anything about it.
137 *
138 * The obvious strategies for addressing that include:
139 * - change those APIs so that they take RNG arguments;
140 * - require a global RNG to be available to all crypto modules.
141 *
142 * Unfortunately those would break compatibility. So what we do instead is
143 * have our own internal DRBG instance, seeded from the secret scalar.
144 *
145 * The following is a light-weight abstraction layer for doing that with
Manuel Pégourié-Gonnardc7295f52020-06-04 12:32:14 +0200146 * HMAC_DRBG (first choice) or CTR_DRBG.
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200147 */
148
Manuel Pégourié-Gonnardc7295f52020-06-04 12:32:14 +0200149#if defined(MBEDTLS_HMAC_DRBG_C)
150
151/* DRBG context type */
152typedef mbedtls_hmac_drbg_context ecp_drbg_context;
153
154/* DRBG context init */
155static inline void ecp_drbg_init( ecp_drbg_context *ctx )
156{
157 mbedtls_hmac_drbg_init( ctx );
158}
159
160/* DRBG context free */
161static inline void ecp_drbg_free( ecp_drbg_context *ctx )
162{
163 mbedtls_hmac_drbg_free( ctx );
164}
165
166/* DRBG function */
167static inline int ecp_drbg_random( void *p_rng,
168 unsigned char *output, size_t output_len )
169{
170 return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
171}
172
173/* DRBG context seeding */
174static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret )
175{
176 const unsigned char *secret_p = (const unsigned char *) secret->p;
177 const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint );
178
179 /* The list starts with strong hashes */
180 const mbedtls_md_type_t md_type = mbedtls_md_list()[0];
181 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type );
182
183 return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_size ) );
184}
185
186#elif defined(MBEDTLS_CTR_DRBG_C)
187
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200188/* DRBG context type */
189typedef mbedtls_ctr_drbg_context ecp_drbg_context;
190
191/* DRBG context init */
192static inline void ecp_drbg_init( ecp_drbg_context *ctx )
193{
194 mbedtls_ctr_drbg_init( ctx );
195}
196
197/* DRBG context free */
198static inline void ecp_drbg_free( ecp_drbg_context *ctx )
199{
200 mbedtls_ctr_drbg_free( ctx );
201}
202
203/* DRBG function */
204static inline int ecp_drbg_random( void *p_rng,
205 unsigned char *output, size_t output_len )
206{
207 return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
208}
209
210/*
211 * Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does,
212 * we need to pass an entropy function when seeding. So we use a dummy
213 * function for that, and pass the actual entropy as customisation string.
214 * (During seeding of CTR_DRBG the entropy input and customisation string are
215 * concatenated before being used to update the secret state.)
216 */
217static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
218{
219 (void) ctx;
220 memset( out, 0, len );
221 return( 0 );
222}
223
224/* DRBG context seeding */
225static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret )
226{
227 const unsigned char *secret_p = (const unsigned char *) secret->p;
228 const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint );
229
230 return( mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL,
231 secret_p, secret_size ) );
232}
233
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200234#else
235#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
236#endif /* DRBG modules */
237#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
238
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200239#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100240/*
241 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200242 *
243 * Default value 0 means that ECC operations will not yield.
244 * Note that regardless of the value of ecp_max_ops, always at
245 * least one step is performed before yielding.
246 *
247 * Setting ecp_max_ops=1 can be suitable for testing purposes
248 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100249 */
250static unsigned ecp_max_ops = 0;
251
252/*
253 * Set ecp_max_ops
254 */
255void mbedtls_ecp_set_max_ops( unsigned max_ops )
256{
257 ecp_max_ops = max_ops;
258}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100259
260/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200261 * Check if restart is enabled
262 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200263int mbedtls_ecp_restart_is_enabled( void )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200264{
265 return( ecp_max_ops != 0 );
266}
267
268/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200269 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100270 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200271struct mbedtls_ecp_restart_mul
272{
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100273 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100274 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100275 mbedtls_ecp_point *T; /* table for precomputed points */
276 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200277 enum { /* what were we doing last time we returned? */
278 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
279 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100280 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
281 ecp_rsm_pre_add, /* precompute remaining points by adding */
282 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200283 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100284 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100285 } state;
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +0200286#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
287 ecp_drbg_context drbg_ctx;
288 unsigned char drbg_seeded;
289#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100290};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100291
292/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200293 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100294 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200295static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100296{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200297 mbedtls_ecp_point_init( &ctx->R );
298 ctx->i = 0;
299 ctx->T = NULL;
300 ctx->T_size = 0;
301 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +0200302#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
303 ecp_drbg_init( &ctx->drbg_ctx );
304 ctx->drbg_seeded = 0;
305#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100306}
307
308/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200309 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100310 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200311static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100312{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100313 unsigned char i;
314
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100315 if( ctx == NULL )
316 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100317
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100318 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100319
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200320 if( ctx->T != NULL )
321 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100322 for( i = 0; i < ctx->T_size; i++ )
323 mbedtls_ecp_point_free( ctx->T + i );
324 mbedtls_free( ctx->T );
325 }
326
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +0200327#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
328 ecp_drbg_free( &ctx->drbg_ctx );
329#endif
330
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200331 ecp_restart_rsm_init( ctx );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100332}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100333
334/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200335 * Restart context for ecp_muladd()
336 */
337struct mbedtls_ecp_restart_muladd
338{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200339 mbedtls_ecp_point mP; /* mP value */
340 mbedtls_ecp_point R; /* R intermediate result */
341 enum { /* what should we do next? */
342 ecp_rsma_mul1 = 0, /* first multiplication */
343 ecp_rsma_mul2, /* second multiplication */
344 ecp_rsma_add, /* addition */
345 ecp_rsma_norm, /* normalization */
346 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200347};
348
349/*
350 * Init restart_muladd sub-context
351 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200352static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200353{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200354 mbedtls_ecp_point_init( &ctx->mP );
355 mbedtls_ecp_point_init( &ctx->R );
356 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200357}
358
359/*
360 * Free the components of a restart_muladd sub-context
361 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200362static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200363{
364 if( ctx == NULL )
365 return;
366
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200367 mbedtls_ecp_point_free( &ctx->mP );
368 mbedtls_ecp_point_free( &ctx->R );
369
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200370 ecp_restart_ma_init( ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200371}
372
373/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200374 * Initialize a restart context
375 */
376void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
377{
Hanno Becker80f71682018-12-18 23:44:43 +0000378 ECP_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200379 ctx->ops_done = 0;
380 ctx->depth = 0;
381 ctx->rsm = NULL;
382 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200383}
384
385/*
386 * Free the components of a restart context
387 */
388void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
389{
390 if( ctx == NULL )
391 return;
392
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200393 ecp_restart_rsm_free( ctx->rsm );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200394 mbedtls_free( ctx->rsm );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200395
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200396 ecp_restart_ma_free( ctx->ma );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200397 mbedtls_free( ctx->ma );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200398
399 mbedtls_ecp_restart_init( ctx );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200400}
401
402/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100403 * Check if we can do the next step
404 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200405int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
406 mbedtls_ecp_restart_ctx *rs_ctx,
407 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100408{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000409 ECP_VALIDATE_RET( grp != NULL );
410
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200411 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100412 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100413 /* scale depending on curve size: the chosen reference is 256-bit,
414 * and multiplication is quadratic. Round to the closest integer. */
415 if( grp->pbits >= 512 )
416 ops *= 4;
417 else if( grp->pbits >= 384 )
418 ops *= 2;
419
Hanno Beckerb10c6602018-10-26 13:50:13 +0100420 /* Avoid infinite loops: always allow first step.
421 * Because of that, however, it's not generally true
422 * that ops_done <= ecp_max_ops, so the check
423 * ops_done > ecp_max_ops below is mandatory. */
424 if( ( rs_ctx->ops_done != 0 ) &&
425 ( rs_ctx->ops_done > ecp_max_ops ||
426 ops > ecp_max_ops - rs_ctx->ops_done ) )
427 {
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100428 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
Hanno Beckerb10c6602018-10-26 13:50:13 +0100429 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100430
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100431 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200432 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100433 }
434
435 return( 0 );
436}
437
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200438/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200439#define ECP_RS_ENTER( SUB ) do { \
440 /* reset ops count for this call if top-level */ \
441 if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
442 rs_ctx->ops_done = 0; \
443 \
444 /* set up our own sub-context if needed */ \
445 if( mbedtls_ecp_restart_is_enabled() && \
446 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
447 { \
448 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
449 if( rs_ctx->SUB == NULL ) \
450 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
451 \
452 ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
453 } \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200454} while( 0 )
455
456/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200457#define ECP_RS_LEAVE( SUB ) do { \
458 /* clear our sub-context when not in progress (done or error) */ \
459 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
460 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
461 { \
462 ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
463 mbedtls_free( rs_ctx->SUB ); \
464 rs_ctx->SUB = NULL; \
465 } \
466 \
467 if( rs_ctx != NULL ) \
468 rs_ctx->depth--; \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200469} while( 0 )
470
471#else /* MBEDTLS_ECP_RESTARTABLE */
472
473#define ECP_RS_ENTER( sub ) (void) rs_ctx;
474#define ECP_RS_LEAVE( sub ) (void) rs_ctx;
475
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200476#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
479 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
480 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
481 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
482 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
483 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
484 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
485 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
486 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
487 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
488 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200489#define ECP_SHORTWEIERSTRASS
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100490#endif
491
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000492#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
493 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200494#define ECP_MONTGOMERY
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100495#endif
496
497/*
498 * Curve types: internal for now, might be exposed later
499 */
500typedef enum
501{
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200502 ECP_TYPE_NONE = 0,
503 ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
504 ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100505} ecp_curve_type;
506
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100507/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200508 * List of supported curves:
509 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200510 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200511 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200512 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100513 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100514 * Curves are listed in order: largest curves first, and for a given size,
515 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200516 *
517 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200520{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
522 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
525 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
528 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
531 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
534 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
537 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100538#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
540 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
543 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
546 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
549 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100550#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
552 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200555};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100556
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200557#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
558 sizeof( ecp_supported_curves[0] )
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200561
562/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200563 * List of supported curves and associated info
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200566{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200567 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200568}
569
570/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100571 * List of supported curves, group ID only
572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100574{
575 static int init_done = 0;
576
577 if( ! init_done )
578 {
579 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 for( curve_info = mbedtls_ecp_curve_list();
583 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100584 curve_info++ )
585 {
586 ecp_supported_grp_id[i++] = curve_info->grp_id;
587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100589
590 init_done = 1;
591 }
592
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200593 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100594}
595
596/*
597 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599const 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 +0200600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 for( curve_info = mbedtls_ecp_curve_list();
604 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200605 curve_info++ )
606 {
607 if( curve_info->grp_id == grp_id )
608 return( curve_info );
609 }
610
611 return( NULL );
612}
613
614/*
615 * Get the curve info from the TLS identifier
616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 for( curve_info = mbedtls_ecp_curve_list();
622 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200623 curve_info++ )
624 {
625 if( curve_info->tls_id == tls_id )
626 return( curve_info );
627 }
628
629 return( NULL );
630}
631
632/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100633 * Get the curve info from the name
634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100638
Hanno Beckerb7a04a72018-12-18 23:50:21 +0000639 if( name == NULL )
640 return( NULL );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 for( curve_info = mbedtls_ecp_curve_list();
643 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100644 curve_info++ )
645 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200646 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100647 return( curve_info );
648 }
649
650 return( NULL );
651}
652
653/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100654 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100657{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100658 if( grp->G.X.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200659 return( ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100660
661 if( grp->G.Y.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200662 return( ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100663 else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200664 return( ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100665}
666
667/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100668 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100671{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000672 ECP_VALIDATE( pt != NULL );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_mpi_init( &pt->X );
675 mbedtls_mpi_init( &pt->Y );
676 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100677}
678
679/*
680 * Initialize (the components of) a group
681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100683{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000684 ECP_VALIDATE( grp != NULL );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100685
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200686 grp->id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200687 mbedtls_mpi_init( &grp->P );
688 mbedtls_mpi_init( &grp->A );
689 mbedtls_mpi_init( &grp->B );
690 mbedtls_ecp_point_init( &grp->G );
691 mbedtls_mpi_init( &grp->N );
692 grp->pbits = 0;
693 grp->nbits = 0;
694 grp->h = 0;
695 grp->modp = NULL;
696 grp->t_pre = NULL;
697 grp->t_post = NULL;
698 grp->t_data = NULL;
699 grp->T = NULL;
700 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100701}
702
703/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200704 * Initialize (the components of) a key pair
705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200707{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000708 ECP_VALIDATE( key != NULL );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_ecp_group_init( &key->grp );
711 mbedtls_mpi_init( &key->d );
712 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200713}
714
715/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100716 * Unallocate (the components of) a point
717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100719{
720 if( pt == NULL )
721 return;
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi_free( &( pt->X ) );
724 mbedtls_mpi_free( &( pt->Y ) );
725 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100726}
727
728/*
729 * Unallocate (the components of) a group
730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100732{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200733 size_t i;
734
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100735 if( grp == NULL )
736 return;
737
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100738 if( grp->h != 1 )
739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_mpi_free( &grp->P );
741 mbedtls_mpi_free( &grp->A );
742 mbedtls_mpi_free( &grp->B );
743 mbedtls_ecp_point_free( &grp->G );
744 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100745 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200746
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200747 if( grp->T != NULL )
748 {
749 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_ecp_point_free( &grp->T[i] );
751 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200752 }
753
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500754 mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100755}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100756
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100757/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200758 * Unallocate (the components of) a key pair
759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200761{
Paul Bakker66d5d072014-06-17 16:39:18 +0200762 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200763 return;
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_ecp_group_free( &key->grp );
766 mbedtls_mpi_free( &key->d );
767 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200768}
769
770/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200771 * Copy the contents of a point
772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200774{
775 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000776 ECP_VALIDATE_RET( P != NULL );
777 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
780 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
781 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200782
783cleanup:
784 return( ret );
785}
786
787/*
788 * Copy the contents of a group object
789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200791{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000792 ECP_VALIDATE_RET( dst != NULL );
793 ECP_VALIDATE_RET( src != NULL );
794
795 return( mbedtls_ecp_group_load( dst, src->id ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200796}
797
798/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100799 * Set point to zero
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100802{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100803 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000804 ECP_VALIDATE_RET( pt != NULL );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
807 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
808 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100809
810cleanup:
811 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100812}
813
814/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100815 * Tell if a point is zero
816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100818{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000819 ECP_VALIDATE_RET( pt != NULL );
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100822}
823
824/*
Brian J Murrayf343de12018-10-22 16:40:49 -0700825 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200826 */
827int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
828 const mbedtls_ecp_point *Q )
829{
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000830 ECP_VALIDATE_RET( P != NULL );
831 ECP_VALIDATE_RET( Q != NULL );
832
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200833 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
834 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
835 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
836 {
837 return( 0 );
838 }
839
840 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
841}
842
843/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100844 * Import a non-zero point from ASCII strings
845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100847 const char *x, const char *y )
848{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100849 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000850 ECP_VALIDATE_RET( P != NULL );
851 ECP_VALIDATE_RET( x != NULL );
852 ECP_VALIDATE_RET( y != NULL );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
855 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
856 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100857
858cleanup:
859 return( ret );
860}
861
862/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100863 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100864 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000865int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp,
866 const mbedtls_ecp_point *P,
867 int format, size_t *olen,
868 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100869{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200870 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100871 size_t plen;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000872 ECP_VALIDATE_RET( grp != NULL );
873 ECP_VALIDATE_RET( P != NULL );
874 ECP_VALIDATE_RET( olen != NULL );
875 ECP_VALIDATE_RET( buf != NULL );
876 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
877 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100878
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100879 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100880 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100883 {
884 if( buflen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100886
887 buf[0] = 0x00;
888 *olen = 1;
889
890 return( 0 );
891 }
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100896 {
897 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100898
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100899 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100901
902 buf[0] = 0x04;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
904 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100907 {
908 *olen = plen + 1;
909
910 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
914 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100915 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100916
917cleanup:
918 return( ret );
919}
920
921/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100922 * Import a point from unsigned binary data (SEC1 2.3.4)
923 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000924int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
925 mbedtls_ecp_point *pt,
926 const unsigned char *buf, size_t ilen )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100927{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100928 int ret;
929 size_t plen;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000930 ECP_VALIDATE_RET( grp != NULL );
931 ECP_VALIDATE_RET( pt != NULL );
932 ECP_VALIDATE_RET( buf != NULL );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100933
Paul Bakker82788fb2014-10-20 13:59:19 +0200934 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200936
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100937 if( buf[0] == 0x00 )
938 {
939 if( ilen == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 return( mbedtls_ecp_set_zero( pt ) );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100941 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100943 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100946
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100947 if( buf[0] != 0x04 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100949
950 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
955 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100956
957cleanup:
958 return( ret );
959}
960
961/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100962 * Import a point from a TLS ECPoint record (RFC 4492)
963 * struct {
964 * opaque point <1..2^8-1>;
965 * } ECPoint;
966 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000967int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
968 mbedtls_ecp_point *pt,
969 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100970{
971 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100972 const unsigned char *buf_start;
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000973 ECP_VALIDATE_RET( grp != NULL );
974 ECP_VALIDATE_RET( pt != NULL );
975 ECP_VALIDATE_RET( buf != NULL );
976 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100977
978 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200979 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100980 */
981 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100983
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100984 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100985 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100987
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100988 /*
989 * Save buffer start for read_binary and update buf
990 */
991 buf_start = *buf;
992 *buf += data_len;
993
Hanno Becker4f8e8e52018-12-14 15:08:03 +0000994 return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100995}
996
997/*
998 * Export a point as a TLS ECPoint record (RFC 4492)
999 * struct {
1000 * opaque point <1..2^8-1>;
1001 * } ECPoint;
1002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001004 int format, size_t *olen,
1005 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001006{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001007 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001008 ECP_VALIDATE_RET( grp != NULL );
1009 ECP_VALIDATE_RET( pt != NULL );
1010 ECP_VALIDATE_RET( olen != NULL );
1011 ECP_VALIDATE_RET( buf != NULL );
1012 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1013 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001014
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001015 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001016 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001017 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001018 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001022 olen, buf + 1, blen - 1) ) != 0 )
1023 return( ret );
1024
1025 /*
1026 * write length to the first byte and update total length
1027 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001028 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001029 ++*olen;
1030
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001031 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001032}
1033
1034/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001035 * Set a group from an ECParameters record (RFC 4492)
1036 */
Janos Follath89ac8c92018-10-30 11:24:05 +00001037int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
1038 const unsigned char **buf, size_t len )
1039{
1040 int ret;
1041 mbedtls_ecp_group_id grp_id;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001042 ECP_VALIDATE_RET( grp != NULL );
1043 ECP_VALIDATE_RET( buf != NULL );
1044 ECP_VALIDATE_RET( *buf != NULL );
Janos Follath89ac8c92018-10-30 11:24:05 +00001045
1046 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 )
1047 return( ret );
1048
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001049 return( mbedtls_ecp_group_load( grp, grp_id ) );
Janos Follath89ac8c92018-10-30 11:24:05 +00001050}
1051
1052/*
1053 * Read a group id from an ECParameters record (RFC 4492) and convert it to
1054 * mbedtls_ecp_group_id.
1055 */
1056int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
1057 const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001058{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001059 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 const mbedtls_ecp_curve_info *curve_info;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001061 ECP_VALIDATE_RET( grp != NULL );
1062 ECP_VALIDATE_RET( buf != NULL );
1063 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001064
1065 /*
1066 * We expect at least three bytes (see below)
1067 */
1068 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001070
1071 /*
1072 * First byte is curve_type; only named_curve is handled
1073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
1075 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001076
1077 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001078 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001079 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001080 tls_id = *(*buf)++;
1081 tls_id <<= 8;
1082 tls_id |= *(*buf)++;
1083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
1085 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001086
Janos Follath89ac8c92018-10-30 11:24:05 +00001087 *grp = curve_info->grp_id;
1088
1089 return( 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001090}
1091
1092/*
1093 * Write the ECParameters record corresponding to a group (RFC 4492)
1094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001096 unsigned char *buf, size_t blen )
1097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 const mbedtls_ecp_curve_info *curve_info;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00001099 ECP_VALIDATE_RET( grp != NULL );
1100 ECP_VALIDATE_RET( buf != NULL );
1101 ECP_VALIDATE_RET( olen != NULL );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
1104 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001105
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001106 /*
1107 * We are going to write 3 bytes (see below)
1108 */
1109 *olen = 3;
1110 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001112
1113 /*
1114 * First byte is curve_type, always named_curve
1115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001117
1118 /*
1119 * Next two bytes are the namedcurve value
1120 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001121 buf[0] = curve_info->tls_id >> 8;
1122 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001123
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001124 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001125}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001126
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001127/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1129 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001130 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 * 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 +02001132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001134{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001135 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001136
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001137 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001139
1140 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001142 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001145 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001148
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001149 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
1151 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001154 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001156
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001157cleanup:
1158 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001159}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001160
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001161/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001162 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001163 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001164 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 * 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 +01001166 * bring the result back to this range.
1167 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001168 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001169 */
1170
1171/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 * 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 +01001173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001175#define INC_MUL_COUNT mul_count++;
1176#else
1177#define INC_MUL_COUNT
1178#endif
1179
Hanno Beckerd6028a12018-10-15 12:01:35 +01001180#define MOD_MUL( N ) \
1181 do \
1182 { \
1183 MBEDTLS_MPI_CHK( ecp_modp( &(N), grp ) ); \
1184 INC_MUL_COUNT \
1185 } while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001186
1187/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001189 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001190 */
Hanno Beckerd6028a12018-10-15 12:01:35 +01001191#define MOD_SUB( N ) \
1192 while( (N).s < 0 && mbedtls_mpi_cmp_int( &(N), 0 ) != 0 ) \
1193 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001194
1195/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 * 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 +02001197 * We known P, N and the result are positive, so sub_abs is correct, and
1198 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001199 */
Hanno Beckerd6028a12018-10-15 12:01:35 +01001200#define MOD_ADD( N ) \
1201 while( mbedtls_mpi_cmp_mpi( &(N), &grp->P ) >= 0 ) \
1202 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001203
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001204#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001205/*
1206 * For curves in short Weierstrass form, we do all the internal operations in
1207 * Jacobian coordinates.
1208 *
1209 * For multiplication, we'll use a comb method with coutermeasueres against
1210 * SPA, hence timing attacks.
1211 */
1212
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001213/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001214 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001215 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001218{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001219 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001223 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001224
Janos Follathb0697532016-08-18 12:38:46 +01001225#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001226 if( mbedtls_internal_ecp_grp_capable( grp ) )
1227 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +01001228#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001231
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001232 /*
1233 * X = X / Z^2 mod p
1234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
1236 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1237 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001238
1239 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001240 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
1243 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001244
1245 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001246 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001249
1250cleanup:
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001253
1254 return( ret );
1255}
1256
1257/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001258 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001259 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001260 * (See for example Cohen's "A Course in Computational Algebraic Number
1261 * Theory", Algorithm 10.3.4.)
1262 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001263 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001264 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001265 *
1266 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001269 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001270{
1271 int ret;
1272 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_mpi *c, u, Zi, ZZi;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001274
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001275 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001276 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001277
Janos Follathb0697532016-08-18 12:38:46 +01001278#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001279 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001280 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +01001281#endif
1282
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001283 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001284 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001285
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001286 for( i = 0; i < T_size; i++ )
1287 mbedtls_mpi_init( &c[i] );
1288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001290
1291 /*
1292 * c[i] = Z_0 * ... * Z_i
1293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001295 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001298 MOD_MUL( c[i] );
1299 }
1300
1301 /*
1302 * u = 1 / (Z_0 * ... * Z_n) mod P
1303 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001304 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001305
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001306 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001307 {
1308 /*
1309 * Zi = 1 / Z_i mod p
1310 * u = 1 / (Z_0 * ... * Z_i) mod P
1311 */
1312 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001314 }
1315 else
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
1318 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001319 }
1320
1321 /*
1322 * proceed as in normalize()
1323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1325 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
1326 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
1327 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001328
1329 /*
1330 * Post-precessing: reclaim some memory by shrinking coordinates
1331 * - not storing Z (always 1)
1332 * - shrinking other coordinates, but still keeping the same number of
1333 * limbs as P, as otherwise it will too likely be regrown too fast.
1334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1336 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1337 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001338
1339 if( i == 0 )
1340 break;
1341 }
1342
1343cleanup:
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001346 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_mpi_free( &c[i] );
1348 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001349
1350 return( ret );
1351}
1352
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001353/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001354 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1355 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1358 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001359 unsigned char inv )
1360{
1361 int ret;
1362 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001366
1367 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1369 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1370 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001371
1372cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001374
1375 return( ret );
1376}
1377
1378/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001379 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001380 *
Peter Dettmance661b22015-02-07 14:43:51 +07001381 * 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 +01001382 *
Peter Dettmance661b22015-02-07 14:43:51 +07001383 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1384 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1385 *
1386 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1387 *
1388 * Cost: 1D := 3M + 4S (A == 0)
1389 * 4M + 4S (A == -3)
1390 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1393 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001394{
1395 int ret;
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001396 mbedtls_mpi M, S, T, U;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001399 dbl_count++;
1400#endif
1401
Janos Follathb0697532016-08-18 12:38:46 +01001402#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001403 if( mbedtls_internal_ecp_grp_capable( grp ) )
1404 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001405#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001406
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001407 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 +01001408
1409 /* Special case for A = -3 */
1410 if( grp->A.p == NULL )
1411 {
Peter Dettmance661b22015-02-07 14:43:51 +07001412 /* M = 3(X + Z^2)(X - Z^2) */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001413 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1414 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
1415 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
1416 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
1417 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001418 }
1419 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001420 {
Peter Dettmance661b22015-02-07 14:43:51 +07001421 /* M = 3.X^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001422 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
1423 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001424
1425 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001426 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001427 {
1428 /* M += A.Z^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1430 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
1431 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
1432 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001433 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001434 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001435
Peter Dettmance661b22015-02-07 14:43:51 +07001436 /* S = 4.X.Y^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001437 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
1438 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
1439 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
1440 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001441
Peter Dettmance661b22015-02-07 14:43:51 +07001442 /* U = 8.Y^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001443 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
1444 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001445
1446 /* T = M^2 - 2.S */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001447 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
1448 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
1449 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
Peter Dettmance661b22015-02-07 14:43:51 +07001450
1451 /* S = M(S - T) - U */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001452 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
1453 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
1454 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
Peter Dettmance661b22015-02-07 14:43:51 +07001455
1456 /* U = 2.Y.Z */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001457 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
1458 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001459
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001460 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1461 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001463
1464cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001465 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 +02001466
1467 return( ret );
1468}
1469
1470/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001471 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001472 *
1473 * The coordinates of Q must be normalized (= affine),
1474 * but those of P don't need to. R is not normalized.
1475 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001476 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001477 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001478 * - at each step, P, Q and R are multiples of the base point, the factor
1479 * being less than its order, so none of them is zero;
1480 * - Q is an odd multiple of the base point, P an even multiple,
1481 * due to the choice of precomputed points in the modified comb method.
1482 * So branches for these cases do not leak secret information.
1483 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001484 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1485 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001486 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1489 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001490{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001491 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001495 add_count++;
1496#endif
1497
Janos Follathb0697532016-08-18 12:38:46 +01001498#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001499 if( mbedtls_internal_ecp_grp_capable( grp ) )
1500 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001501#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001502
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1522 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1523 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1524 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1525 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1526 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1544 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1545 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1546 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1547 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1548 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1549 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1550 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1551 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1552 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1553 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1554 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1557 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1558 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001559
1560cleanup:
1561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1563 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001564
1565 return( ret );
1566}
1567
1568/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001569 * Randomize jacobian coordinates:
1570 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001571 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001572 *
1573 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001576 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1577{
1578 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 mbedtls_mpi l, ll;
Janos Follathb0697532016-08-18 12:38:46 +01001580 size_t p_size;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001581 int count = 0;
1582
Janos Follathb0697532016-08-18 12:38:46 +01001583#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001584 if( mbedtls_internal_ecp_grp_capable( grp ) )
1585 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001586#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001587
1588 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001590
1591 /* Generate l such that 1 < l < p */
1592 do
1593 {
Ron Eldorca6ff582017-01-12 14:50:50 +02001594 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1597 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001598
1599 if( count++ > 10 )
Jonas6645fd32020-05-08 16:57:18 +09001600 {
1601 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
1602 goto cleanup;
1603 }
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001606
1607 /* Z = l * Z */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001609
1610 /* X = l^2 * X */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1612 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001613
1614 /* Y = l^3 * Y */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1616 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001617
1618cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001620
1621 return( ret );
1622}
1623
1624/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001625 * Check and define parameters used by the comb method (see below for details)
1626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1628#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001629#endif
1630
1631/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001633
1634/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001636
1637/*
1638 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001639 *
1640 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001641 * modified version that provides resistance to SPA by avoiding zero
1642 * digits in the representation as in [3]. We modify the method further by
1643 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001644 * representation uses one more K_i, due to carries, but saves on the size of
1645 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001646 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001647 * Summary of the comb method and its modifications:
1648 *
1649 * - The goal is to compute m*P for some w*d-bit integer m.
1650 *
1651 * - The basic comb method splits m into the w-bit integers
1652 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1653 * index has residue i modulo d, and computes m * P as
1654 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1655 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1656 *
1657 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1658 * .. + 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]] ..,
1659 * thereby successively converting it into a form where all summands
1660 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1661 *
1662 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1663 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1664 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1665 * Performing and iterating this procedure for those x[i] that are even
1666 * (keeping track of carry), we can transform the original sum into one of the form
1667 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1668 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1669 * which is why we are only computing half of it in the first place in
1670 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1671 *
1672 * - For the sake of compactness, only the seven low-order bits of x[i]
1673 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001674 * 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 +02001675 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001676 *
1677 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001678 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001679 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001681 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1682 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001683 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001684static void ecp_comb_recode_core( unsigned char x[], size_t d,
1685 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001686{
1687 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001688 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001689
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001690 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001691
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001692 /* First get the classical comb values (except for x_d = 0) */
1693 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001694 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001696
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001697 /* Now make sure x_1 .. x_d are odd */
1698 c = 0;
1699 for( i = 1; i <= d; i++ )
1700 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001701 /* Add carry and update it */
1702 cc = x[i] & c;
1703 x[i] = x[i] ^ c;
1704 c = cc;
1705
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001706 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001707 adjust = 1 - ( x[i] & 0x01 );
1708 c |= x[i] & ( x[i-1] * adjust );
1709 x[i] = x[i] ^ ( x[i-1] * adjust );
1710 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001711 }
1712}
1713
1714/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001715 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001716 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001717 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001718 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001719 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1720 * 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 +01001721 *
1722 * 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 +02001723 *
1724 * Note: Even comb values (those where P would be omitted from the
1725 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001726 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001727 *
1728 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001729 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001730 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1731 * (3) [add] Computation of all T[i]
1732 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001733 *
1734 * Step 1 can be interrupted but not the others; together with the final
1735 * coordinate normalization they are the largest steps done at once, depending
1736 * on the window size. Here are operation counts for P-256:
1737 *
1738 * step (2) (3) (4)
1739 * w = 5 142 165 208
1740 * w = 4 136 77 160
1741 * w = 3 130 33 136
1742 * w = 2 124 11 124
1743 *
1744 * So if ECC operations are blocking for too long even with a low max_ops
1745 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1746 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1749 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001750 unsigned char w, size_t d,
1751 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001752{
1753 int ret;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001754 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001755 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001756 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001758
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001759#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001760 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001761 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001762 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
1763 goto dbl;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001764 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001765 goto norm_dbl;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001766 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
1767 goto add;
1768 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
1769 goto norm_add;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001770 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001771#else
1772 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001773#endif
1774
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001775#if defined(MBEDTLS_ECP_RESTARTABLE)
1776 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1777 {
1778 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1779
1780 /* initial state for the loop */
1781 rs_ctx->rsm->i = 0;
1782 }
1783
1784dbl:
1785#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001786 /*
1787 * Set T[0] = P and
1788 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001791
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001792#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001793 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
1794 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001795 else
1796#endif
1797 j = 0;
1798
1799 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001800 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001801 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001802
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001803 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001804 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001805
1806 if( j % d == 0 )
1807 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1808
1809 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001810 }
1811
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001812#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001813 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1814 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1815
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001816norm_dbl:
1817#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001818 /*
1819 * Normalize current elements in T. As T has holes,
1820 * use an auxiliary array of pointers to elements in T.
1821 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001822 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001823 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001824 TT[j++] = T + i;
1825
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001826 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001827
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001828 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001829
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001830#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001831 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1832 rs_ctx->rsm->state = ecp_rsm_pre_add;
1833
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001834add:
1835#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001836 /*
1837 * Compute the remaining ones using the minimal number of additions
1838 * Be careful to update T[2^l] only after using it!
1839 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001840 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001841
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001842 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001843 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001844 j = i;
1845 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001847 }
1848
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001849#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001850 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1851 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1852
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001853norm_add:
1854#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001855 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001856 * Normalize final elements in T. Even though there are no holes now, we
1857 * still need the auxiliary array for homogeneity with the previous
1858 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001859 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001860 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001861 TT[j] = T + j + 1;
1862
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001863 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001864
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001865 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001866
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001867cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001868#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001869 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1870 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001871 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001872 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001873 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001874 }
1875#endif
Janos Follathb0697532016-08-18 12:38:46 +01001876
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001877 return( ret );
1878}
1879
1880/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001881 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001882 *
1883 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001886 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001887 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001888{
1889 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001890 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001891
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001892 /* Ignore the "sign" bit and scale down */
1893 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001894
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001895 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001896 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1899 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001900 }
1901
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001902 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001904
1905cleanup:
1906 return( ret );
1907}
1908
1909/*
1910 * Core multiplication algorithm for the (modified) comb method.
1911 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001912 *
1913 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001916 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001917 const unsigned char x[], size_t d,
1918 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001919 void *p_rng,
1920 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001921{
1922 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001924 size_t i;
1925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001927
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001928#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001929 (void) rs_ctx;
1930#endif
1931
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001932#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001933 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1934 rs_ctx->rsm->state != ecp_rsm_comb_core )
1935 {
1936 rs_ctx->rsm->i = 0;
1937 rs_ctx->rsm->state = ecp_rsm_comb_core;
1938 }
1939
1940 /* new 'if' instead of nested for the sake of the 'else' branch */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001941 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001942 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001943 /* restore current index (R already pointing to rs_ctx->rsm->R) */
1944 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001945 }
1946 else
1947#endif
1948 {
1949 /* Start with a non-zero point and randomize its coordinates */
1950 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001951 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001952 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02001953#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001954 if( f_rng != 0 )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02001955#endif
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001956 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
1957 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001958
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001959 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001960 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001961 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001962 --i;
1963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001965 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001967 }
1968
1969cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001972
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001973#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001974 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1975 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001976 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02001977 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001978 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001979 }
1980#endif
1981
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001982 return( ret );
1983}
1984
1985/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001986 * Recode the scalar to get constant-time comb multiplication
1987 *
1988 * As the actual scalar recoding needs an odd scalar as a starting point,
1989 * this wrapper ensures that by replacing m by N - m if necessary, and
1990 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001991 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02001992 * This works because we only support large prime order for Short Weierstrass
1993 * curves, so N is always odd hence either m or N - m is.
1994 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001995 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001996 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001997static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
1998 const mbedtls_mpi *m,
1999 unsigned char k[COMB_MAX_D + 1],
2000 size_t d,
2001 unsigned char w,
2002 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002003{
2004 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002005 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002006
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002007 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002008 mbedtls_mpi_init( &mm );
2009
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002010 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002011 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
2012 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2013
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002014 /* do we need the parity trick? */
2015 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
2016
2017 /* execute parity fix in constant time */
2018 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002019 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002020 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
2021
2022 /* actual scalar recoding */
2023 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002024
2025cleanup:
2026 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002027 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002028
2029 return( ret );
2030}
2031
2032/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002033 * Perform comb multiplication (for short Weierstrass curves)
2034 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002035 *
2036 * Scalar recoding may use a parity trick that makes us compute -m * P,
2037 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002038 */
2039static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
2040 mbedtls_ecp_point *R,
2041 const mbedtls_mpi *m,
2042 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002043 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002044 unsigned char w,
2045 size_t d,
2046 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002047 void *p_rng,
2048 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002049{
2050 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002051 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002052 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002053 mbedtls_ecp_point *RR = R;
2054
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002055#if defined(MBEDTLS_ECP_RESTARTABLE)
2056 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2057 {
2058 RR = &rs_ctx->rsm->R;
2059
2060 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
2061 goto final_norm;
2062 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002063#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002064
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002065 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
2066 &parity_trick ) );
2067 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
2068 f_rng, p_rng, rs_ctx ) );
2069 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
2070
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002071#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002072 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002073 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002074
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002075final_norm:
Manuel Pégourié-Gonnard18b0b3c2020-06-08 09:53:20 +02002076 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002077#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002078 /*
2079 * Knowledge of the jacobian coordinates may leak the last few bits of the
2080 * scalar [1], and since our MPI implementation isn't constant-flow,
2081 * inversion (used for coordinate normalization) may leak the full value
2082 * of its input via side-channels [2].
2083 *
2084 * [1] https://eprint.iacr.org/2003/191
2085 * [2] https://eprint.iacr.org/2020/055
2086 *
2087 * Avoid the leak by randomizing coordinates before we normalize them.
2088 */
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002089#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002090 if( f_rng != 0 )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002091#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002092 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
2093
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002094 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
2095
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002096#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02002097 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2098 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002099#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002100
2101cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002102 return( ret );
2103}
2104
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002105/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002106 * Pick window size based on curve size and whether we optimize for base point
2107 */
2108static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
2109 unsigned char p_eq_g )
2110{
2111 unsigned char w;
2112
2113 /*
2114 * Minimize the number of multiplications, that is minimize
2115 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2116 * (see costs of the various parts, with 1S = 1M)
2117 */
2118 w = grp->nbits >= 384 ? 5 : 4;
2119
2120 /*
2121 * If P == G, pre-compute a bit more, since this may be re-used later.
2122 * Just adding one avoids upping the cost of the first mul too much,
2123 * and the memory cost too.
2124 */
2125 if( p_eq_g )
2126 w++;
2127
2128 /*
2129 * Make sure w is within bounds.
2130 * (The last test is useful only for very small curves in the test suite.)
2131 */
2132 if( w > MBEDTLS_ECP_WINDOW_SIZE )
2133 w = MBEDTLS_ECP_WINDOW_SIZE;
2134 if( w >= grp->nbits )
2135 w = 2;
2136
2137 return( w );
2138}
2139
2140/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002141 * Multiplication using the comb method - for curves in short Weierstrass form
2142 *
2143 * This function is mainly responsible for administrative work:
2144 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002145 * - managing the table of precomputed points (passed between the below two
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002146 * functions): allocation, computation, ownership tranfer, freeing.
2147 *
2148 * It delegates the actual arithmetic work to:
2149 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2150 *
2151 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2154 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002155 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002156 void *p_rng,
2157 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002158{
2159 int ret;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002160 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002161 size_t d;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002162 unsigned char T_size = 0, T_ok = 0;
2163 mbedtls_ecp_point *T = NULL;
2164#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2165 ecp_drbg_context drbg_ctx;
2166
2167 ecp_drbg_init( &drbg_ctx );
2168#endif
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002169
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002170 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002171
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002172#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2173 if( f_rng == NULL )
2174 {
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +02002175 /* Adjust pointers */
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002176 f_rng = &ecp_drbg_random;
Manuel Pégourié-Gonnard047986c2020-06-04 09:43:14 +02002177#if defined(MBEDTLS_ECP_RESTARTABLE)
2178 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2179 p_rng = &rs_ctx->rsm->drbg_ctx;
2180 else
2181#endif
2182 p_rng = &drbg_ctx;
2183
2184 /* Initialize internal DRBG if necessary */
2185#if defined(MBEDTLS_ECP_RESTARTABLE)
2186 if( rs_ctx == NULL || rs_ctx->rsm == NULL ||
2187 rs_ctx->rsm->drbg_seeded == 0 )
2188#endif
2189 {
2190 MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m ) );
2191 }
2192#if defined(MBEDTLS_ECP_RESTARTABLE)
2193 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2194 rs_ctx->rsm->drbg_seeded = 1;
2195#endif
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002196 }
2197#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2198
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002199 /* Is P the base point ? */
2200#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2201 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
2202 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002203#else
2204 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002205#endif
2206
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002207 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002208 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002209 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002210 d = ( grp->nbits + w - 1 ) / w;
2211
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002212 /* Pre-computed table: do we have it already for the base point? */
2213 if( p_eq_g && grp->T != NULL )
2214 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002215 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002216 T = grp->T;
2217 T_ok = 1;
2218 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002219 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002220#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002221 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002222 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002223 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002224 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002225 T = rs_ctx->rsm->T;
2226 rs_ctx->rsm->T = NULL;
2227 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002228
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002229 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002230 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002231 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002232 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002233#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002234 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002235 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002236 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002237 if( T == NULL )
2238 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002239 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002240 goto cleanup;
2241 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002242
2243 for( i = 0; i < T_size; i++ )
2244 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002245
2246 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002247 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002248
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002249 /* Compute table (or finish computing it) if not done already */
2250 if( !T_ok )
2251 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002252 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002253
2254 if( p_eq_g )
2255 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002256 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002257 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002258 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002259 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002260 }
2261 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002262
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002263 /* Actual comb multiplication using precomputed points */
2264 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002265 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002266 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002267
2268cleanup:
2269
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002270#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2271 ecp_drbg_free( &drbg_ctx );
2272#endif
2273
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002274 /* does T belong to the group? */
2275 if( T == grp->T )
2276 T = NULL;
2277
2278 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002279#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002280 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 +01002281 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002282 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002283 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002284 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002285 T = NULL;
2286 }
2287#endif
2288
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002289 /* did T belong to us? then let's destroy it! */
2290 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002291 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002292 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 mbedtls_ecp_point_free( &T[i] );
2294 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002295 }
2296
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002297 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002298#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002299 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2300#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002301 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01002302 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002304
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002305 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002306
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002307 return( ret );
2308}
2309
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002310#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002311
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002312#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002313/*
2314 * For Montgomery curves, we do all the internal arithmetic in projective
2315 * coordinates. Import/export of points uses only the x coordinates, which is
2316 * internaly represented as X / Z.
2317 *
2318 * For scalar multiplication, we'll use a Montgomery ladder.
2319 */
2320
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002321/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002322 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2323 * Cost: 1M + 1I
2324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002326{
2327 int ret;
2328
Janos Follathb0697532016-08-18 12:38:46 +01002329#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002330 if( mbedtls_internal_ecp_grp_capable( grp ) )
2331 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002332#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
2335 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
2336 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002337
2338cleanup:
2339 return( ret );
2340}
2341
2342/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002343 * Randomize projective x/z coordinates:
2344 * (X, Z) -> (l X, l Z) for random l
2345 * This is sort of the reverse operation of ecp_normalize_mxz().
2346 *
2347 * This countermeasure was first suggested in [2].
2348 * Cost: 2M
2349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002351 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2352{
2353 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 mbedtls_mpi l;
Janos Follathb0697532016-08-18 12:38:46 +01002355 size_t p_size;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002356 int count = 0;
2357
Janos Follathb0697532016-08-18 12:38:46 +01002358#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002359 if( mbedtls_internal_ecp_grp_capable( grp ) )
2360 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Janos Follath372697b2016-10-28 16:53:11 +01002361#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002362
2363 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002365
2366 /* Generate l such that 1 < l < p */
2367 do
2368 {
Ron Eldorca6ff582017-01-12 14:50:50 +02002369 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
2372 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002373
2374 if( count++ > 10 )
Jonas6645fd32020-05-08 16:57:18 +09002375 {
2376 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
2377 goto cleanup;
2378 }
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002379 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
2383 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002384
2385cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002387
2388 return( ret );
2389}
2390
2391/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002392 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2393 * for Montgomery curves in x/z coordinates.
2394 *
2395 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2396 * with
2397 * d = X1
2398 * P = (X2, Z2)
2399 * Q = (X3, Z3)
2400 * R = (X4, Z4)
2401 * S = (X5, Z5)
2402 * and eliminating temporary variables tO, ..., t4.
2403 *
2404 * Cost: 5M + 4S
2405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2407 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2408 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2409 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002410{
2411 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002413
Janos Follathb0697532016-08-18 12:38:46 +01002414#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002415 if( mbedtls_internal_ecp_grp_capable( grp ) )
2416 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002417#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2420 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2421 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
2424 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
2425 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
2429 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
2430 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
2431 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
2432 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
2433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
2435 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
2436 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
2437 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
2438 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
2439 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
2440 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002441
2442cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2444 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2445 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002446
2447 return( ret );
2448}
2449
2450/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002451 * Multiplication with Montgomery ladder in x/z coordinates,
2452 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2455 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002456 int (*f_rng)(void *, unsigned char *, size_t),
2457 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002458{
2459 int ret;
2460 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002461 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 mbedtls_ecp_point RP;
2463 mbedtls_mpi PX;
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002464#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2465 ecp_drbg_context drbg_ctx;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002466
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002467 ecp_drbg_init( &drbg_ctx );
2468#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002470
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002471#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2472 if( f_rng == NULL )
2473 {
2474 MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) );
2475 f_rng = &ecp_drbg_random;
2476 p_rng = &drbg_ctx;
2477 }
2478#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2479
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002480 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2482 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002483
2484 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2486 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2487 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002488
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002489 /* RP.X might be sligtly larger than P, so reduce it */
2490 MOD_ADD( RP.X );
2491
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002492 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002493#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002494 if( f_rng != NULL )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002495#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002497
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002498 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002499 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002500 while( i-- > 0 )
2501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002503 /*
2504 * if (b) R = 2R + P else R = 2R,
2505 * which is:
2506 * if (b) double_add( RP, R, RP, R )
2507 * else double_add( R, RP, R, RP )
2508 * but using safe conditional swaps to avoid leaks
2509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2511 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2512 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2513 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2514 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002515 }
2516
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002517 /*
2518 * Knowledge of the projective coordinates may leak the last few bits of the
2519 * scalar [1], and since our MPI implementation isn't constant-flow,
2520 * inversion (used for coordinate normalization) may leak the full value
2521 * of its input via side-channels [2].
2522 *
2523 * [1] https://eprint.iacr.org/2003/191
2524 * [2] https://eprint.iacr.org/2020/055
2525 *
2526 * Avoid the leak by randomizing coordinates before we normalize them.
2527 */
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002528#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002529 if( f_rng != NULL )
Manuel Pégourié-Gonnardc334f412020-06-04 10:43:29 +02002530#endif
Manuel Pégourié-Gonnardf6004162020-03-25 12:41:29 +01002531 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
2532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002534
2535cleanup:
Manuel Pégourié-Gonnardd18f0512020-06-03 12:11:56 +02002536#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2537 ecp_drbg_free( &drbg_ctx );
2538#endif
2539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002541
2542 return( ret );
2543}
2544
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002545#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002546
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002547/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002548 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002549 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002550int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002552 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2553 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002554{
Janos Follathb0697532016-08-18 12:38:46 +01002555 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002556#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2557 char is_grp_capable = 0;
2558#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002559 ECP_VALIDATE_RET( grp != NULL );
2560 ECP_VALIDATE_RET( R != NULL );
2561 ECP_VALIDATE_RET( m != NULL );
2562 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002563
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002564#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002565 /* reset ops count for this call if top-level */
2566 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2567 rs_ctx->ops_done = 0;
2568#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002569
Janos Follathc44ab972016-11-18 16:38:23 +00002570#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002571 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002572 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002573#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002574
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002575#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002576 /* skip argument check when restarting */
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002577 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002578#endif
2579 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002580 /* check_privkey is free */
2581 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2582
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002583 /* Common sanity checks */
2584 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2585 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002586 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002587
2588 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002589#if defined(ECP_MONTGOMERY)
2590 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002591 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002592#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002593#if defined(ECP_SHORTWEIERSTRASS)
2594 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002595 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002596#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002597
Janos Follath6c8ccd52016-11-29 15:37:09 +00002598cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002599
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002600#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002601 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002602 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002603#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002604
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002605#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002606 if( rs_ctx != NULL )
2607 rs_ctx->depth--;
2608#endif
2609
Janos Follathb0697532016-08-18 12:38:46 +01002610 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002611}
2612
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002613/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002614 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002615 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002616int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002617 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002618 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002619{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002620 ECP_VALIDATE_RET( grp != NULL );
2621 ECP_VALIDATE_RET( R != NULL );
2622 ECP_VALIDATE_RET( m != NULL );
2623 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002624 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002625}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002626
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002627#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002628/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002629 * Check that an affine point is valid as a public key,
2630 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002633{
2634 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002636
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002637 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2639 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2640 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2641 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2642 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002645
2646 /*
2647 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002648 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
2651 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002652
2653 /* Special case for A = -3 */
2654 if( grp->A.p == NULL )
2655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002657 }
2658 else
2659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002661 }
2662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
2664 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2667 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002668
2669cleanup:
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002672
2673 return( ret );
2674}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002675#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002676
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002677/*
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002678 * R = m * P with shortcuts for m == 1 and m == -1
2679 * NOT constant-time - ONLY for short Weierstrass!
2680 */
2681static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2682 mbedtls_ecp_point *R,
2683 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002684 const mbedtls_ecp_point *P,
2685 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002686{
2687 int ret;
2688
2689 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
2690 {
2691 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2692 }
2693 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2694 {
2695 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2696 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2697 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2698 }
2699 else
2700 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002701 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2702 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002703 }
2704
2705cleanup:
2706 return( ret );
2707}
2708
2709/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002710 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002711 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002712 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002713int mbedtls_ecp_muladd_restartable(
2714 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002715 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002716 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2717 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002718{
2719 int ret;
2720 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002721 mbedtls_ecp_point *pmP = &mP;
2722 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002723#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2724 char is_grp_capable = 0;
2725#endif
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002726 ECP_VALIDATE_RET( grp != NULL );
2727 ECP_VALIDATE_RET( R != NULL );
2728 ECP_VALIDATE_RET( m != NULL );
2729 ECP_VALIDATE_RET( P != NULL );
2730 ECP_VALIDATE_RET( n != NULL );
2731 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002732
2733 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
2734 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2735
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002736 mbedtls_ecp_point_init( &mP );
2737
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002738 ECP_RS_ENTER( ma );
2739
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002740#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002741 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2742 {
2743 /* redirect intermediate results to restart context */
2744 pmP = &rs_ctx->ma->mP;
2745 pR = &rs_ctx->ma->R;
2746
2747 /* jump to next operation */
2748 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2749 goto mul2;
2750 if( rs_ctx->ma->state == ecp_rsma_add )
2751 goto add;
2752 if( rs_ctx->ma->state == ecp_rsma_norm )
2753 goto norm;
2754 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002755#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002756
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002757 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002758#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002759 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002760 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002761
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002762mul2:
2763#endif
2764 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Janos Follathaf6f2692018-12-07 09:55:24 +00002765
2766#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2767 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2768 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2769#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2770
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002771#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002772 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002773 rs_ctx->ma->state = ecp_rsma_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002774
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002775add:
2776#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002777 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002778 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002779#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002780 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002781 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002782
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002783norm:
2784#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002785 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002786 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2787
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002788#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002789 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2790 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2791#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002792
2793cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002794#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002795 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002796 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002797#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002798
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002799 mbedtls_ecp_point_free( &mP );
2800
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002801 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002802
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002803 return( ret );
2804}
2805
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002806/*
2807 * Linear combination
2808 * NOT constant-time
2809 */
2810int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2811 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2812 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2813{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002814 ECP_VALIDATE_RET( grp != NULL );
2815 ECP_VALIDATE_RET( R != NULL );
2816 ECP_VALIDATE_RET( m != NULL );
2817 ECP_VALIDATE_RET( P != NULL );
2818 ECP_VALIDATE_RET( n != NULL );
2819 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002820 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2821}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002822
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002823#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002824/*
2825 * Check validity of a public key for Montgomery curves with x-only schemes
2826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002828{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002829 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002830 /* Allow any public value, if it's too big then we'll just reduce it mod p
2831 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2833 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002834
2835 return( 0 );
2836}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002837#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002838
2839/*
2840 * Check that a point is valid as a public key
2841 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002842int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
2843 const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002844{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002845 ECP_VALIDATE_RET( grp != NULL );
2846 ECP_VALIDATE_RET( pt != NULL );
2847
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002848 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2850 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002851
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002852#if defined(ECP_MONTGOMERY)
2853 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002854 return( ecp_check_pubkey_mx( grp, pt ) );
2855#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002856#if defined(ECP_SHORTWEIERSTRASS)
2857 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002858 return( ecp_check_pubkey_sw( grp, pt ) );
2859#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002861}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002862
2863/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002865 */
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002866int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
2867 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002868{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002869 ECP_VALIDATE_RET( grp != NULL );
2870 ECP_VALIDATE_RET( d != NULL );
2871
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002872#if defined(ECP_MONTGOMERY)
2873 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002874 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002875 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2877 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002878 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002880
2881 /* see [Curve25519] page 5 */
2882 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
2883 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2884
2885 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002886 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002887#endif /* ECP_MONTGOMERY */
2888#if defined(ECP_SHORTWEIERSTRASS)
2889 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002890 {
2891 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2893 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2894 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002895 else
2896 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002897 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002898#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002901}
2902
2903/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002904 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002905 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002906int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
2907 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002908 int (*f_rng)(void *, unsigned char *, size_t),
2909 void *p_rng )
2910{
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002911 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00002912 size_t n_size;
2913
2914 ECP_VALIDATE_RET( grp != NULL );
2915 ECP_VALIDATE_RET( d != NULL );
2916 ECP_VALIDATE_RET( f_rng != NULL );
2917
2918 n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002919
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002920#if defined(ECP_MONTGOMERY)
2921 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002922 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002923 /* [M225] page 5 */
2924 size_t b;
2925
Janos Follath98e28a72016-05-31 14:03:54 +01002926 do {
2927 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
2928 } while( mbedtls_mpi_bitlen( d ) == 0);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002929
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002930 /* Make sure the most significant bit is nbits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002931 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002932 if( b > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002934 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002936
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002937 /* Make sure the last two bits are unset for Curve448, three bits for
2938 Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
2940 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002941 if( grp->nbits == 254 )
2942 {
2943 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
2944 }
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002945 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002946#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002947
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002948#if defined(ECP_SHORTWEIERSTRASS)
2949 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002950 {
2951 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002952 int count = 0;
Janos Follath867a3ab2019-10-11 14:21:53 +01002953 unsigned cmp = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01002954
2955 /*
2956 * Match the procedure given in RFC 6979 (deterministic ECDSA):
2957 * - use the same byte ordering;
2958 * - keep the leftmost nbits bits of the generated octet string;
2959 * - try until result is in the desired range.
2960 * This also avoids any biais, which is especially important for ECDSA.
2961 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002962 do
2963 {
Hanno Becker7c8cb9c2017-10-17 15:19:38 +01002964 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002966
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01002967 /*
2968 * Each try has at worst a probability 1/2 of failing (the msb has
2969 * a probability 1/2 of being 0, and then the result will be < N),
2970 * so after 30 tries failure probability is a most 2**(-30).
2971 *
2972 * For most curves, 1 try is enough with overwhelming probability,
2973 * since N starts with a lot of 1s in binary, but some curves
2974 * such as secp224k1 are actually very close to the worst case.
2975 */
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02002976 if( ++count > 30 )
2977 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Janos Follath4c3408b2019-09-16 14:27:39 +01002978
Janos Follath867a3ab2019-10-11 14:21:53 +01002979 ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
Janos Follath4c3408b2019-09-16 14:27:39 +01002980 if( ret != 0 )
2981 {
2982 goto cleanup;
2983 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002984 }
Janos Follath867a3ab2019-10-11 14:21:53 +01002985 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002986 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002987#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002988
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002989cleanup:
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002990 return( ret );
2991}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002992
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002993/*
2994 * Generate a keypair with configurable base point
2995 */
2996int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
2997 const mbedtls_ecp_point *G,
2998 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2999 int (*f_rng)(void *, unsigned char *, size_t),
3000 void *p_rng )
3001{
3002 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003003 ECP_VALIDATE_RET( grp != NULL );
3004 ECP_VALIDATE_RET( d != NULL );
3005 ECP_VALIDATE_RET( G != NULL );
3006 ECP_VALIDATE_RET( Q != NULL );
3007 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003008
3009 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
3010 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
3011
3012cleanup:
3013 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003014}
3015
3016/*
3017 * Generate key pair, wrapper for conventional base point
3018 */
3019int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
3020 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3021 int (*f_rng)(void *, unsigned char *, size_t),
3022 void *p_rng )
3023{
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003024 ECP_VALIDATE_RET( grp != NULL );
3025 ECP_VALIDATE_RET( d != NULL );
3026 ECP_VALIDATE_RET( Q != NULL );
3027 ECP_VALIDATE_RET( f_rng != NULL );
3028
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003029 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003030}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003031
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003032/*
3033 * Generate a keypair, prettier wrapper
3034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003036 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
3037{
3038 int ret;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003039 ECP_VALIDATE_RET( key != NULL );
3040 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003041
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003042 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003043 return( ret );
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003046}
3047
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003048/*
3049 * Check a public-private key pair
3050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003052{
3053 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 mbedtls_ecp_point Q;
3055 mbedtls_ecp_group grp;
Hanno Becker4f8e8e52018-12-14 15:08:03 +00003056 ECP_VALIDATE_RET( pub != NULL );
3057 ECP_VALIDATE_RET( prv != NULL );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003060 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
3062 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
3063 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003066 }
3067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 mbedtls_ecp_point_init( &Q );
3069 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 /* mbedtls_ecp_mul() needs a non-const group... */
3072 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003073
3074 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
3078 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
3079 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003082 goto cleanup;
3083 }
3084
3085cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 mbedtls_ecp_point_free( &Q );
3087 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003088
3089 return( ret );
3090}
3091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003093
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003094/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003095 * Checkup routine
3096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003098{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003099 int ret;
3100 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 mbedtls_ecp_group grp;
3102 mbedtls_ecp_point R, P;
3103 mbedtls_mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003104 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003105 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003106 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003107 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003108 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003109 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003110 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003111 "400000000000000000000000000000000000000000000000", /* one and zeros */
3112 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3113 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003114 };
3115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 mbedtls_ecp_group_init( &grp );
3117 mbedtls_ecp_point_init( &R );
3118 mbedtls_ecp_point_init( &P );
3119 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003120
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003121 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003123 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003124#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003125 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003126#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003127
3128 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003130
3131 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
3133 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003134
3135 add_count = 0;
3136 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003137 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3139 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003140
3141 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3142 {
3143 add_c_prev = add_count;
3144 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003145 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003146 add_count = 0;
3147 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003148 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3151 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003152
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003153 if( add_count != add_c_prev ||
3154 dbl_count != dbl_c_prev ||
3155 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003156 {
3157 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003159
3160 ret = 1;
3161 goto cleanup;
3162 }
3163 }
3164
3165 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003167
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003168 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003170 /* We computed P = 2G last time, use it */
3171
3172 add_count = 0;
3173 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003174 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3176 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003177
3178 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3179 {
3180 add_c_prev = add_count;
3181 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003182 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003183 add_count = 0;
3184 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003185 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3188 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003189
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003190 if( add_count != add_c_prev ||
3191 dbl_count != dbl_c_prev ||
3192 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003193 {
3194 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003196
3197 ret = 1;
3198 goto cleanup;
3199 }
3200 }
3201
3202 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003204
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003205cleanup:
3206
3207 if( ret < 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 mbedtls_ecp_group_free( &grp );
3211 mbedtls_ecp_point_free( &R );
3212 mbedtls_ecp_point_free( &P );
3213 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003214
3215 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003217
3218 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003219}
3220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003222
Janos Follathb0697532016-08-18 12:38:46 +01003223#endif /* !MBEDTLS_ECP_ALT */
3224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#endif /* MBEDTLS_ECP_C */