blob: b8ce020be41b32499a68ef2b6a5ffae14c49c586 [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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050050/**
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"
Janos Follath24eed8d2019-11-22 13:21:35 +000084#include "mbedtls/error.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020085
Rich Evans00ab4702015-02-06 13:43:58 +000086#include <string.h>
87
Janos Follathb0697532016-08-18 12:38:46 +010088#if !defined(MBEDTLS_ECP_ALT)
89
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090/* Parameter validation macros based on platform_util.h */
91#define ECP_VALIDATE_RET( cond ) \
92 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
93#define ECP_VALIDATE( cond ) \
94 MBEDTLS_INTERNAL_VALIDATE( cond )
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000097#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020098#else
Rich Evans00ab4702015-02-06 13:43:58 +000099#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +0000100#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200102#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +0200104#endif
105
Janos Follath47d28f02016-11-01 13:22:05 +0000106#include "mbedtls/ecp_internal.h"
Janos Follathb0697532016-08-18 12:38:46 +0100107
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200108#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
109#if defined(MBEDTLS_CTR_DRBG_C)
110#include "mbedtls/ctr_drbg.h"
111#elif defined(MBEDTLS_HMAC_DRBG_C)
112#include "mbedtls/hmac_drbg.h"
113#else
114#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
115#endif
116#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
117
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +0100118#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
119 !defined(inline) && !defined(__cplusplus)
Paul Bakker6a6087e2013-10-28 18:53:08 +0100120#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +0200121#endif
Paul Bakker6a6087e2013-10-28 18:53:08 +0100122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100124/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100125 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200126 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100127 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100128static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100129#endif
130
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200131#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
132/*
133 * Currently ecp_mul() takes a RNG function as an argument, used for
134 * side-channel protection, but it can be NULL. The initial reasonning was
135 * that people will pass non-NULL RNG when they care about side-channels, but
136 * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with
137 * no opportunity for the user to do anything about it.
138 *
139 * The obvious strategies for addressing that include:
140 * - change those APIs so that they take RNG arguments;
141 * - require a global RNG to be available to all crypto modules.
142 *
143 * Unfortunately those would break compatibility. So what we do instead is
144 * have our own internal DRBG instance, seeded from the secret scalar.
145 *
146 * The following is a light-weight abstraction layer for doing that with
147 * CTR_DRBG or HMAC_DRBG.
148 */
149
150#if defined(MBEDTLS_CTR_DRBG_C)
151/* DRBG context type */
152typedef mbedtls_ctr_drbg_context ecp_drbg_context;
153
154/* DRBG context init */
155static inline void ecp_drbg_init( ecp_drbg_context *ctx )
156{
157 mbedtls_ctr_drbg_init( ctx );
158}
159
160/* DRBG context free */
161static inline void ecp_drbg_free( ecp_drbg_context *ctx )
162{
163 mbedtls_ctr_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_ctr_drbg_random( p_rng, output, output_len ) );
171}
172
173/*
174 * Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does,
175 * we need to pass an entropy function when seeding. So we use a dummy
176 * function for that, and pass the actual entropy as customisation string.
177 * (During seeding of CTR_DRBG the entropy input and customisation string are
178 * concatenated before being used to update the secret state.)
179 */
180static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
181{
182 (void) ctx;
183 memset( out, 0, len );
184 return( 0 );
185}
186
187/* DRBG context seeding */
188static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret )
189{
190 const unsigned char *secret_p = (const unsigned char *) secret->p;
191 const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint );
192
193 return( mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL,
194 secret_p, secret_size ) );
195}
196
197#elif defined(MBEDTLS_HMAC_DRBG_C)
198/* DRBG context type */
199typedef mbedtls_hmac_drbg_context ecp_drbg_context;
200
201/* DRBG context init */
202static inline void ecp_drbg_init( ecp_drbg_context *ctx )
203{
204 mbedtls_hmac_drbg_init( ctx );
205}
206
207/* DRBG context free */
208static inline void ecp_drbg_free( ecp_drbg_context *ctx )
209{
210 mbedtls_hmac_drbg_free( ctx );
211}
212
213/* DRBG function */
214static inline int ecp_drbg_random( void *p_rng,
215 unsigned char *output, size_t output_len )
216{
217 return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
218}
219
220/* DRBG context seeding */
221static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret )
222{
223 const unsigned char *secret_p = (const unsigned char *) secret->p;
224 const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint );
225
226 /* The list starts with strong hashes */
227 const mbedtls_md_type_t md_type = mbedtls_md_list()[0];
228 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type );
229
230 return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_size ) );
231}
232
233#else
234#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
235#endif /* DRBG modules */
236#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
237
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200238#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100239/*
240 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200241 *
242 * Default value 0 means that ECC operations will not yield.
243 * Note that regardless of the value of ecp_max_ops, always at
244 * least one step is performed before yielding.
245 *
246 * Setting ecp_max_ops=1 can be suitable for testing purposes
247 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100248 */
249static unsigned ecp_max_ops = 0;
250
251/*
252 * Set ecp_max_ops
253 */
254void mbedtls_ecp_set_max_ops( unsigned max_ops )
255{
256 ecp_max_ops = max_ops;
257}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100258
259/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200260 * Check if restart is enabled
261 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200262int mbedtls_ecp_restart_is_enabled( void )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200263{
264 return( ecp_max_ops != 0 );
265}
266
267/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200268 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100269 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200270struct mbedtls_ecp_restart_mul
271{
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100272 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100273 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100274 mbedtls_ecp_point *T; /* table for precomputed points */
275 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200276 enum { /* what were we doing last time we returned? */
277 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
278 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100279 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
280 ecp_rsm_pre_add, /* precompute remaining points by adding */
281 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200282 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100283 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100284 } state;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200285#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
286 ecp_drbg_context drbg_ctx;
287 unsigned char drbg_seeded;
288#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100289};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100290
291/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200292 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100293 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200294static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100295{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200296 mbedtls_ecp_point_init( &ctx->R );
297 ctx->i = 0;
298 ctx->T = NULL;
299 ctx->T_size = 0;
300 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200301#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
302 ecp_drbg_init( &ctx->drbg_ctx );
303 ctx->drbg_seeded = 0;
304#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100305}
306
307/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200308 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100309 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200310static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100311{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100312 unsigned char i;
313
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100314 if( ctx == NULL )
315 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100316
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100317 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100318
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200319 if( ctx->T != NULL )
320 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100321 for( i = 0; i < ctx->T_size; i++ )
322 mbedtls_ecp_point_free( ctx->T + i );
323 mbedtls_free( ctx->T );
324 }
325
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200326#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
327 ecp_drbg_free( &ctx->drbg_ctx );
328#endif
329
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200330 ecp_restart_rsm_init( ctx );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100331}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100332
333/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200334 * Restart context for ecp_muladd()
335 */
336struct mbedtls_ecp_restart_muladd
337{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200338 mbedtls_ecp_point mP; /* mP value */
339 mbedtls_ecp_point R; /* R intermediate result */
340 enum { /* what should we do next? */
341 ecp_rsma_mul1 = 0, /* first multiplication */
342 ecp_rsma_mul2, /* second multiplication */
343 ecp_rsma_add, /* addition */
344 ecp_rsma_norm, /* normalization */
345 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200346};
347
348/*
349 * Init restart_muladd sub-context
350 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200351static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200352{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200353 mbedtls_ecp_point_init( &ctx->mP );
354 mbedtls_ecp_point_init( &ctx->R );
355 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200356}
357
358/*
359 * Free the components of a restart_muladd sub-context
360 */
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200361static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200362{
363 if( ctx == NULL )
364 return;
365
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200366 mbedtls_ecp_point_free( &ctx->mP );
367 mbedtls_ecp_point_free( &ctx->R );
368
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200369 ecp_restart_ma_init( ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200370}
371
372/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200373 * Initialize a restart context
374 */
375void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
376{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 ECP_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200378 ctx->ops_done = 0;
379 ctx->depth = 0;
380 ctx->rsm = NULL;
381 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200382}
383
384/*
385 * Free the components of a restart context
386 */
387void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
388{
389 if( ctx == NULL )
390 return;
391
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200392 ecp_restart_rsm_free( ctx->rsm );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200393 mbedtls_free( ctx->rsm );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200394
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200395 ecp_restart_ma_free( ctx->ma );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200396 mbedtls_free( ctx->ma );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200397
398 mbedtls_ecp_restart_init( ctx );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200399}
400
401/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100402 * Check if we can do the next step
403 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200404int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
405 mbedtls_ecp_restart_ctx *rs_ctx,
406 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100407{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500408 ECP_VALIDATE_RET( grp != NULL );
409
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200410 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100411 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100412 /* scale depending on curve size: the chosen reference is 256-bit,
413 * and multiplication is quadratic. Round to the closest integer. */
414 if( grp->pbits >= 512 )
415 ops *= 4;
416 else if( grp->pbits >= 384 )
417 ops *= 2;
418
Hanno Beckerb10c6602018-10-26 13:50:13 +0100419 /* Avoid infinite loops: always allow first step.
420 * Because of that, however, it's not generally true
421 * that ops_done <= ecp_max_ops, so the check
422 * ops_done > ecp_max_ops below is mandatory. */
423 if( ( rs_ctx->ops_done != 0 ) &&
424 ( rs_ctx->ops_done > ecp_max_ops ||
425 ops > ecp_max_ops - rs_ctx->ops_done ) )
426 {
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100427 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
Hanno Beckerb10c6602018-10-26 13:50:13 +0100428 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100429
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100430 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200431 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100432 }
433
434 return( 0 );
435}
436
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200437/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200438#define ECP_RS_ENTER( SUB ) do { \
439 /* reset ops count for this call if top-level */ \
440 if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
441 rs_ctx->ops_done = 0; \
442 \
443 /* set up our own sub-context if needed */ \
444 if( mbedtls_ecp_restart_is_enabled() && \
445 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
446 { \
447 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
448 if( rs_ctx->SUB == NULL ) \
449 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
450 \
451 ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
452 } \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200453} while( 0 )
454
455/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200456#define ECP_RS_LEAVE( SUB ) do { \
457 /* clear our sub-context when not in progress (done or error) */ \
458 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
459 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
460 { \
461 ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
462 mbedtls_free( rs_ctx->SUB ); \
463 rs_ctx->SUB = NULL; \
464 } \
465 \
466 if( rs_ctx != NULL ) \
467 rs_ctx->depth--; \
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200468} while( 0 )
469
470#else /* MBEDTLS_ECP_RESTARTABLE */
471
472#define ECP_RS_ENTER( sub ) (void) rs_ctx;
473#define ECP_RS_LEAVE( sub ) (void) rs_ctx;
474
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200475#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
478 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
479 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
480 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
481 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
482 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
483 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
484 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
485 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
486 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
487 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200488#define ECP_SHORTWEIERSTRASS
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100489#endif
490
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000491#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
492 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200493#define ECP_MONTGOMERY
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100494#endif
495
496/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200497 * List of supported curves:
498 * - internal ID
Christoph M. Wintersteigercb310732019-02-15 15:50:38 +0000499 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200500 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200501 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100502 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100503 * Curves are listed in order: largest curves first, and for a given size,
504 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200505 *
Gilles Peskine1174db52020-02-26 19:52:06 +0100506 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200509{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
511 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200512#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
514 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
517 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
520 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
523 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
526 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
529 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
532 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
535 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
538 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
541 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100542#endif
Christoph M. Wintersteiger2c69d102019-02-22 15:05:02 +0000543#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) && defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteiger86e36c42018-12-06 17:27:31 +0000544 { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200547};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100548
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200549#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
550 sizeof( ecp_supported_curves[0] )
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200553
554/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200555 * List of supported curves and associated info
556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200558{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200559 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200560}
561
562/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100563 * List of supported curves, group ID only
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100566{
567 static int init_done = 0;
568
569 if( ! init_done )
570 {
571 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 for( curve_info = mbedtls_ecp_curve_list();
575 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100576 curve_info++ )
577 {
578 ecp_supported_grp_id[i++] = curve_info->grp_id;
579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100581
582 init_done = 1;
583 }
584
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200585 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100586}
587
588/*
589 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591const 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 +0200592{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 for( curve_info = mbedtls_ecp_curve_list();
596 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200597 curve_info++ )
598 {
599 if( curve_info->grp_id == grp_id )
600 return( curve_info );
601 }
602
603 return( NULL );
604}
605
606/*
607 * Get the curve info from the TLS identifier
608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 for( curve_info = mbedtls_ecp_curve_list();
614 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200615 curve_info++ )
616 {
617 if( curve_info->tls_id == tls_id )
618 return( curve_info );
619 }
620
621 return( NULL );
622}
623
624/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100625 * Get the curve info from the name
626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100628{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100630
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500631 if( name == NULL )
632 return( NULL );
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 for( curve_info = mbedtls_ecp_curve_list();
635 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100636 curve_info++ )
637 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200638 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100639 return( curve_info );
640 }
641
642 return( NULL );
643}
644
645/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100646 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100647 */
Janos Follathdf9295b2019-02-26 12:36:52 +0000648mbedtls_ecp_curve_type mbedtls_ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100649{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100650 if( grp->G.X.p == NULL )
Janos Follathdf9295b2019-02-26 12:36:52 +0000651 return( MBEDTLS_ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100652
653 if( grp->G.Y.p == NULL )
Janos Follathdf9295b2019-02-26 12:36:52 +0000654 return( MBEDTLS_ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100655 else
Janos Follathdf9295b2019-02-26 12:36:52 +0000656 return( MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100657}
658
659/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100660 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100663{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500664 ECP_VALIDATE( pt != NULL );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_mpi_init( &pt->X );
667 mbedtls_mpi_init( &pt->Y );
668 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100669}
670
671/*
672 * Initialize (the components of) a group
673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100675{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500676 ECP_VALIDATE( grp != NULL );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100677
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200678 grp->id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200679 mbedtls_mpi_init( &grp->P );
680 mbedtls_mpi_init( &grp->A );
681 mbedtls_mpi_init( &grp->B );
682 mbedtls_ecp_point_init( &grp->G );
683 mbedtls_mpi_init( &grp->N );
684 grp->pbits = 0;
685 grp->nbits = 0;
686 grp->h = 0;
687 grp->modp = NULL;
688 grp->t_pre = NULL;
689 grp->t_post = NULL;
690 grp->t_data = NULL;
691 grp->T = NULL;
692 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100693}
694
695/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200696 * Initialize (the components of) a key pair
697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200699{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500700 ECP_VALIDATE( key != NULL );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_ecp_group_init( &key->grp );
703 mbedtls_mpi_init( &key->d );
704 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200705}
706
707/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100708 * Unallocate (the components of) a point
709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100711{
712 if( pt == NULL )
713 return;
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_mpi_free( &( pt->X ) );
716 mbedtls_mpi_free( &( pt->Y ) );
717 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100718}
719
720/*
721 * Unallocate (the components of) a group
722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100724{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200725 size_t i;
726
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100727 if( grp == NULL )
728 return;
729
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100730 if( grp->h != 1 )
731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_mpi_free( &grp->P );
733 mbedtls_mpi_free( &grp->A );
734 mbedtls_mpi_free( &grp->B );
735 mbedtls_ecp_point_free( &grp->G );
736 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100737 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200738
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200739 if( grp->T != NULL )
740 {
741 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_ecp_point_free( &grp->T[i] );
743 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200744 }
745
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500746 mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100747}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100748
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100749/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200750 * Unallocate (the components of) a key pair
751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200753{
Paul Bakker66d5d072014-06-17 16:39:18 +0200754 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200755 return;
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_ecp_group_free( &key->grp );
758 mbedtls_mpi_free( &key->d );
759 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200760}
761
762/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200763 * Copy the contents of a point
764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200766{
Janos Follath24eed8d2019-11-22 13:21:35 +0000767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500768 ECP_VALIDATE_RET( P != NULL );
769 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
772 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
773 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200774
775cleanup:
776 return( ret );
777}
778
779/*
780 * Copy the contents of a group object
781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200783{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500784 ECP_VALIDATE_RET( dst != NULL );
785 ECP_VALIDATE_RET( src != NULL );
786
787 return( mbedtls_ecp_group_load( dst, src->id ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200788}
789
790/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100791 * Set point to zero
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100794{
Janos Follath24eed8d2019-11-22 13:21:35 +0000795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500796 ECP_VALIDATE_RET( pt != NULL );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
799 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
800 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100801
802cleanup:
803 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100804}
805
806/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100807 * Tell if a point is zero
808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100810{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500811 ECP_VALIDATE_RET( pt != NULL );
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100814}
815
816/*
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500817 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200818 */
819int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
820 const mbedtls_ecp_point *Q )
821{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500822 ECP_VALIDATE_RET( P != NULL );
823 ECP_VALIDATE_RET( Q != NULL );
824
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200825 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
826 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
827 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
828 {
829 return( 0 );
830 }
831
832 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
833}
834
835/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100836 * Import a non-zero point from ASCII strings
837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100839 const char *x, const char *y )
840{
Janos Follath24eed8d2019-11-22 13:21:35 +0000841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500842 ECP_VALIDATE_RET( P != NULL );
843 ECP_VALIDATE_RET( x != NULL );
844 ECP_VALIDATE_RET( y != NULL );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
847 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
848 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100849
850cleanup:
851 return( ret );
852}
853
854/*
Janos Follath7caf8e42019-02-20 12:00:22 +0000855 * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100856 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500857int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp,
858 const mbedtls_ecp_point *P,
859 int format, size_t *olen,
860 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100861{
Janos Follath28eb06d2019-02-26 10:53:34 +0000862 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100863 size_t plen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500864 ECP_VALIDATE_RET( grp != NULL );
865 ECP_VALIDATE_RET( P != NULL );
866 ECP_VALIDATE_RET( olen != NULL );
867 ECP_VALIDATE_RET( buf != NULL );
868 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
869 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100872
Janos Follath7caf8e42019-02-20 12:00:22 +0000873#if defined(ECP_MONTGOMERY)
Janos Follathdf9295b2019-02-26 12:36:52 +0000874 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100875 {
Janos Follath7caf8e42019-02-20 12:00:22 +0000876 *olen = plen;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100877 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100879
Janos Follath7caf8e42019-02-20 12:00:22 +0000880 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary_le( &P->X, buf, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100881 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000882#endif
883#if defined(ECP_SHORTWEIERSTRASS)
Janos Follathdf9295b2019-02-26 12:36:52 +0000884 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100885 {
Janos Follath7caf8e42019-02-20 12:00:22 +0000886 /*
887 * Common case: P == 0
888 */
889 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
890 {
891 if( buflen < 1 )
892 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100893
Janos Follath7caf8e42019-02-20 12:00:22 +0000894 buf[0] = 0x00;
895 *olen = 1;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100896
Janos Follath7caf8e42019-02-20 12:00:22 +0000897 return( 0 );
898 }
899
900 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
901 {
902 *olen = 2 * plen + 1;
903
904 if( buflen < *olen )
905 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
906
907 buf[0] = 0x04;
908 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
909 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
910 }
911 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
912 {
913 *olen = plen + 1;
914
915 if( buflen < *olen )
916 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
917
918 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
919 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
920 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100921 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000922#endif
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100923
924cleanup:
925 return( ret );
926}
927
928/*
Janos Follath59b813c2019-02-13 10:44:06 +0000929 * Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100930 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500931int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
932 mbedtls_ecp_point *pt,
933 const unsigned char *buf, size_t ilen )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100934{
Janos Follath28eb06d2019-02-26 10:53:34 +0000935 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100936 size_t plen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500937 ECP_VALIDATE_RET( grp != NULL );
938 ECP_VALIDATE_RET( pt != NULL );
939 ECP_VALIDATE_RET( buf != NULL );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100940
Paul Bakker82788fb2014-10-20 13:59:19 +0200941 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100945
Janos Follath59b813c2019-02-13 10:44:06 +0000946#if defined(ECP_MONTGOMERY)
Janos Follathdf9295b2019-02-26 12:36:52 +0000947 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follath59b813c2019-02-13 10:44:06 +0000948 {
949 if( plen != ilen )
950 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100951
Janos Follath59b813c2019-02-13 10:44:06 +0000952 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary_le( &pt->X, buf, plen ) );
953 mbedtls_mpi_free( &pt->Y );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100954
Janos Follath59b813c2019-02-13 10:44:06 +0000955 if( grp->id == MBEDTLS_ECP_DP_CURVE25519 )
Janos Follathffbd7e82019-02-25 11:35:20 +0000956 /* Set most significant bit to 0 as prescribed in RFC7748 §5 */
Janos Follath59b813c2019-02-13 10:44:06 +0000957 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &pt->X, plen * 8 - 1, 0 ) );
Janos Follath28eb06d2019-02-26 10:53:34 +0000958
959 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Janos Follath59b813c2019-02-13 10:44:06 +0000960 }
961#endif
962#if defined(ECP_SHORTWEIERSTRASS)
Janos Follathdf9295b2019-02-26 12:36:52 +0000963 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Janos Follath59b813c2019-02-13 10:44:06 +0000964 {
965 if( buf[0] == 0x00 )
966 {
967 if( ilen == 1 )
968 return( mbedtls_ecp_set_zero( pt ) );
969 else
970 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
971 }
972
973 if( buf[0] != 0x04 )
974 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
975
976 if( ilen != 2 * plen + 1 )
977 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
978
979 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
980 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y,
981 buf + 1 + plen, plen ) );
Janos Follath28eb06d2019-02-26 10:53:34 +0000982 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Janos Follath59b813c2019-02-13 10:44:06 +0000983 }
984#endif
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100985
986cleanup:
987 return( ret );
988}
989
990/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100991 * Import a point from a TLS ECPoint record (RFC 4492)
992 * struct {
993 * opaque point <1..2^8-1>;
994 * } ECPoint;
995 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500996int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
997 mbedtls_ecp_point *pt,
998 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100999{
1000 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001001 const unsigned char *buf_start;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001002 ECP_VALIDATE_RET( grp != NULL );
1003 ECP_VALIDATE_RET( pt != NULL );
1004 ECP_VALIDATE_RET( buf != NULL );
1005 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001006
1007 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +02001008 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001009 */
1010 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001012
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001013 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001014 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001016
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001017 /*
1018 * Save buffer start for read_binary and update buf
1019 */
1020 buf_start = *buf;
1021 *buf += data_len;
1022
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001023 return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001024}
1025
1026/*
1027 * Export a point as a TLS ECPoint record (RFC 4492)
1028 * struct {
1029 * opaque point <1..2^8-1>;
1030 * } ECPoint;
1031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001033 int format, size_t *olen,
1034 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001035{
Janos Follath24eed8d2019-11-22 13:21:35 +00001036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001037 ECP_VALIDATE_RET( grp != NULL );
1038 ECP_VALIDATE_RET( pt != NULL );
1039 ECP_VALIDATE_RET( olen != NULL );
1040 ECP_VALIDATE_RET( buf != NULL );
1041 ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1042 format == MBEDTLS_ECP_PF_COMPRESSED );
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001043
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001044 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001045 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001046 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001047 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001051 olen, buf + 1, blen - 1) ) != 0 )
1052 return( ret );
1053
1054 /*
1055 * write length to the first byte and update total length
1056 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001057 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001058 ++*olen;
1059
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001060 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001061}
1062
1063/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001064 * Set a group from an ECParameters record (RFC 4492)
1065 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001066int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
1067 const unsigned char **buf, size_t len )
1068{
Janos Follath24eed8d2019-11-22 13:21:35 +00001069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001070 mbedtls_ecp_group_id grp_id;
1071 ECP_VALIDATE_RET( grp != NULL );
1072 ECP_VALIDATE_RET( buf != NULL );
1073 ECP_VALIDATE_RET( *buf != NULL );
1074
1075 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 )
1076 return( ret );
1077
1078 return( mbedtls_ecp_group_load( grp, grp_id ) );
1079}
1080
1081/*
1082 * Read a group id from an ECParameters record (RFC 4492) and convert it to
1083 * mbedtls_ecp_group_id.
1084 */
1085int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
1086 const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001087{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001088 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001090 ECP_VALIDATE_RET( grp != NULL );
1091 ECP_VALIDATE_RET( buf != NULL );
1092 ECP_VALIDATE_RET( *buf != NULL );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001093
1094 /*
1095 * We expect at least three bytes (see below)
1096 */
1097 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001099
1100 /*
1101 * First byte is curve_type; only named_curve is handled
1102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
1104 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001105
1106 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001107 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001108 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001109 tls_id = *(*buf)++;
1110 tls_id <<= 8;
1111 tls_id |= *(*buf)++;
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
1114 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001115
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001116 *grp = curve_info->grp_id;
1117
1118 return( 0 );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001119}
1120
1121/*
1122 * Write the ECParameters record corresponding to a group (RFC 4492)
1123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001125 unsigned char *buf, size_t blen )
1126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001128 ECP_VALIDATE_RET( grp != NULL );
1129 ECP_VALIDATE_RET( buf != NULL );
1130 ECP_VALIDATE_RET( olen != NULL );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
1133 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001134
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001135 /*
1136 * We are going to write 3 bytes (see below)
1137 */
1138 *olen = 3;
1139 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001141
1142 /*
1143 * First byte is curve_type, always named_curve
1144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001146
1147 /*
1148 * Next two bytes are the namedcurve value
1149 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001150 buf[0] = curve_info->tls_id >> 8;
1151 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001152
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001153 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001154}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001155
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001156/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1158 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001159 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 * 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 +02001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001163{
Janos Follath24eed8d2019-11-22 13:21:35 +00001164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001165
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001166 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001168
1169 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001171 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001174 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001177
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001178 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
1180 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001183 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001185
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001186cleanup:
1187 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001188}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001189
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001190/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001191 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001192 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001193 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 * 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 +01001195 * bring the result back to this range.
1196 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001197 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001198 */
1199
1200/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 * 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 +01001202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001204#define INC_MUL_COUNT mul_count++;
1205#else
1206#define INC_MUL_COUNT
1207#endif
1208
Hanno Becker1eeca412018-10-15 12:01:35 +01001209#define MOD_MUL( N ) \
1210 do \
1211 { \
1212 MBEDTLS_MPI_CHK( ecp_modp( &(N), grp ) ); \
1213 INC_MUL_COUNT \
1214 } while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001215
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001216static inline int mbedtls_mpi_mul_mod( const mbedtls_ecp_group *grp,
1217 mbedtls_mpi *X,
1218 const mbedtls_mpi *A,
1219 const mbedtls_mpi *B )
1220{
Janos Follath24eed8d2019-11-22 13:21:35 +00001221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( X, A, B ) );
1223 MOD_MUL( *X );
1224cleanup:
1225 return( ret );
1226}
1227
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001228/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001230 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001231 */
Hanno Becker1eeca412018-10-15 12:01:35 +01001232#define MOD_SUB( N ) \
1233 while( (N).s < 0 && mbedtls_mpi_cmp_int( &(N), 0 ) != 0 ) \
1234 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001235
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001236static inline int mbedtls_mpi_sub_mod( const mbedtls_ecp_group *grp,
1237 mbedtls_mpi *X,
1238 const mbedtls_mpi *A,
1239 const mbedtls_mpi *B )
1240{
Janos Follath24eed8d2019-11-22 13:21:35 +00001241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001242 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( X, A, B ) );
1243 MOD_SUB( *X );
1244cleanup:
1245 return( ret );
1246}
1247
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001248/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 * 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 +02001250 * We known P, N and the result are positive, so sub_abs is correct, and
1251 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001252 */
Hanno Becker1eeca412018-10-15 12:01:35 +01001253#define MOD_ADD( N ) \
1254 while( mbedtls_mpi_cmp_mpi( &(N), &grp->P ) >= 0 ) \
1255 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &(N), &(N), &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001256
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001257static inline int mbedtls_mpi_add_mod( const mbedtls_ecp_group *grp,
1258 mbedtls_mpi *X,
1259 const mbedtls_mpi *A,
1260 const mbedtls_mpi *B )
1261{
Janos Follath24eed8d2019-11-22 13:21:35 +00001262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001263 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, A, B ) );
1264 MOD_ADD( *X );
1265cleanup:
1266 return( ret );
1267}
1268
1269static inline int mbedtls_mpi_shift_l_mod( const mbedtls_ecp_group *grp,
1270 mbedtls_mpi *X,
1271 size_t count )
1272{
Janos Follath24eed8d2019-11-22 13:21:35 +00001273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001274 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, count ) );
1275 MOD_ADD( *X );
1276cleanup:
1277 return( ret );
1278}
1279
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001280#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001281/*
1282 * For curves in short Weierstrass form, we do all the internal operations in
1283 * Jacobian coordinates.
1284 *
1285 * For multiplication, we'll use a comb method with coutermeasueres against
1286 * SPA, hence timing attacks.
1287 */
1288
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001289/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001290 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001291 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001294{
Janos Follath24eed8d2019-11-22 13:21:35 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001299 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001300
Janos Follathb0697532016-08-18 12:38:46 +01001301#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001302 if( mbedtls_internal_ecp_grp_capable( grp ) )
1303 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +01001304#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001307
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001308 /*
1309 * X = X / Z^2 mod p
1310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001312 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ZZi, &Zi, &Zi ) );
1313 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->X, &pt->X, &ZZi ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001314
1315 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001316 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001317 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001318 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &ZZi ) );
1319 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &Zi ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001320
1321 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001322 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001325
1326cleanup:
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001329
1330 return( ret );
1331}
1332
1333/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001334 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001335 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001336 * (See for example Cohen's "A Course in Computational Algebraic Number
1337 * Theory", Algorithm 10.3.4.)
1338 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001339 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001340 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001341 *
1342 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001345 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001346{
Janos Follath24eed8d2019-11-22 13:21:35 +00001347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001348 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_mpi *c, u, Zi, ZZi;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001350
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001351 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001352 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001353
Janos Follathb0697532016-08-18 12:38:46 +01001354#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001355 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001356 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +01001357#endif
1358
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001359 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001360 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001361
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001362 for( i = 0; i < T_size; i++ )
1363 mbedtls_mpi_init( &c[i] );
1364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001366
1367 /*
1368 * c[i] = Z_0 * ... * Z_i
1369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001371 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001372 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001373 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001374 }
1375
1376 /*
1377 * u = 1 / (Z_0 * ... * Z_n) mod P
1378 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001379 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001380
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001381 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001382 {
1383 /*
1384 * Zi = 1 / Z_i mod p
1385 * u = 1 / (Z_0 * ... * Z_i) mod P
1386 */
1387 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001389 }
1390 else
1391 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001392 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &Zi, &u, &c[i-1] ) );
1393 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &u, &u, &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001394 }
1395
1396 /*
1397 * proceed as in normalize()
1398 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001399 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ZZi, &Zi, &Zi ) );
1400 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->X, &T[i]->X, &ZZi ) );
1401 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->Y, &T[i]->Y, &ZZi ) );
1402 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T[i]->Y, &T[i]->Y, &Zi ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001403
1404 /*
1405 * Post-precessing: reclaim some memory by shrinking coordinates
1406 * - not storing Z (always 1)
1407 * - shrinking other coordinates, but still keeping the same number of
1408 * limbs as P, as otherwise it will too likely be regrown too fast.
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1411 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1412 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001413
1414 if( i == 0 )
1415 break;
1416 }
1417
1418cleanup:
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001421 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 mbedtls_mpi_free( &c[i] );
1423 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001424
1425 return( ret );
1426}
1427
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001428/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001429 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1430 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1433 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001434 unsigned char inv )
1435{
Janos Follath24eed8d2019-11-22 13:21:35 +00001436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001437 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001441
1442 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1444 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1445 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001446
1447cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001449
1450 return( ret );
1451}
1452
1453/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001454 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001455 *
Peter Dettmance661b22015-02-07 14:43:51 +07001456 * 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 +01001457 *
Peter Dettmance661b22015-02-07 14:43:51 +07001458 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1459 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1460 *
1461 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1462 *
1463 * Cost: 1D := 3M + 4S (A == 0)
1464 * 4M + 4S (A == -3)
1465 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1468 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001469{
Janos Follath24eed8d2019-11-22 13:21:35 +00001470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001471 mbedtls_mpi M, S, T, U;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001474 dbl_count++;
1475#endif
1476
Janos Follathb0697532016-08-18 12:38:46 +01001477#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001478 if( mbedtls_internal_ecp_grp_capable( grp ) )
1479 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001480#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001481
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001482 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 +01001483
1484 /* Special case for A = -3 */
1485 if( grp->A.p == NULL )
1486 {
Peter Dettmance661b22015-02-07 14:43:51 +07001487 /* M = 3(X + Z^2)(X - Z^2) */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001488 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->Z, &P->Z ) );
1489 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &T, &P->X, &S ) );
1490 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &U, &P->X, &S ) );
1491 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &T, &U ) );
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001492 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001493 }
1494 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001495 {
Peter Dettmance661b22015-02-07 14:43:51 +07001496 /* M = 3.X^2 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001497 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->X, &P->X ) );
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001498 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001499
1500 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001501 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001502 {
1503 /* M += A.Z^4 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001504 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->Z, &P->Z ) );
1505 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &S, &S ) );
1506 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &T, &grp->A ) );
1507 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &M, &M, &S ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001508 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001509 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001510
Peter Dettmance661b22015-02-07 14:43:51 +07001511 /* S = 4.X.Y^2 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001512 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &P->Y, &P->Y ) );
1513 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &T, 1 ) );
1514 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &P->X, &T ) );
1515 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &S, 1 ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001516
Peter Dettmance661b22015-02-07 14:43:51 +07001517 /* U = 8.Y^4 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001518 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &U, &T, &T ) );
1519 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &U, 1 ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001520
1521 /* T = M^2 - 2.S */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001522 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T, &M, &M ) );
1523 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T, &T, &S ) );
1524 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T, &T, &S ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001525
1526 /* S = M(S - T) - U */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001527 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S, &S, &T ) );
1528 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S, &S, &M ) );
1529 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S, &S, &U ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001530
1531 /* U = 2.Y.Z */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001532 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &U, &P->Y, &P->Z ) );
1533 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &U, 1 ) );
Peter Dettmance661b22015-02-07 14:43:51 +07001534
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001535 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1536 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1537 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001538
1539cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001540 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 +02001541
1542 return( ret );
1543}
1544
1545/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001546 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001547 *
1548 * The coordinates of Q must be normalized (= affine),
1549 * but those of P don't need to. R is not normalized.
1550 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001551 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001552 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001553 * - at each step, P, Q and R are multiples of the base point, the factor
1554 * being less than its order, so none of them is zero;
1555 * - Q is an odd multiple of the base point, P an even multiple,
1556 * due to the choice of precomputed points in the modified comb method.
1557 * So branches for these cases do not leak secret information.
1558 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001559 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1560 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001561 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1564 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001565{
Janos Follath24eed8d2019-11-22 13:21:35 +00001566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001570 add_count++;
1571#endif
1572
Janos Follathb0697532016-08-18 12:38:46 +01001573#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001574 if( mbedtls_internal_ecp_grp_capable( grp ) )
1575 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001576#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001577
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001578 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001579 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
1582 return( mbedtls_ecp_copy( R, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
1585 return( mbedtls_ecp_copy( R, P ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001586
1587 /*
1588 * Make sure Q coordinates are normalized
1589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
1591 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
1594 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001595
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001596 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T1, &P->Z, &P->Z ) );
1597 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T2, &T1, &P->Z ) );
1598 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T1, &T1, &Q->X ) );
1599 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T2, &T2, &Q->Y ) );
1600 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T1, &T1, &P->X ) );
1601 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T2, &T2, &P->Y ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001602
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001603 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001607 {
1608 ret = ecp_double_jac( grp, R, P );
1609 goto cleanup;
1610 }
1611 else
1612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 ret = mbedtls_ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001614 goto cleanup;
1615 }
1616 }
1617
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001618 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &Z, &P->Z, &T1 ) );
1619 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T1, &T1 ) );
1620 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T4, &T3, &T1 ) );
1621 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T3, &P->X ) );
1622 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &T3 ) );
1623 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l_mod( grp, &T1, 1 ) );
1624 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &X, &T2, &T2 ) );
1625 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &X, &X, &T1 ) );
1626 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &X, &X, &T4 ) );
1627 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &T3, &T3, &X ) );
1628 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T3, &T3, &T2 ) );
1629 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &T4, &T4, &P->Y ) );
1630 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &Y, &T3, &T4 ) );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1633 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1634 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001635
1636cleanup:
1637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1639 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001640
1641 return( ret );
1642}
1643
1644/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001645 * Randomize jacobian coordinates:
1646 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001647 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001648 *
1649 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001652 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1653{
Janos Follath24eed8d2019-11-22 13:21:35 +00001654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_mpi l, ll;
Janos Follathb0697532016-08-18 12:38:46 +01001656 size_t p_size;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001657 int count = 0;
1658
Janos Follathb0697532016-08-18 12:38:46 +01001659#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001660 if( mbedtls_internal_ecp_grp_capable( grp ) )
1661 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001662#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001663
1664 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001666
1667 /* Generate l such that 1 < l < p */
1668 do
1669 {
Ron Eldorca6ff582017-01-12 14:50:50 +02001670 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1673 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001674
1675 if( count++ > 10 )
Jonas6692a062020-05-08 16:57:18 +09001676 {
1677 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
1678 goto cleanup;
1679 }
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001682
1683 /* Z = l * Z */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001684 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Z, &pt->Z, &l ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001685
1686 /* X = l^2 * X */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001687 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ll, &l, &l ) );
1688 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->X, &pt->X, &ll ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001689
1690 /* Y = l^3 * Y */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001691 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &ll, &ll, &l ) );
1692 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &pt->Y, &pt->Y, &ll ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001693
1694cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001696
1697 return( ret );
1698}
1699
1700/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001701 * Check and define parameters used by the comb method (see below for details)
1702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1704#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001705#endif
1706
1707/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001709
1710/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001712
1713/*
1714 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001715 *
1716 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001717 * modified version that provides resistance to SPA by avoiding zero
1718 * digits in the representation as in [3]. We modify the method further by
1719 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001720 * representation uses one more K_i, due to carries, but saves on the size of
1721 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001722 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001723 * Summary of the comb method and its modifications:
1724 *
1725 * - The goal is to compute m*P for some w*d-bit integer m.
1726 *
1727 * - The basic comb method splits m into the w-bit integers
1728 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1729 * index has residue i modulo d, and computes m * P as
1730 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1731 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1732 *
1733 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1734 * .. + 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]] ..,
1735 * thereby successively converting it into a form where all summands
1736 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1737 *
1738 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1739 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1740 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1741 * Performing and iterating this procedure for those x[i] that are even
1742 * (keeping track of carry), we can transform the original sum into one of the form
1743 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1744 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1745 * which is why we are only computing half of it in the first place in
1746 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1747 *
1748 * - For the sake of compactness, only the seven low-order bits of x[i]
1749 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001750 * 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 +02001751 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001752 *
1753 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001754 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001755 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001757 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1758 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001759 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001760static void ecp_comb_recode_core( unsigned char x[], size_t d,
1761 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001762{
1763 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001764 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001765
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001766 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001767
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001768 /* First get the classical comb values (except for x_d = 0) */
1769 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001770 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001772
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001773 /* Now make sure x_1 .. x_d are odd */
1774 c = 0;
1775 for( i = 1; i <= d; i++ )
1776 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001777 /* Add carry and update it */
1778 cc = x[i] & c;
1779 x[i] = x[i] ^ c;
1780 c = cc;
1781
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001782 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001783 adjust = 1 - ( x[i] & 0x01 );
1784 c |= x[i] & ( x[i-1] * adjust );
1785 x[i] = x[i] ^ ( x[i-1] * adjust );
1786 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001787 }
1788}
1789
1790/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001791 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001792 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001793 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001794 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001795 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1796 * 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 +01001797 *
1798 * 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 +02001799 *
1800 * Note: Even comb values (those where P would be omitted from the
1801 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001802 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001803 *
1804 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001805 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001806 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1807 * (3) [add] Computation of all T[i]
1808 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001809 *
1810 * Step 1 can be interrupted but not the others; together with the final
1811 * coordinate normalization they are the largest steps done at once, depending
1812 * on the window size. Here are operation counts for P-256:
1813 *
1814 * step (2) (3) (4)
1815 * w = 5 142 165 208
1816 * w = 4 136 77 160
1817 * w = 3 130 33 136
1818 * w = 2 124 11 124
1819 *
1820 * So if ECC operations are blocking for too long even with a low max_ops
1821 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1822 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1825 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001826 unsigned char w, size_t d,
1827 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001828{
Janos Follath24eed8d2019-11-22 13:21:35 +00001829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001830 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001831 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001832 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001834
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001835#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001836 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001837 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001838 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
1839 goto dbl;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001840 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001841 goto norm_dbl;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001842 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
1843 goto add;
1844 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
1845 goto norm_add;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001846 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001847#else
1848 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001849#endif
1850
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001851#if defined(MBEDTLS_ECP_RESTARTABLE)
1852 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1853 {
1854 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1855
1856 /* initial state for the loop */
1857 rs_ctx->rsm->i = 0;
1858 }
1859
1860dbl:
1861#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001862 /*
1863 * Set T[0] = P and
1864 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001867
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 && rs_ctx->rsm->i != 0 )
1870 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001871 else
1872#endif
1873 j = 0;
1874
1875 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001876 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001877 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001878
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001879 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001880 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001881
1882 if( j % d == 0 )
1883 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1884
1885 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001886 }
1887
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001888#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001889 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1890 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1891
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001892norm_dbl:
1893#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001894 /*
1895 * Normalize current elements in T. As T has holes,
1896 * use an auxiliary array of pointers to elements in T.
1897 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001898 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001899 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001900 TT[j++] = T + i;
1901
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001902 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001903
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001904 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001905
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001906#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001907 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1908 rs_ctx->rsm->state = ecp_rsm_pre_add;
1909
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001910add:
1911#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001912 /*
1913 * Compute the remaining ones using the minimal number of additions
1914 * Be careful to update T[2^l] only after using it!
1915 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001916 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001917
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001918 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001919 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001920 j = i;
1921 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001923 }
1924
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001925#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001926 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1927 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1928
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001929norm_add:
1930#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001931 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001932 * Normalize final elements in T. Even though there are no holes now, we
1933 * still need the auxiliary array for homogeneity with the previous
1934 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001935 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001936 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001937 TT[j] = T + j + 1;
1938
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001939 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001940
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001941 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001942
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001943cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001944#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001945 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1946 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001947 {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001948 if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001949 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001950 }
1951#endif
Janos Follathb0697532016-08-18 12:38:46 +01001952
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001953 return( ret );
1954}
1955
1956/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001957 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001958 *
1959 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001962 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001963 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001964{
Janos Follath24eed8d2019-11-22 13:21:35 +00001965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001966 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001967
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001968 /* Ignore the "sign" bit and scale down */
1969 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001970
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001971 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001972 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1975 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001976 }
1977
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001978 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001980
1981cleanup:
1982 return( ret );
1983}
1984
1985/*
1986 * Core multiplication algorithm for the (modified) comb method.
1987 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001988 *
1989 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001992 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001993 const unsigned char x[], size_t d,
1994 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001995 void *p_rng,
1996 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001997{
Janos Follath24eed8d2019-11-22 13:21:35 +00001998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002000 size_t i;
2001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002003
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002004#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002005 (void) rs_ctx;
2006#endif
2007
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002008#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002009 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
2010 rs_ctx->rsm->state != ecp_rsm_comb_core )
2011 {
2012 rs_ctx->rsm->i = 0;
2013 rs_ctx->rsm->state = ecp_rsm_comb_core;
2014 }
2015
2016 /* new 'if' instead of nested for the sake of the 'else' branch */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002017 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002018 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002019 /* restore current index (R already pointing to rs_ctx->rsm->R) */
2020 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002021 }
2022 else
2023#endif
2024 {
2025 /* Start with a non-zero point and randomize its coordinates */
2026 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002027 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002028 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
2029 if( f_rng != 0 )
2030 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
2031 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002032
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002033 while( i != 0 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002034 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002035 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002036 --i;
2037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002039 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002041 }
2042
2043cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002046
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002047#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002048 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
2049 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002050 {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002051 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002052 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002053 }
2054#endif
2055
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002056 return( ret );
2057}
2058
2059/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002060 * Recode the scalar to get constant-time comb multiplication
2061 *
2062 * As the actual scalar recoding needs an odd scalar as a starting point,
2063 * this wrapper ensures that by replacing m by N - m if necessary, and
2064 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002065 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002066 * This works because we only support large prime order for Short Weierstrass
2067 * curves, so N is always odd hence either m or N - m is.
2068 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002069 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002070 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002071static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
2072 const mbedtls_mpi *m,
2073 unsigned char k[COMB_MAX_D + 1],
2074 size_t d,
2075 unsigned char w,
2076 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002077{
Janos Follath24eed8d2019-11-22 13:21:35 +00002078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002079 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002080
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002081 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002082 mbedtls_mpi_init( &mm );
2083
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002084 /* N is always odd (see above), just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002085 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
2086 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2087
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002088 /* do we need the parity trick? */
2089 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
2090
2091 /* execute parity fix in constant time */
2092 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002093 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002094 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
2095
2096 /* actual scalar recoding */
2097 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002098
2099cleanup:
2100 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002101 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002102
2103 return( ret );
2104}
2105
2106/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002107 * Perform comb multiplication (for short Weierstrass curves)
2108 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002109 *
2110 * Scalar recoding may use a parity trick that makes us compute -m * P,
2111 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002112 */
2113static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
2114 mbedtls_ecp_point *R,
2115 const mbedtls_mpi *m,
2116 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002117 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002118 unsigned char w,
2119 size_t d,
2120 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002121 void *p_rng,
2122 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002123{
Janos Follath24eed8d2019-11-22 13:21:35 +00002124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002125 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002126 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002127 mbedtls_ecp_point *RR = R;
2128
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002129#if defined(MBEDTLS_ECP_RESTARTABLE)
2130 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2131 {
2132 RR = &rs_ctx->rsm->R;
2133
2134 if( rs_ctx->rsm->state == ecp_rsm_final_norm )
2135 goto final_norm;
2136 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002137#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002138
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002139 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
2140 &parity_trick ) );
2141 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
2142 f_rng, p_rng, rs_ctx ) );
2143 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
2144
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002145#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002146 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002147 rs_ctx->rsm->state = ecp_rsm_final_norm;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002148
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002149final_norm:
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002150#endif
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002151 /*
2152 * Knowledge of the jacobian coordinates may leak the last few bits of the
2153 * scalar [1], and since our MPI implementation isn't constant-flow,
2154 * inversion (used for coordinate normalization) may leak the full value
2155 * of its input via side-channels [2].
2156 *
2157 * [1] https://eprint.iacr.org/2003/191
2158 * [2] https://eprint.iacr.org/2020/055
2159 *
2160 * Avoid the leak by randomizing coordinates before we normalize them.
2161 */
2162 if( f_rng != 0 )
2163 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
2164
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002165 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002166 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
2167
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002168#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +02002169 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2170 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002171#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002172
2173cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002174 return( ret );
2175}
2176
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002177/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002178 * Pick window size based on curve size and whether we optimize for base point
2179 */
2180static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
2181 unsigned char p_eq_g )
2182{
2183 unsigned char w;
2184
2185 /*
2186 * Minimize the number of multiplications, that is minimize
2187 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2188 * (see costs of the various parts, with 1S = 1M)
2189 */
2190 w = grp->nbits >= 384 ? 5 : 4;
2191
2192 /*
2193 * If P == G, pre-compute a bit more, since this may be re-used later.
2194 * Just adding one avoids upping the cost of the first mul too much,
2195 * and the memory cost too.
2196 */
2197 if( p_eq_g )
2198 w++;
2199
2200 /*
2201 * Make sure w is within bounds.
2202 * (The last test is useful only for very small curves in the test suite.)
2203 */
k-stachowiak653a4a22019-07-03 14:31:09 +02002204#if( MBEDTLS_ECP_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002205 if( w > MBEDTLS_ECP_WINDOW_SIZE )
2206 w = MBEDTLS_ECP_WINDOW_SIZE;
k-stachowiak653a4a22019-07-03 14:31:09 +02002207#endif
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002208 if( w >= grp->nbits )
2209 w = 2;
2210
2211 return( w );
2212}
2213
2214/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002215 * Multiplication using the comb method - for curves in short Weierstrass form
2216 *
2217 * This function is mainly responsible for administrative work:
2218 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002219 * - managing the table of precomputed points (passed between the below two
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002220 * functions): allocation, computation, ownership tranfer, freeing.
2221 *
2222 * It delegates the actual arithmetic work to:
2223 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2224 *
2225 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2228 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002229 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002230 void *p_rng,
2231 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002232{
Janos Follath24eed8d2019-11-22 13:21:35 +00002233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002234 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002235 size_t d;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002236 unsigned char T_size = 0, T_ok = 0;
2237 mbedtls_ecp_point *T = NULL;
2238#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2239 ecp_drbg_context drbg_ctx;
2240
2241 ecp_drbg_init( &drbg_ctx );
2242#endif
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002243
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002244 ECP_RS_ENTER( rsm );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002245
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002246#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2247 if( f_rng == NULL )
2248 {
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002249 /* Adjust pointers */
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002250 f_rng = &ecp_drbg_random;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002251#if defined(MBEDTLS_ECP_RESTARTABLE)
2252 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2253 p_rng = &rs_ctx->rsm->drbg_ctx;
2254 else
2255#endif
2256 p_rng = &drbg_ctx;
2257
2258 /* Initialize internal DRBG if necessary */
2259#if defined(MBEDTLS_ECP_RESTARTABLE)
2260 if( rs_ctx == NULL || rs_ctx->rsm == NULL ||
2261 rs_ctx->rsm->drbg_seeded == 0 )
2262#endif
2263 {
2264 MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m ) );
2265 }
2266#if defined(MBEDTLS_ECP_RESTARTABLE)
2267 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
2268 rs_ctx->rsm->drbg_seeded = 1;
2269#endif
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002270 }
2271#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2272
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002273 /* Is P the base point ? */
2274#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2275 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
2276 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002277#else
2278 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002279#endif
2280
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002281 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002282 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002283 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002284 d = ( grp->nbits + w - 1 ) / w;
2285
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002286 /* Pre-computed table: do we have it already for the base point? */
2287 if( p_eq_g && grp->T != NULL )
2288 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002289 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002290 T = grp->T;
2291 T_ok = 1;
2292 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002293 else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002294#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002295 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002296 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002297 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002298 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002299 T = rs_ctx->rsm->T;
2300 rs_ctx->rsm->T = NULL;
2301 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002302
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002303 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002304 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002305 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002306 else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002307#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002308 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002309 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002310 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002311 if( T == NULL )
2312 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002313 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002314 goto cleanup;
2315 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002316
2317 for( i = 0; i < T_size; i++ )
2318 mbedtls_ecp_point_init( &T[i] );
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002319
2320 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002321 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002322
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002323 /* Compute table (or finish computing it) if not done already */
2324 if( !T_ok )
2325 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002326 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002327
2328 if( p_eq_g )
2329 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002330 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002331 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002332 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002333 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002334 }
2335 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002336
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002337 /* Actual comb multiplication using precomputed points */
2338 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002339 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002340 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002341
2342cleanup:
2343
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002344#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2345 ecp_drbg_free( &drbg_ctx );
2346#endif
2347
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002348 /* does T belong to the group? */
2349 if( T == grp->T )
2350 T = NULL;
2351
2352 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002353#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002354 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 +01002355 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002356 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002357 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002358 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002359 T = NULL;
2360 }
2361#endif
2362
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002363 /* did T belong to us? then let's destroy it! */
2364 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002365 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002366 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 mbedtls_ecp_point_free( &T[i] );
2368 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002369 }
2370
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002371 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002372#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002373 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2374#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002375 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01002376 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002378
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002379 ECP_RS_LEAVE( rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002380
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002381 return( ret );
2382}
2383
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002384#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002385
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002386#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002387/*
2388 * For Montgomery curves, we do all the internal arithmetic in projective
2389 * coordinates. Import/export of points uses only the x coordinates, which is
2390 * internaly represented as X / Z.
2391 *
2392 * For scalar multiplication, we'll use a Montgomery ladder.
2393 */
2394
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002395/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002396 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2397 * Cost: 1M + 1I
2398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002400{
Janos Follath24eed8d2019-11-22 13:21:35 +00002401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002402
Janos Follathb0697532016-08-18 12:38:46 +01002403#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002404 if( mbedtls_internal_ecp_grp_capable( grp ) )
2405 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01002406#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002409 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->X, &P->X, &P->Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002411
2412cleanup:
2413 return( ret );
2414}
2415
2416/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002417 * Randomize projective x/z coordinates:
2418 * (X, Z) -> (l X, l Z) for random l
2419 * This is sort of the reverse operation of ecp_normalize_mxz().
2420 *
2421 * This countermeasure was first suggested in [2].
2422 * Cost: 2M
2423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002425 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2426{
Janos Follath24eed8d2019-11-22 13:21:35 +00002427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 mbedtls_mpi l;
Janos Follathb0697532016-08-18 12:38:46 +01002429 size_t p_size;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002430 int count = 0;
2431
Janos Follathb0697532016-08-18 12:38:46 +01002432#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002433 if( mbedtls_internal_ecp_grp_capable( grp ) )
2434 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Janos Follath372697b2016-10-28 16:53:11 +01002435#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002436
2437 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002439
2440 /* Generate l such that 1 < l < p */
2441 do
2442 {
Ron Eldorca6ff582017-01-12 14:50:50 +02002443 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
2446 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002447
2448 if( count++ > 10 )
Jonas6692a062020-05-08 16:57:18 +09002449 {
2450 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
2451 goto cleanup;
2452 }
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002455
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002456 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->X, &P->X, &l ) );
2457 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &P->Z, &P->Z, &l ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002458
2459cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002461
2462 return( ret );
2463}
2464
2465/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002466 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2467 * for Montgomery curves in x/z coordinates.
2468 *
2469 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2470 * with
2471 * d = X1
2472 * P = (X2, Z2)
2473 * Q = (X3, Z3)
2474 * R = (X4, Z4)
2475 * S = (X5, Z5)
2476 * and eliminating temporary variables tO, ..., t4.
2477 *
2478 * Cost: 5M + 4S
2479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2481 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2482 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2483 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002484{
Janos Follath24eed8d2019-11-22 13:21:35 +00002485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002487
Janos Follathb0697532016-08-18 12:38:46 +01002488#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002489 if( mbedtls_internal_ecp_grp_capable( grp ) )
2490 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002491#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2494 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2495 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002496
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002497 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &A, &P->X, &P->Z ) );
2498 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &AA, &A, &A ) );
2499 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &B, &P->X, &P->Z ) );
2500 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &BB, &B, &B ) );
2501 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &E, &AA, &BB ) );
2502 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &C, &Q->X, &Q->Z ) );
2503 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &D, &Q->X, &Q->Z ) );
2504 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &DA, &D, &A ) );
2505 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &CB, &C, &B ) );
Aurelien Jarno66deb382020-04-20 21:36:48 +02002506 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &S->X, &DA, &CB ) );
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002507 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->X, &S->X, &S->X ) );
2508 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mod( grp, &S->Z, &DA, &CB ) );
2509 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->Z, &S->Z, &S->Z ) );
2510 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &S->Z, d, &S->Z ) );
2511 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->X, &AA, &BB ) );
2512 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->Z, &grp->A, &E ) );
2513 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &R->Z, &BB, &R->Z ) );
2514 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &R->Z, &E, &R->Z ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002515
2516cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2518 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2519 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002520
2521 return( ret );
2522}
2523
2524/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002525 * Multiplication with Montgomery ladder in x/z coordinates,
2526 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2529 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002530 int (*f_rng)(void *, unsigned char *, size_t),
2531 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002532{
Janos Follath24eed8d2019-11-22 13:21:35 +00002533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002534 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002535 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 mbedtls_ecp_point RP;
2537 mbedtls_mpi PX;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002538#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2539 ecp_drbg_context drbg_ctx;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002540
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002541 ecp_drbg_init( &drbg_ctx );
2542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002544
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002545#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2546 if( f_rng == NULL )
2547 {
2548 MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) );
2549 f_rng = &ecp_drbg_random;
2550 p_rng = &drbg_ctx;
2551 }
2552#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2553
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002554 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2556 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002557
2558 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2560 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2561 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002562
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002563 /* RP.X might be sligtly larger than P, so reduce it */
2564 MOD_ADD( RP.X );
2565
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002566 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002567 if( f_rng != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002569
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002570 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002571 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002572 while( i-- > 0 )
2573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002575 /*
2576 * if (b) R = 2R + P else R = 2R,
2577 * which is:
2578 * if (b) double_add( RP, R, RP, R )
2579 * else double_add( R, RP, R, RP )
2580 * but using safe conditional swaps to avoid leaks
2581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2583 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2584 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2585 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2586 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002587 }
2588
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002589 /*
2590 * Knowledge of the projective coordinates may leak the last few bits of the
2591 * scalar [1], and since our MPI implementation isn't constant-flow,
2592 * inversion (used for coordinate normalization) may leak the full value
2593 * of its input via side-channels [2].
2594 *
2595 * [1] https://eprint.iacr.org/2003/191
2596 * [2] https://eprint.iacr.org/2020/055
2597 *
2598 * Avoid the leak by randomizing coordinates before we normalize them.
2599 */
2600 if( f_rng != NULL )
2601 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
2602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002604
2605cleanup:
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002606#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2607 ecp_drbg_free( &drbg_ctx );
2608#endif
2609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002611
2612 return( ret );
2613}
2614
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002615#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002616
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002617/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002618 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002619 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002620int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002622 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2623 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002624{
Janos Follathb0697532016-08-18 12:38:46 +01002625 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002626#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2627 char is_grp_capable = 0;
2628#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002629 ECP_VALIDATE_RET( grp != NULL );
2630 ECP_VALIDATE_RET( R != NULL );
2631 ECP_VALIDATE_RET( m != NULL );
2632 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002633
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002634#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002635 /* reset ops count for this call if top-level */
2636 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2637 rs_ctx->ops_done = 0;
2638#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002639
Janos Follathc44ab972016-11-18 16:38:23 +00002640#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002641 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002642 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002643#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002644
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002645#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002646 /* skip argument check when restarting */
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002647 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002648#endif
2649 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002650 /* check_privkey is free */
2651 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2652
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002653 /* Common sanity checks */
2654 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2655 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002656 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002657
2658 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002659#if defined(ECP_MONTGOMERY)
Janos Follathdf9295b2019-02-26 12:36:52 +00002660 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002661 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002662#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002663#if defined(ECP_SHORTWEIERSTRASS)
Janos Follathdf9295b2019-02-26 12:36:52 +00002664 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002665 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002666#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002667
Janos Follath6c8ccd52016-11-29 15:37:09 +00002668cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002669
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002670#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002671 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002672 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002673#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002674
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002675#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002676 if( rs_ctx != NULL )
2677 rs_ctx->depth--;
2678#endif
2679
Janos Follathb0697532016-08-18 12:38:46 +01002680 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002681}
2682
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002683/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002684 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002685 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002686int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002687 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002688 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002689{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002690 ECP_VALIDATE_RET( grp != NULL );
2691 ECP_VALIDATE_RET( R != NULL );
2692 ECP_VALIDATE_RET( m != NULL );
2693 ECP_VALIDATE_RET( P != NULL );
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002694 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002695}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002696
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002697#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002698/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002699 * Check that an affine point is valid as a public key,
2700 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002703{
Janos Follath24eed8d2019-11-22 13:21:35 +00002704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002706
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002707 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2709 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2710 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2711 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2712 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002715
2716 /*
2717 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002718 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002719 */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002720 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &YY, &pt->Y, &pt->Y ) );
2721 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &RHS, &pt->X, &pt->X ) );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002722
2723 /* Special case for A = -3 */
2724 if( grp->A.p == NULL )
2725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002727 }
2728 else
2729 {
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002730 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &RHS, &RHS, &grp->A ) );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002731 }
2732
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02002733 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mod( grp, &RHS, &RHS, &pt->X ) );
2734 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mod( grp, &RHS, &RHS, &grp->B ) );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2737 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002738
2739cleanup:
2740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002742
2743 return( ret );
2744}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002745#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002746
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002747/*
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002748 * R = m * P with shortcuts for m == 1 and m == -1
2749 * NOT constant-time - ONLY for short Weierstrass!
2750 */
2751static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2752 mbedtls_ecp_point *R,
2753 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002754 const mbedtls_ecp_point *P,
2755 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002756{
Janos Follath24eed8d2019-11-22 13:21:35 +00002757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002758
2759 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
2760 {
2761 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2762 }
2763 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2764 {
2765 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2766 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2767 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2768 }
2769 else
2770 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002771 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2772 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002773 }
2774
2775cleanup:
2776 return( ret );
2777}
2778
2779/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002780 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002781 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002782 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002783int mbedtls_ecp_muladd_restartable(
2784 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002785 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002786 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2787 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002788{
Janos Follath24eed8d2019-11-22 13:21:35 +00002789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002790 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002791 mbedtls_ecp_point *pmP = &mP;
2792 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002793#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2794 char is_grp_capable = 0;
2795#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002796 ECP_VALIDATE_RET( grp != NULL );
2797 ECP_VALIDATE_RET( R != NULL );
2798 ECP_VALIDATE_RET( m != NULL );
2799 ECP_VALIDATE_RET( P != NULL );
2800 ECP_VALIDATE_RET( n != NULL );
2801 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002802
Janos Follathdf9295b2019-02-26 12:36:52 +00002803 if( mbedtls_ecp_get_type( grp ) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002804 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2805
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002806 mbedtls_ecp_point_init( &mP );
2807
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002808 ECP_RS_ENTER( ma );
2809
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002810#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002811 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2812 {
2813 /* redirect intermediate results to restart context */
2814 pmP = &rs_ctx->ma->mP;
2815 pR = &rs_ctx->ma->R;
2816
2817 /* jump to next operation */
2818 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2819 goto mul2;
2820 if( rs_ctx->ma->state == ecp_rsma_add )
2821 goto add;
2822 if( rs_ctx->ma->state == ecp_rsma_norm )
2823 goto norm;
2824 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002825#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002826
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002827 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002828#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002829 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002830 rs_ctx->ma->state = ecp_rsma_mul2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002831
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002832mul2:
2833#endif
2834 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002835
2836#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2837 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
2838 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
2839#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2840
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002841#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002842 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002843 rs_ctx->ma->state = ecp_rsma_add;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002844
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002845add:
2846#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002847 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002848 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002849#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002850 if( rs_ctx != NULL && rs_ctx->ma != NULL )
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002851 rs_ctx->ma->state = ecp_rsma_norm;
Janos Follath430d3372016-11-03 14:25:37 +00002852
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002853norm:
2854#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002855 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002856 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2857
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002858#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002859 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2860 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2861#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002862
2863cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002864#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002865 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002866 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002867#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002868
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002869 mbedtls_ecp_point_free( &mP );
2870
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002871 ECP_RS_LEAVE( ma );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002872
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002873 return( ret );
2874}
2875
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002876/*
2877 * Linear combination
2878 * NOT constant-time
2879 */
2880int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2881 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2882 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2883{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002884 ECP_VALIDATE_RET( grp != NULL );
2885 ECP_VALIDATE_RET( R != NULL );
2886 ECP_VALIDATE_RET( m != NULL );
2887 ECP_VALIDATE_RET( P != NULL );
2888 ECP_VALIDATE_RET( n != NULL );
2889 ECP_VALIDATE_RET( Q != NULL );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002890 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2891}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002892
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002893#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002894/*
2895 * Check validity of a public key for Montgomery curves with x-only schemes
2896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002898{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002899 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002900 /* Allow any public value, if it's too big then we'll just reduce it mod p
2901 * (RFC 7748 sec. 5 para. 3). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2903 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002904
2905 return( 0 );
2906}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002907#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002908
2909/*
2910 * Check that a point is valid as a public key
2911 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002912int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
2913 const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002914{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002915 ECP_VALIDATE_RET( grp != NULL );
2916 ECP_VALIDATE_RET( pt != NULL );
2917
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002918 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2920 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002921
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002922#if defined(ECP_MONTGOMERY)
Janos Follathdf9295b2019-02-26 12:36:52 +00002923 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002924 return( ecp_check_pubkey_mx( grp, pt ) );
2925#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002926#if defined(ECP_SHORTWEIERSTRASS)
Janos Follathdf9295b2019-02-26 12:36:52 +00002927 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002928 return( ecp_check_pubkey_sw( grp, pt ) );
2929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002931}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002932
2933/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002935 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002936int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
2937 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002938{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002939 ECP_VALIDATE_RET( grp != NULL );
2940 ECP_VALIDATE_RET( d != NULL );
2941
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002942#if defined(ECP_MONTGOMERY)
Janos Follathdf9295b2019-02-26 12:36:52 +00002943 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002944 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002945 /* see RFC 7748 sec. 5 para. 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2947 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002948 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002950
2951 /* see [Curve25519] page 5 */
2952 if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
2953 return( MBEDTLS_ERR_ECP_INVALID_KEY );
2954
2955 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002956 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002957#endif /* ECP_MONTGOMERY */
2958#if defined(ECP_SHORTWEIERSTRASS)
Janos Follathdf9295b2019-02-26 12:36:52 +00002959 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002960 {
2961 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2963 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2964 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002965 else
2966 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002967 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002968#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002971}
2972
2973/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002974 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002975 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002976int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
2977 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002978 int (*f_rng)(void *, unsigned char *, size_t),
2979 void *p_rng )
2980{
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002981 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002982 size_t n_size;
2983
2984 ECP_VALIDATE_RET( grp != NULL );
2985 ECP_VALIDATE_RET( d != NULL );
2986 ECP_VALIDATE_RET( f_rng != NULL );
2987
2988 n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002989
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002990#if defined(ECP_MONTGOMERY)
Janos Follathdf9295b2019-02-26 12:36:52 +00002991 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002992 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002993 /* [M225] page 5 */
2994 size_t b;
2995
Janos Follath98e28a72016-05-31 14:03:54 +01002996 do {
2997 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
2998 } while( mbedtls_mpi_bitlen( d ) == 0);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002999
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003000 /* Make sure the most significant bit is nbits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02003001 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003002 if( b > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003004 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003006
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003007 /* Make sure the last two bits are unset for Curve448, three bits for
3008 Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
3010 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003011 if( grp->nbits == 254 )
3012 {
3013 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
3014 }
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003015 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02003016#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003017
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02003018#if defined(ECP_SHORTWEIERSTRASS)
Janos Follathdf9295b2019-02-26 12:36:52 +00003019 if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003020 {
3021 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003022 int count = 0;
Janos Follath0e5532d2019-10-11 14:21:53 +01003023 unsigned cmp = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01003024
3025 /*
3026 * Match the procedure given in RFC 6979 (deterministic ECDSA):
3027 * - use the same byte ordering;
3028 * - keep the leftmost nbits bits of the generated octet string;
3029 * - try until result is in the desired range.
3030 * This also avoids any biais, which is especially important for ECDSA.
3031 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003032 do
3033 {
Hanno Becker7c8cb9c2017-10-17 15:19:38 +01003034 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003036
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01003037 /*
3038 * Each try has at worst a probability 1/2 of failing (the msb has
3039 * a probability 1/2 of being 0, and then the result will be < N),
3040 * so after 30 tries failure probability is a most 2**(-30).
3041 *
3042 * For most curves, 1 try is enough with overwhelming probability,
3043 * since N starts with a lot of 1s in binary, but some curves
3044 * such as secp224k1 are actually very close to the worst case.
3045 */
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02003046 if( ++count > 30 )
Jonasb2462142020-05-28 20:00:43 +09003047 {
3048 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
3049 goto cleanup;
3050 }
Janos Follatha779b462019-09-16 14:27:39 +01003051
Janos Follath0e5532d2019-10-11 14:21:53 +01003052 ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
Janos Follatha779b462019-09-16 14:27:39 +01003053 if( ret != 0 )
3054 {
3055 goto cleanup;
3056 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003057 }
Janos Follath0e5532d2019-10-11 14:21:53 +01003058 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01003059 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02003060#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003061
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01003062cleanup:
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003063 return( ret );
3064}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01003065
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003066/*
3067 * Generate a keypair with configurable base point
3068 */
3069int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
3070 const mbedtls_ecp_point *G,
3071 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3072 int (*f_rng)(void *, unsigned char *, size_t),
3073 void *p_rng )
3074{
Janos Follath24eed8d2019-11-22 13:21:35 +00003075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003076 ECP_VALIDATE_RET( grp != NULL );
3077 ECP_VALIDATE_RET( d != NULL );
3078 ECP_VALIDATE_RET( G != NULL );
3079 ECP_VALIDATE_RET( Q != NULL );
3080 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003081
3082 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
3083 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
3084
3085cleanup:
3086 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003087}
3088
3089/*
3090 * Generate key pair, wrapper for conventional base point
3091 */
3092int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
3093 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3094 int (*f_rng)(void *, unsigned char *, size_t),
3095 void *p_rng )
3096{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003097 ECP_VALIDATE_RET( grp != NULL );
3098 ECP_VALIDATE_RET( d != NULL );
3099 ECP_VALIDATE_RET( Q != NULL );
3100 ECP_VALIDATE_RET( f_rng != NULL );
3101
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003102 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003103}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003104
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003105/*
3106 * Generate a keypair, prettier wrapper
3107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003109 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
3110{
Janos Follath24eed8d2019-11-22 13:21:35 +00003111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003112 ECP_VALIDATE_RET( key != NULL );
3113 ECP_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003114
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003115 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003116 return( ret );
3117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003119}
3120
Janos Follath77800962019-02-25 16:32:08 +00003121#define ECP_CURVE25519_KEY_SIZE 32
Janos Follath171a7ef2019-02-15 16:17:45 +00003122/*
3123 * Read a private key.
3124 */
3125int mbedtls_ecp_read_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3126 const unsigned char *buf, size_t buflen )
3127{
3128 int ret = 0;
3129
Janos Follath28eb06d2019-02-26 10:53:34 +00003130 ECP_VALIDATE_RET( key != NULL );
3131 ECP_VALIDATE_RET( buf != NULL );
3132
Janos Follath171a7ef2019-02-15 16:17:45 +00003133 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
3134 return( ret );
3135
Janos Follath28eb06d2019-02-26 10:53:34 +00003136 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3137
3138#if defined(ECP_MONTGOMERY)
Janos Follathdf9295b2019-02-26 12:36:52 +00003139 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follath171a7ef2019-02-15 16:17:45 +00003140 {
Janos Follath28eb06d2019-02-26 10:53:34 +00003141 /*
3142 * If it is Curve25519 curve then mask the key as mandated by RFC7748
3143 */
3144 if( grp_id == MBEDTLS_ECP_DP_CURVE25519 )
3145 {
3146 if( buflen != ECP_CURVE25519_KEY_SIZE )
3147 return MBEDTLS_ERR_ECP_INVALID_KEY;
Janos Follath171a7ef2019-02-15 16:17:45 +00003148
Janos Follath28eb06d2019-02-26 10:53:34 +00003149 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary_le( &key->d, buf, buflen ) );
Janos Follath171a7ef2019-02-15 16:17:45 +00003150
Janos Follath28eb06d2019-02-26 10:53:34 +00003151 /* Set the three least significant bits to 0 */
3152 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 0, 0 ) );
3153 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 1, 0 ) );
3154 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &key->d, 2, 0 ) );
Janos Follath171a7ef2019-02-15 16:17:45 +00003155
Janos Follath28eb06d2019-02-26 10:53:34 +00003156 /* Set the most significant bit to 0 */
3157 MBEDTLS_MPI_CHK(
3158 mbedtls_mpi_set_bit( &key->d,
3159 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0 )
3160 );
Janos Follath171a7ef2019-02-15 16:17:45 +00003161
Janos Follath28eb06d2019-02-26 10:53:34 +00003162 /* Set the second most significant bit to 1 */
3163 MBEDTLS_MPI_CHK(
3164 mbedtls_mpi_set_bit( &key->d,
3165 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1 )
3166 );
3167 }
3168 else
3169 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Janos Follath171a7ef2019-02-15 16:17:45 +00003170 }
3171
3172#endif
3173#if defined(ECP_SHORTWEIERSTRASS)
Janos Follathdf9295b2019-02-26 12:36:52 +00003174 if( mbedtls_ecp_get_type( &key->grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
Janos Follath171a7ef2019-02-15 16:17:45 +00003175 {
3176 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &key->d, buf, buflen ) );
3177
3178 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( &key->grp, &key->d ) );
3179 }
3180
3181#endif
3182cleanup:
3183
3184 if( ret != 0 )
3185 mbedtls_mpi_free( &key->d );
3186
3187 return( ret );
3188}
3189
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003190/*
3191 * Check a public-private key pair
3192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003194{
Janos Follath24eed8d2019-11-22 13:21:35 +00003195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 mbedtls_ecp_point Q;
3197 mbedtls_ecp_group grp;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003198 ECP_VALIDATE_RET( pub != NULL );
3199 ECP_VALIDATE_RET( prv != NULL );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003202 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
3204 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
3205 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003208 }
3209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 mbedtls_ecp_point_init( &Q );
3211 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 /* mbedtls_ecp_mul() needs a non-const group... */
3214 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003215
3216 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
3220 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
3221 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003224 goto cleanup;
3225 }
3226
3227cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 mbedtls_ecp_point_free( &Q );
3229 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003230
3231 return( ret );
3232}
3233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003235
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003236/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003237 * Checkup routine
3238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003240{
Janos Follath24eed8d2019-11-22 13:21:35 +00003241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003242 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 mbedtls_ecp_group grp;
3244 mbedtls_ecp_point R, P;
3245 mbedtls_mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003246 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003247 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003248 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003249 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003250 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003251 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003252 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003253 "400000000000000000000000000000000000000000000000", /* one and zeros */
3254 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3255 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003256 };
3257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 mbedtls_ecp_group_init( &grp );
3259 mbedtls_ecp_point_init( &R );
3260 mbedtls_ecp_point_init( &P );
3261 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003262
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003263 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003265 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003266#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003267 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003268#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003269
3270 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003272
3273 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
3275 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003276
3277 add_count = 0;
3278 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003279 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3281 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003282
3283 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3284 {
3285 add_c_prev = add_count;
3286 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003287 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003288 add_count = 0;
3289 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003290 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3293 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003294
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003295 if( add_count != add_c_prev ||
3296 dbl_count != dbl_c_prev ||
3297 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003298 {
3299 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003301
3302 ret = 1;
3303 goto cleanup;
3304 }
3305 }
3306
3307 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003309
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003310 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003312 /* We computed P = 2G last time, use it */
3313
3314 add_count = 0;
3315 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003316 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
3318 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003319
3320 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
3321 {
3322 add_c_prev = add_count;
3323 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003324 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003325 add_count = 0;
3326 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003327 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
3330 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003331
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01003332 if( add_count != add_c_prev ||
3333 dbl_count != dbl_c_prev ||
3334 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003335 {
3336 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003338
3339 ret = 1;
3340 goto cleanup;
3341 }
3342 }
3343
3344 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003346
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003347cleanup:
3348
3349 if( ret < 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003350 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 mbedtls_ecp_group_free( &grp );
3353 mbedtls_ecp_point_free( &R );
3354 mbedtls_ecp_point_free( &P );
3355 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003356
3357 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003359
3360 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003361}
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003364
Janos Follathb0697532016-08-18 12:38:46 +01003365#endif /* !MBEDTLS_ECP_ALT */
3366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367#endif /* MBEDTLS_ECP_C */