blob: a029c22067c223aeae440e11583453138f9866c3 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010018 */
19
20/*
21 * References:
22 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010023 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010024 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010025 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010026 * RFC 4492 for the related TLS structures and constants
Nicholas Wilson08f3ef12015-11-10 13:10:01 +000027 * RFC 7748 for the Curve448 and Curve25519 curve definitions
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020028 *
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020029 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010030 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020031 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020032 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
33 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
34 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010035 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020036 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010037 * render ECC resistant against Side Channel Attacks. IACR Cryptology
38 * ePrint Archive, 2004, vol. 2004, p. 342.
39 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010040 */
41
Gilles Peskinedb09ef62020-06-03 01:43:33 +020042#include "common.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010043
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044/**
45 * \brief Function level alternative implementation.
46 *
47 * The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
48 * replace certain functions in this module. The alternative implementations are
49 * typically hardware accelerators and need to activate the hardware before the
50 * computation starts and deactivate it after it finishes. The
51 * mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
52 * this purpose.
53 *
54 * To preserve the correct functionality the following conditions must hold:
55 *
56 * - The alternative implementation must be activated by
57 * mbedtls_internal_ecp_init() before any of the replaceable functions is
58 * called.
59 * - mbedtls_internal_ecp_free() must \b only be called when the alternative
60 * implementation is activated.
61 * - mbedtls_internal_ecp_init() must \b not be called when the alternative
62 * implementation is activated.
63 * - Public functions must not return while the alternative implementation is
64 * activated.
65 * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
66 * before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
67 * \endcode ensures that the alternative implementation supports the current
68 * group.
69 */
70#if defined(MBEDTLS_ECP_INTERNAL_ALT)
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/ecp.h"
Janos Follath430d3372016-11-03 14:25:37 +000076#include "mbedtls/threading.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Janos Follathbc96a792021-06-24 14:48:38 +010079#include "mbedtls/bn_mul.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020080
Gilles Peskine80ba8502021-04-03 20:36:37 +020081#include "ecp_invasive.h"
82
Rich Evans00ab4702015-02-06 13:43:58 +000083#include <string.h>
84
Janos Follathb0697532016-08-18 12:38:46 +010085#if !defined(MBEDTLS_ECP_ALT)
86
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087/* Parameter validation macros based on platform_util.h */
David Horstmannceeaeb92023-01-05 15:44:23 +000088#define ECP_VALIDATE_RET(cond) \
89 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA)
90#define ECP_VALIDATE(cond) \
91 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000093#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020094
Janos Follath47d28f02016-11-01 13:22:05 +000095#include "mbedtls/ecp_internal.h"
Janos Follathb0697532016-08-18 12:38:46 +010096
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +020097#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +020098#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +020099#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200100#elif defined(MBEDTLS_CTR_DRBG_C)
101#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200102#else
David Horstmannceeaeb92023-01-05 15:44:23 +0000103#error \
104 "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200105#endif
106#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100109/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100110 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200111 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100112 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100113static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100114#endif
115
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200116#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
117/*
118 * Currently ecp_mul() takes a RNG function as an argument, used for
Manuel Pégourié-Gonnard25705e62020-06-10 09:18:25 +0200119 * side-channel protection, but it can be NULL. The initial reasoning was
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200120 * that people will pass non-NULL RNG when they care about side-channels, but
121 * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with
122 * no opportunity for the user to do anything about it.
123 *
124 * The obvious strategies for addressing that include:
125 * - change those APIs so that they take RNG arguments;
126 * - require a global RNG to be available to all crypto modules.
127 *
128 * Unfortunately those would break compatibility. So what we do instead is
129 * have our own internal DRBG instance, seeded from the secret scalar.
130 *
131 * The following is a light-weight abstraction layer for doing that with
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200132 * HMAC_DRBG (first choice) or CTR_DRBG.
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200133 */
134
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200135#if defined(MBEDTLS_HMAC_DRBG_C)
136
137/* DRBG context type */
138typedef mbedtls_hmac_drbg_context ecp_drbg_context;
139
140/* DRBG context init */
David Horstmannceeaeb92023-01-05 15:44:23 +0000141static inline void ecp_drbg_init(ecp_drbg_context *ctx)
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200142{
David Horstmannceeaeb92023-01-05 15:44:23 +0000143 mbedtls_hmac_drbg_init(ctx);
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200144}
145
146/* DRBG context free */
David Horstmannceeaeb92023-01-05 15:44:23 +0000147static inline void ecp_drbg_free(ecp_drbg_context *ctx)
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200148{
David Horstmannceeaeb92023-01-05 15:44:23 +0000149 mbedtls_hmac_drbg_free(ctx);
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200150}
151
152/* DRBG function */
David Horstmannceeaeb92023-01-05 15:44:23 +0000153static inline int ecp_drbg_random(void *p_rng,
154 unsigned char *output, size_t output_len)
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200155{
David Horstmannceeaeb92023-01-05 15:44:23 +0000156 return mbedtls_hmac_drbg_random(p_rng, output, output_len);
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200157}
158
159/* DRBG context seeding */
David Horstmannceeaeb92023-01-05 15:44:23 +0000160static int ecp_drbg_seed(ecp_drbg_context *ctx,
161 const mbedtls_mpi *secret, size_t secret_len)
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200162{
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200163 int ret;
164 unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200165 /* The list starts with strong hashes */
166 const mbedtls_md_type_t md_type = mbedtls_md_list()[0];
David Horstmannceeaeb92023-01-05 15:44:23 +0000167 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200168
David Horstmannceeaeb92023-01-05 15:44:23 +0000169 if (secret_len > MBEDTLS_ECP_MAX_BYTES) {
Manuel Pégourié-Gonnard1215c542020-06-19 11:59:49 +0200170 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
171 goto cleanup;
172 }
173
David Horstmannceeaeb92023-01-05 15:44:23 +0000174 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(secret,
175 secret_bytes, secret_len));
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200176
David Horstmannceeaeb92023-01-05 15:44:23 +0000177 ret = mbedtls_hmac_drbg_seed_buf(ctx, md_info, secret_bytes, secret_len);
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200178
179cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000180 mbedtls_platform_zeroize(secret_bytes, secret_len);
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200181
David Horstmannceeaeb92023-01-05 15:44:23 +0000182 return ret;
Manuel Pégourié-Gonnardd53ef2f2020-06-04 12:32:14 +0200183}
184
185#elif defined(MBEDTLS_CTR_DRBG_C)
186
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200187/* DRBG context type */
188typedef mbedtls_ctr_drbg_context ecp_drbg_context;
189
190/* DRBG context init */
David Horstmannceeaeb92023-01-05 15:44:23 +0000191static inline void ecp_drbg_init(ecp_drbg_context *ctx)
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200192{
David Horstmannceeaeb92023-01-05 15:44:23 +0000193 mbedtls_ctr_drbg_init(ctx);
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200194}
195
196/* DRBG context free */
David Horstmannceeaeb92023-01-05 15:44:23 +0000197static inline void ecp_drbg_free(ecp_drbg_context *ctx)
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200198{
David Horstmannceeaeb92023-01-05 15:44:23 +0000199 mbedtls_ctr_drbg_free(ctx);
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200200}
201
202/* DRBG function */
David Horstmannceeaeb92023-01-05 15:44:23 +0000203static inline int ecp_drbg_random(void *p_rng,
204 unsigned char *output, size_t output_len)
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200205{
David Horstmannceeaeb92023-01-05 15:44:23 +0000206 return mbedtls_ctr_drbg_random(p_rng, output, output_len);
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200207}
208
209/*
210 * Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does,
211 * we need to pass an entropy function when seeding. So we use a dummy
212 * function for that, and pass the actual entropy as customisation string.
213 * (During seeding of CTR_DRBG the entropy input and customisation string are
214 * concatenated before being used to update the secret state.)
215 */
216static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
217{
218 (void) ctx;
David Horstmannceeaeb92023-01-05 15:44:23 +0000219 memset(out, 0, len);
220 return 0;
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200221}
222
223/* DRBG context seeding */
David Horstmannceeaeb92023-01-05 15:44:23 +0000224static int ecp_drbg_seed(ecp_drbg_context *ctx,
225 const mbedtls_mpi *secret, size_t secret_len)
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200226{
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200227 int ret;
228 unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200229
David Horstmannceeaeb92023-01-05 15:44:23 +0000230 if (secret_len > MBEDTLS_ECP_MAX_BYTES) {
Manuel Pégourié-Gonnard1215c542020-06-19 11:59:49 +0200231 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
232 goto cleanup;
233 }
234
David Horstmannceeaeb92023-01-05 15:44:23 +0000235 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(secret,
236 secret_bytes, secret_len));
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200237
David Horstmannceeaeb92023-01-05 15:44:23 +0000238 ret = mbedtls_ctr_drbg_seed(ctx, ecp_ctr_drbg_null_entropy, NULL,
239 secret_bytes, secret_len);
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200240
241cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000242 mbedtls_platform_zeroize(secret_bytes, secret_len);
Manuel Pégourié-Gonnard4539a452020-06-18 12:27:56 +0200243
David Horstmannceeaeb92023-01-05 15:44:23 +0000244 return ret;
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200245}
246
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200247#else
David Horstmannceeaeb92023-01-05 15:44:23 +0000248#error \
249 "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200250#endif /* DRBG modules */
251#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
252
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200253#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100254/*
255 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200256 *
257 * Default value 0 means that ECC operations will not yield.
258 * Note that regardless of the value of ecp_max_ops, always at
259 * least one step is performed before yielding.
260 *
261 * Setting ecp_max_ops=1 can be suitable for testing purposes
262 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100263 */
264static unsigned ecp_max_ops = 0;
265
266/*
267 * Set ecp_max_ops
268 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000269void mbedtls_ecp_set_max_ops(unsigned max_ops)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100270{
271 ecp_max_ops = max_ops;
272}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100273
274/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200275 * Check if restart is enabled
276 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000277int mbedtls_ecp_restart_is_enabled(void)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200278{
David Horstmannceeaeb92023-01-05 15:44:23 +0000279 return ecp_max_ops != 0;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200280}
281
282/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200283 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100284 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000285struct mbedtls_ecp_restart_mul {
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100286 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100287 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100288 mbedtls_ecp_point *T; /* table for precomputed points */
289 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200290 enum { /* what were we doing last time we returned? */
291 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
292 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100293 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
294 ecp_rsm_pre_add, /* precompute remaining points by adding */
295 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200296 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100297 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100298 } state;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200299#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
300 ecp_drbg_context drbg_ctx;
301 unsigned char drbg_seeded;
302#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100303};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100304
305/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200306 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100307 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000308static void ecp_restart_rsm_init(mbedtls_ecp_restart_mul_ctx *ctx)
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100309{
David Horstmannceeaeb92023-01-05 15:44:23 +0000310 mbedtls_ecp_point_init(&ctx->R);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200311 ctx->i = 0;
312 ctx->T = NULL;
313 ctx->T_size = 0;
314 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200315#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +0000316 ecp_drbg_init(&ctx->drbg_ctx);
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200317 ctx->drbg_seeded = 0;
318#endif
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100319}
320
321/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200322 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100323 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000324static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx)
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100325{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100326 unsigned char i;
327
David Horstmannceeaeb92023-01-05 15:44:23 +0000328 if (ctx == NULL) {
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100329 return;
David Horstmannceeaeb92023-01-05 15:44:23 +0000330 }
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100331
David Horstmannceeaeb92023-01-05 15:44:23 +0000332 mbedtls_ecp_point_free(&ctx->R);
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100333
David Horstmannceeaeb92023-01-05 15:44:23 +0000334 if (ctx->T != NULL) {
335 for (i = 0; i < ctx->T_size; i++) {
336 mbedtls_ecp_point_free(ctx->T + i);
337 }
338 mbedtls_free(ctx->T);
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100339 }
340
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200341#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +0000342 ecp_drbg_free(&ctx->drbg_ctx);
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +0200343#endif
344
David Horstmannceeaeb92023-01-05 15:44:23 +0000345 ecp_restart_rsm_init(ctx);
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100346}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100347
348/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200349 * Restart context for ecp_muladd()
350 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000351struct mbedtls_ecp_restart_muladd {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200352 mbedtls_ecp_point mP; /* mP value */
353 mbedtls_ecp_point R; /* R intermediate result */
354 enum { /* what should we do next? */
355 ecp_rsma_mul1 = 0, /* first multiplication */
356 ecp_rsma_mul2, /* second multiplication */
357 ecp_rsma_add, /* addition */
358 ecp_rsma_norm, /* normalization */
359 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200360};
361
362/*
363 * Init restart_muladd sub-context
364 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000365static void ecp_restart_ma_init(mbedtls_ecp_restart_muladd_ctx *ctx)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200366{
David Horstmannceeaeb92023-01-05 15:44:23 +0000367 mbedtls_ecp_point_init(&ctx->mP);
368 mbedtls_ecp_point_init(&ctx->R);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200369 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200370}
371
372/*
373 * Free the components of a restart_muladd sub-context
374 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000375static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200376{
David Horstmannceeaeb92023-01-05 15:44:23 +0000377 if (ctx == NULL) {
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200378 return;
David Horstmannceeaeb92023-01-05 15:44:23 +0000379 }
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200380
David Horstmannceeaeb92023-01-05 15:44:23 +0000381 mbedtls_ecp_point_free(&ctx->mP);
382 mbedtls_ecp_point_free(&ctx->R);
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200383
David Horstmannceeaeb92023-01-05 15:44:23 +0000384 ecp_restart_ma_init(ctx);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200385}
386
387/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200388 * Initialize a restart context
389 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000390void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200391{
David Horstmannceeaeb92023-01-05 15:44:23 +0000392 ECP_VALIDATE(ctx != NULL);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200393 ctx->ops_done = 0;
394 ctx->depth = 0;
395 ctx->rsm = NULL;
396 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200397}
398
399/*
400 * Free the components of a restart context
401 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000402void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200403{
David Horstmannceeaeb92023-01-05 15:44:23 +0000404 if (ctx == NULL) {
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200405 return;
David Horstmannceeaeb92023-01-05 15:44:23 +0000406 }
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200407
David Horstmannceeaeb92023-01-05 15:44:23 +0000408 ecp_restart_rsm_free(ctx->rsm);
409 mbedtls_free(ctx->rsm);
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200410
David Horstmannceeaeb92023-01-05 15:44:23 +0000411 ecp_restart_ma_free(ctx->ma);
412 mbedtls_free(ctx->ma);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200413
David Horstmannceeaeb92023-01-05 15:44:23 +0000414 mbedtls_ecp_restart_init(ctx);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200415}
416
417/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100418 * Check if we can do the next step
419 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000420int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
421 mbedtls_ecp_restart_ctx *rs_ctx,
422 unsigned ops)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100423{
David Horstmannceeaeb92023-01-05 15:44:23 +0000424 ECP_VALIDATE_RET(grp != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500425
David Horstmannceeaeb92023-01-05 15:44:23 +0000426 if (rs_ctx != NULL && ecp_max_ops != 0) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100427 /* scale depending on curve size: the chosen reference is 256-bit,
428 * and multiplication is quadratic. Round to the closest integer. */
David Horstmannceeaeb92023-01-05 15:44:23 +0000429 if (grp->pbits >= 512) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100430 ops *= 4;
David Horstmannceeaeb92023-01-05 15:44:23 +0000431 } else if (grp->pbits >= 384) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100432 ops *= 2;
David Horstmannceeaeb92023-01-05 15:44:23 +0000433 }
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100434
Hanno Beckerb10c6602018-10-26 13:50:13 +0100435 /* Avoid infinite loops: always allow first step.
436 * Because of that, however, it's not generally true
437 * that ops_done <= ecp_max_ops, so the check
438 * ops_done > ecp_max_ops below is mandatory. */
David Horstmannceeaeb92023-01-05 15:44:23 +0000439 if ((rs_ctx->ops_done != 0) &&
440 (rs_ctx->ops_done > ecp_max_ops ||
441 ops > ecp_max_ops - rs_ctx->ops_done)) {
442 return MBEDTLS_ERR_ECP_IN_PROGRESS;
Hanno Beckerb10c6602018-10-26 13:50:13 +0100443 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100444
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100445 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200446 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100447 }
448
David Horstmannceeaeb92023-01-05 15:44:23 +0000449 return 0;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100450}
451
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200452/* Call this when entering a function that needs its own sub-context */
David Horstmannceeaeb92023-01-05 15:44:23 +0000453#define ECP_RS_ENTER(SUB) do { \
454 /* reset ops count for this call if top-level */ \
455 if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200456 rs_ctx->ops_done = 0; \
457 \
David Horstmannceeaeb92023-01-05 15:44:23 +0000458 /* set up our own sub-context if needed */ \
459 if (mbedtls_ecp_restart_is_enabled() && \
460 rs_ctx != NULL && rs_ctx->SUB == NULL) \
461 { \
462 rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
463 if (rs_ctx->SUB == NULL) \
464 return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
465 \
466 ecp_restart_## SUB ##_init(rs_ctx->SUB); \
467 } \
468} while (0)
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200469
470/* Call this when leaving a function that needs its own sub-context */
David Horstmannceeaeb92023-01-05 15:44:23 +0000471#define ECP_RS_LEAVE(SUB) do { \
472 /* clear our sub-context when not in progress (done or error) */ \
473 if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
474 ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
475 { \
476 ecp_restart_## SUB ##_free(rs_ctx->SUB); \
477 mbedtls_free(rs_ctx->SUB); \
478 rs_ctx->SUB = NULL; \
479 } \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200480 \
David Horstmannceeaeb92023-01-05 15:44:23 +0000481 if (rs_ctx != NULL) \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200482 rs_ctx->depth--; \
David Horstmannceeaeb92023-01-05 15:44:23 +0000483} while (0)
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200484
485#else /* MBEDTLS_ECP_RESTARTABLE */
486
David Horstmannceeaeb92023-01-05 15:44:23 +0000487#define ECP_RS_ENTER(sub) (void) rs_ctx;
488#define ECP_RS_LEAVE(sub) (void) rs_ctx;
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200489
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200490#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100491
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100492/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200493 * List of supported curves:
494 * - internal ID
Christoph M. Wintersteigercb310732019-02-15 15:50:38 +0000495 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200496 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200497 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100498 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100499 * Curves are listed in order: largest curves first, and for a given size,
500 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200501 *
Gilles Peskine1174db52020-02-26 19:52:06 +0100502 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
507 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
510 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
513 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
516 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
519 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
522 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
525 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
528 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
531 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
534 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
537 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100538#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200539#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Christoph M. Wintersteiger86e36c42018-12-06 17:27:31 +0000540 { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100541#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200542#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
543 { MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200546};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100547
David Horstmannceeaeb92023-01-05 15:44:23 +0000548#define ECP_NB_CURVES sizeof(ecp_supported_curves) / \
549 sizeof(ecp_supported_curves[0])
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200552
553/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200554 * List of supported curves and associated info
555 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000556const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200557{
David Horstmannceeaeb92023-01-05 15:44:23 +0000558 return ecp_supported_curves;
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200559}
560
561/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100562 * List of supported curves, group ID only
563 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000564const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100565{
566 static int init_done = 0;
567
David Horstmannceeaeb92023-01-05 15:44:23 +0000568 if (!init_done) {
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100569 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100571
David Horstmannceeaeb92023-01-05 15:44:23 +0000572 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
David Horstmannceeaeb92023-01-05 15:44:23 +0000574 curve_info++) {
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100575 ecp_supported_grp_id[i++] = curve_info->grp_id;
576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100578
579 init_done = 1;
580 }
581
David Horstmannceeaeb92023-01-05 15:44:23 +0000582 return ecp_supported_grp_id;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100583}
584
585/*
586 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200587 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000588const 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 +0200589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200591
David Horstmannceeaeb92023-01-05 15:44:23 +0000592 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
David Horstmannceeaeb92023-01-05 15:44:23 +0000594 curve_info++) {
595 if (curve_info->grp_id == grp_id) {
596 return curve_info;
597 }
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200598 }
599
David Horstmannceeaeb92023-01-05 15:44:23 +0000600 return NULL;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200601}
602
603/*
604 * Get the curve info from the TLS identifier
605 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000606const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200609
David Horstmannceeaeb92023-01-05 15:44:23 +0000610 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
David Horstmannceeaeb92023-01-05 15:44:23 +0000612 curve_info++) {
613 if (curve_info->tls_id == tls_id) {
614 return curve_info;
615 }
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200616 }
617
David Horstmannceeaeb92023-01-05 15:44:23 +0000618 return NULL;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200619}
620
621/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100622 * Get the curve info from the name
623 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000624const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100627
David Horstmannceeaeb92023-01-05 15:44:23 +0000628 if (name == NULL) {
629 return NULL;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100630 }
631
David Horstmannceeaeb92023-01-05 15:44:23 +0000632 for (curve_info = mbedtls_ecp_curve_list();
633 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
634 curve_info++) {
635 if (strcmp(curve_info->name, name) == 0) {
636 return curve_info;
637 }
638 }
639
640 return NULL;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100641}
642
643/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100644 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100645 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000646mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100647{
David Horstmannceeaeb92023-01-05 15:44:23 +0000648 if (grp->G.X.p == NULL) {
649 return MBEDTLS_ECP_TYPE_NONE;
650 }
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100651
David Horstmannceeaeb92023-01-05 15:44:23 +0000652 if (grp->G.Y.p == NULL) {
653 return MBEDTLS_ECP_TYPE_MONTGOMERY;
654 } else {
655 return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS;
656 }
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 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000662void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100663{
David Horstmannceeaeb92023-01-05 15:44:23 +0000664 ECP_VALIDATE(pt != NULL);
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100665
David Horstmannceeaeb92023-01-05 15:44:23 +0000666 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 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000674void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100675{
David Horstmannceeaeb92023-01-05 15:44:23 +0000676 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;
David Horstmannceeaeb92023-01-05 15:44:23 +0000679 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);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200684 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 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000698void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200699{
David Horstmannceeaeb92023-01-05 15:44:23 +0000700 ECP_VALIDATE(key != NULL);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200701
David Horstmannceeaeb92023-01-05 15:44:23 +0000702 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 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000710void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100711{
David Horstmannceeaeb92023-01-05 15:44:23 +0000712 if (pt == NULL) {
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100713 return;
David Horstmannceeaeb92023-01-05 15:44:23 +0000714 }
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100715
David Horstmannceeaeb92023-01-05 15:44:23 +0000716 mbedtls_mpi_free(&(pt->X));
717 mbedtls_mpi_free(&(pt->Y));
718 mbedtls_mpi_free(&(pt->Z));
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100719}
720
721/*
722 * Unallocate (the components of) a group
723 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000724void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100725{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200726 size_t i;
727
David Horstmannceeaeb92023-01-05 15:44:23 +0000728 if (grp == NULL) {
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100729 return;
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100730 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200731
David Horstmannceeaeb92023-01-05 15:44:23 +0000732 if (grp->h != 1) {
733 mbedtls_mpi_free(&grp->P);
734 mbedtls_mpi_free(&grp->A);
735 mbedtls_mpi_free(&grp->B);
736 mbedtls_ecp_point_free(&grp->G);
737 mbedtls_mpi_free(&grp->N);
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200738 }
739
David Horstmannceeaeb92023-01-05 15:44:23 +0000740 if (grp->T != NULL) {
741 for (i = 0; i < grp->T_size; i++) {
742 mbedtls_ecp_point_free(&grp->T[i]);
743 }
744 mbedtls_free(grp->T);
745 }
746
747 mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group));
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100748}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100749
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100750/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200751 * Unallocate (the components of) a key pair
752 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000753void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200754{
David Horstmannceeaeb92023-01-05 15:44:23 +0000755 if (key == NULL) {
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200756 return;
David Horstmannceeaeb92023-01-05 15:44:23 +0000757 }
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200758
David Horstmannceeaeb92023-01-05 15:44:23 +0000759 mbedtls_ecp_group_free(&key->grp);
760 mbedtls_mpi_free(&key->d);
761 mbedtls_ecp_point_free(&key->Q);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200762}
763
764/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200765 * Copy the contents of a point
766 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000767int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200768{
Janos Follath24eed8d2019-11-22 13:21:35 +0000769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +0000770 ECP_VALIDATE_RET(P != NULL);
771 ECP_VALIDATE_RET(Q != NULL);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200772
David Horstmannceeaeb92023-01-05 15:44:23 +0000773 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
774 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y));
775 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z));
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200776
777cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000778 return ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200779}
780
781/*
782 * Copy the contents of a group object
783 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000784int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200785{
David Horstmannceeaeb92023-01-05 15:44:23 +0000786 ECP_VALIDATE_RET(dst != NULL);
787 ECP_VALIDATE_RET(src != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500788
David Horstmannceeaeb92023-01-05 15:44:23 +0000789 return mbedtls_ecp_group_load(dst, src->id);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200790}
791
792/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100793 * Set point to zero
794 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000795int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100796{
Janos Follath24eed8d2019-11-22 13:21:35 +0000797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +0000798 ECP_VALIDATE_RET(pt != NULL);
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100799
David Horstmannceeaeb92023-01-05 15:44:23 +0000800 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
801 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1));
802 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0));
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100803
804cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000805 return ret;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100806}
807
808/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100809 * Tell if a point is zero
810 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000811int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100812{
David Horstmannceeaeb92023-01-05 15:44:23 +0000813 ECP_VALIDATE_RET(pt != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500814
David Horstmannceeaeb92023-01-05 15:44:23 +0000815 return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100816}
817
818/*
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500819 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200820 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000821int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
822 const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200823{
David Horstmannceeaeb92023-01-05 15:44:23 +0000824 ECP_VALIDATE_RET(P != NULL);
825 ECP_VALIDATE_RET(Q != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500826
David Horstmannceeaeb92023-01-05 15:44:23 +0000827 if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
828 mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
829 mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
830 return 0;
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200831 }
832
David Horstmannceeaeb92023-01-05 15:44:23 +0000833 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200834}
835
836/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100837 * Import a non-zero point from ASCII strings
838 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000839int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
840 const char *x, const char *y)
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100841{
Janos Follath24eed8d2019-11-22 13:21:35 +0000842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +0000843 ECP_VALIDATE_RET(P != NULL);
844 ECP_VALIDATE_RET(x != NULL);
845 ECP_VALIDATE_RET(y != NULL);
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100846
David Horstmannceeaeb92023-01-05 15:44:23 +0000847 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
848 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y));
849 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100850
851cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000852 return ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100853}
854
855/*
Janos Follath7caf8e42019-02-20 12:00:22 +0000856 * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100857 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000858int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
859 const mbedtls_ecp_point *P,
860 int format, size_t *olen,
861 unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100862{
Janos Follath28eb06d2019-02-26 10:53:34 +0000863 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100864 size_t plen;
David Horstmannceeaeb92023-01-05 15:44:23 +0000865 ECP_VALIDATE_RET(grp != NULL);
866 ECP_VALIDATE_RET(P != NULL);
867 ECP_VALIDATE_RET(olen != NULL);
868 ECP_VALIDATE_RET(buf != NULL);
869 ECP_VALIDATE_RET(format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
870 format == MBEDTLS_ECP_PF_COMPRESSED);
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100871
David Horstmannceeaeb92023-01-05 15:44:23 +0000872 plen = mbedtls_mpi_size(&grp->P);
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100873
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200874#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine59970052019-02-28 13:12:06 +0100875 (void) format; /* Montgomery curves always use the same point format */
David Horstmannceeaeb92023-01-05 15:44:23 +0000876 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000877 *olen = plen;
David Horstmannceeaeb92023-01-05 15:44:23 +0000878 if (buflen < *olen) {
879 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
880 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100881
David Horstmannceeaeb92023-01-05 15:44:23 +0000882 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen));
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100883 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000884#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200885#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +0000886 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000887 /*
888 * Common case: P == 0
889 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000890 if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
891 if (buflen < 1) {
892 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
893 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100894
Janos Follath7caf8e42019-02-20 12:00:22 +0000895 buf[0] = 0x00;
896 *olen = 1;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100897
David Horstmannceeaeb92023-01-05 15:44:23 +0000898 return 0;
Janos Follath7caf8e42019-02-20 12:00:22 +0000899 }
900
David Horstmannceeaeb92023-01-05 15:44:23 +0000901 if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000902 *olen = 2 * plen + 1;
903
David Horstmannceeaeb92023-01-05 15:44:23 +0000904 if (buflen < *olen) {
905 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
906 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000907
908 buf[0] = 0x04;
David Horstmannceeaeb92023-01-05 15:44:23 +0000909 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
910 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen));
911 } else if (format == MBEDTLS_ECP_PF_COMPRESSED) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000912 *olen = plen + 1;
913
David Horstmannceeaeb92023-01-05 15:44:23 +0000914 if (buflen < *olen) {
915 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
916 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000917
David Horstmannceeaeb92023-01-05 15:44:23 +0000918 buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0);
919 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
Janos Follath7caf8e42019-02-20 12:00:22 +0000920 }
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:
David Horstmannceeaeb92023-01-05 15:44:23 +0000925 return ret;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100926}
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 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000931int 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;
David Horstmannceeaeb92023-01-05 15:44:23 +0000937 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
David Horstmannceeaeb92023-01-05 15:44:23 +0000941 if (ilen < 1) {
942 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
943 }
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200944
David Horstmannceeaeb92023-01-05 15:44:23 +0000945 plen = mbedtls_mpi_size(&grp->P);
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100946
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200947#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +0000948 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
949 if (plen != ilen) {
950 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
951 }
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100952
David Horstmannceeaeb92023-01-05 15:44:23 +0000953 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen));
954 mbedtls_mpi_free(&pt->Y);
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100955
David Horstmannceeaeb92023-01-05 15:44:23 +0000956 if (grp->id == MBEDTLS_ECP_DP_CURVE25519) {
Janos Follathffbd7e82019-02-25 11:35:20 +0000957 /* Set most significant bit to 0 as prescribed in RFC7748 §5 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000958 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0));
959 }
Janos Follath28eb06d2019-02-26 10:53:34 +0000960
David Horstmannceeaeb92023-01-05 15:44:23 +0000961 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
Janos Follath59b813c2019-02-13 10:44:06 +0000962 }
963#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200964#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +0000965 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
966 if (buf[0] == 0x00) {
967 if (ilen == 1) {
968 return mbedtls_ecp_set_zero(pt);
969 } else {
970 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
971 }
Janos Follath59b813c2019-02-13 10:44:06 +0000972 }
973
David Horstmannceeaeb92023-01-05 15:44:23 +0000974 if (buf[0] != 0x04) {
975 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
976 }
Janos Follath59b813c2019-02-13 10:44:06 +0000977
David Horstmannceeaeb92023-01-05 15:44:23 +0000978 if (ilen != 2 * plen + 1) {
979 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
980 }
Janos Follath59b813c2019-02-13 10:44:06 +0000981
David Horstmannceeaeb92023-01-05 15:44:23 +0000982 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen));
983 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->Y,
984 buf + 1 + plen, plen));
985 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
Janos Follath59b813c2019-02-13 10:44:06 +0000986 }
987#endif
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100988
989cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000990 return ret;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100991}
992
993/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100994 * Import a point from a TLS ECPoint record (RFC 4492)
995 * struct {
996 * opaque point <1..2^8-1>;
997 * } ECPoint;
998 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000999int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
1000 mbedtls_ecp_point *pt,
1001 const unsigned char **buf, size_t buf_len)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001002{
1003 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001004 const unsigned char *buf_start;
David Horstmannceeaeb92023-01-05 15:44:23 +00001005 ECP_VALIDATE_RET(grp != NULL);
1006 ECP_VALIDATE_RET(pt != NULL);
1007 ECP_VALIDATE_RET(buf != NULL);
1008 ECP_VALIDATE_RET(*buf != NULL);
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001009
1010 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +02001011 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001012 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001013 if (buf_len < 2) {
1014 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1015 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001016
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001017 data_len = *(*buf)++;
David Horstmannceeaeb92023-01-05 15:44:23 +00001018 if (data_len < 1 || data_len > buf_len - 1) {
1019 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1020 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001021
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +01001022 /*
1023 * Save buffer start for read_binary and update buf
1024 */
1025 buf_start = *buf;
1026 *buf += data_len;
1027
David Horstmannceeaeb92023-01-05 15:44:23 +00001028 return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len);
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001029}
1030
1031/*
1032 * Export a point as a TLS ECPoint record (RFC 4492)
1033 * struct {
1034 * opaque point <1..2^8-1>;
1035 * } ECPoint;
1036 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001037int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
1038 int format, size_t *olen,
1039 unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001040{
Janos Follath24eed8d2019-11-22 13:21:35 +00001041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00001042 ECP_VALIDATE_RET(grp != NULL);
1043 ECP_VALIDATE_RET(pt != NULL);
1044 ECP_VALIDATE_RET(olen != NULL);
1045 ECP_VALIDATE_RET(buf != NULL);
1046 ECP_VALIDATE_RET(format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1047 format == MBEDTLS_ECP_PF_COMPRESSED);
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001048
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001049 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001050 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001051 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001052 if (blen < 1) {
1053 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1054 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001055
David Horstmannceeaeb92023-01-05 15:44:23 +00001056 if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format,
1057 olen, buf + 1, blen - 1)) != 0) {
1058 return ret;
1059 }
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001060
1061 /*
1062 * write length to the first byte and update total length
1063 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001064 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +01001065 ++*olen;
1066
David Horstmannceeaeb92023-01-05 15:44:23 +00001067 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +01001068}
1069
1070/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001071 * Set a group from an ECParameters record (RFC 4492)
1072 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001073int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
1074 const unsigned char **buf, size_t len)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001075{
Janos Follath24eed8d2019-11-22 13:21:35 +00001076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001077 mbedtls_ecp_group_id grp_id;
David Horstmannceeaeb92023-01-05 15:44:23 +00001078 ECP_VALIDATE_RET(grp != NULL);
1079 ECP_VALIDATE_RET(buf != NULL);
1080 ECP_VALIDATE_RET(*buf != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001081
David Horstmannceeaeb92023-01-05 15:44:23 +00001082 if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) {
1083 return ret;
1084 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001085
David Horstmannceeaeb92023-01-05 15:44:23 +00001086 return mbedtls_ecp_group_load(grp, grp_id);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001087}
1088
1089/*
1090 * Read a group id from an ECParameters record (RFC 4492) and convert it to
1091 * mbedtls_ecp_group_id.
1092 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001093int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
1094 const unsigned char **buf, size_t len)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001095{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001096 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 const mbedtls_ecp_curve_info *curve_info;
David Horstmannceeaeb92023-01-05 15:44:23 +00001098 ECP_VALIDATE_RET(grp != NULL);
1099 ECP_VALIDATE_RET(buf != NULL);
1100 ECP_VALIDATE_RET(*buf != NULL);
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001101
1102 /*
1103 * We expect at least three bytes (see below)
1104 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001105 if (len < 3) {
1106 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1107 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001108
1109 /*
1110 * First byte is curve_type; only named_curve is handled
1111 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001112 if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1113 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1114 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001115
1116 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001117 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +01001118 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001119 tls_id = *(*buf)++;
1120 tls_id <<= 8;
1121 tls_id |= *(*buf)++;
1122
David Horstmannceeaeb92023-01-05 15:44:23 +00001123 if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) {
1124 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
1125 }
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001126
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001127 *grp = curve_info->grp_id;
1128
David Horstmannceeaeb92023-01-05 15:44:23 +00001129 return 0;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001130}
1131
1132/*
1133 * Write the ECParameters record corresponding to a group (RFC 4492)
1134 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001135int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
1136 unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 const mbedtls_ecp_curve_info *curve_info;
David Horstmannceeaeb92023-01-05 15:44:23 +00001139 ECP_VALIDATE_RET(grp != NULL);
1140 ECP_VALIDATE_RET(buf != NULL);
1141 ECP_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +02001142
David Horstmannceeaeb92023-01-05 15:44:23 +00001143 if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) {
1144 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1145 }
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001146
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001147 /*
1148 * We are going to write 3 bytes (see below)
1149 */
1150 *olen = 3;
David Horstmannceeaeb92023-01-05 15:44:23 +00001151 if (blen < *olen) {
1152 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1153 }
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001154
1155 /*
1156 * First byte is curve_type, always named_curve
1157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001159
1160 /*
1161 * Next two bytes are the namedcurve value
1162 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001163 MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0);
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +01001164
David Horstmannceeaeb92023-01-05 15:44:23 +00001165 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +01001166}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001167
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001168/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1170 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001171 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 * 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 +02001173 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001174static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001175{
Janos Follath24eed8d2019-11-22 13:21:35 +00001176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001177
David Horstmannceeaeb92023-01-05 15:44:23 +00001178 if (grp->modp == NULL) {
1179 return mbedtls_mpi_mod_mpi(N, N, &grp->P);
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001180 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001181
David Horstmannceeaeb92023-01-05 15:44:23 +00001182 /* N->s < 0 is a much faster test, which fails only if N is 0 */
1183 if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
1184 mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
1185 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1186 }
1187
1188 MBEDTLS_MPI_CHK(grp->modp(N));
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001189
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001190 /* N->s < 0 is a much faster test, which fails only if N is 0 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001191 while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) {
1192 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P));
1193 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001194
David Horstmannceeaeb92023-01-05 15:44:23 +00001195 while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001196 /* we known P, N and the result are positive */
David Horstmannceeaeb92023-01-05 15:44:23 +00001197 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P));
1198 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001199
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001200cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001201 return ret;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001202}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001203
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001204/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001205 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001206 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001207 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 * 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 +01001209 * bring the result back to this range.
1210 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001211 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001212 */
1213
1214/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 * 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 +01001216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001218#define INC_MUL_COUNT mul_count++;
1219#else
1220#define INC_MUL_COUNT
1221#endif
1222
David Horstmannceeaeb92023-01-05 15:44:23 +00001223#define MOD_MUL(N) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001224 do \
1225 { \
David Horstmannceeaeb92023-01-05 15:44:23 +00001226 MBEDTLS_MPI_CHK(ecp_modp(&(N), grp)); \
Hanno Becker1eeca412018-10-15 12:01:35 +01001227 INC_MUL_COUNT \
David Horstmannceeaeb92023-01-05 15:44:23 +00001228 } while (0)
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001229
David Horstmannceeaeb92023-01-05 15:44:23 +00001230static inline int mbedtls_mpi_mul_mod(const mbedtls_ecp_group *grp,
1231 mbedtls_mpi *X,
1232 const mbedtls_mpi *A,
1233 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001234{
Janos Follath24eed8d2019-11-22 13:21:35 +00001235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00001236 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(X, A, B));
1237 MOD_MUL(*X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001238cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001239 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001240}
1241
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001242/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001244 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001245 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001246#define MOD_SUB(N) \
1247 while ((N).s < 0 && mbedtls_mpi_cmp_int(&(N), 0) != 0) \
1248 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&(N), &(N), &grp->P))
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001249
David Horstmannceeaeb92023-01-05 15:44:23 +00001250#if (defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
1251 !(defined(MBEDTLS_ECP_NO_FALLBACK) && \
1252 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1253 defined(MBEDTLS_ECP_ADD_MIXED_ALT))) || \
1254 (defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) && \
1255 !(defined(MBEDTLS_ECP_NO_FALLBACK) && \
1256 defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)))
1257static inline int mbedtls_mpi_sub_mod(const mbedtls_ecp_group *grp,
1258 mbedtls_mpi *X,
1259 const mbedtls_mpi *A,
1260 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001261{
Janos Follath24eed8d2019-11-22 13:21:35 +00001262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00001263 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, A, B));
1264 MOD_SUB(*X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001265cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001266 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001267}
Steven Cooremane5388962021-03-01 14:04:53 +01001268#endif /* All functions referencing mbedtls_mpi_sub_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001269
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001270/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 * 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 +02001272 * We known P, N and the result are positive, so sub_abs is correct, and
1273 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001274 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001275#define MOD_ADD(N) \
1276 while (mbedtls_mpi_cmp_mpi(&(N), &grp->P) >= 0) \
1277 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&(N), &(N), &grp->P))
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001278
David Horstmannceeaeb92023-01-05 15:44:23 +00001279static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp,
1280 mbedtls_mpi *X,
1281 const mbedtls_mpi *A,
1282 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001283{
Janos Follath24eed8d2019-11-22 13:21:35 +00001284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00001285 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B));
1286 MOD_ADD(*X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001287cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001288 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001289}
1290
Steven Cooremane5388962021-03-01 14:04:53 +01001291#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
David Horstmannceeaeb92023-01-05 15:44:23 +00001292 !(defined(MBEDTLS_ECP_NO_FALLBACK) && \
1293 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1294 defined(MBEDTLS_ECP_ADD_MIXED_ALT))
1295static inline int mbedtls_mpi_shift_l_mod(const mbedtls_ecp_group *grp,
1296 mbedtls_mpi *X,
1297 size_t count)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001298{
Janos Follath24eed8d2019-11-22 13:21:35 +00001299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00001300 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, count));
1301 MOD_ADD(*X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001302cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001303 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001304}
David Horstmannceeaeb92023-01-05 15:44:23 +00001305#endif \
1306 /* All functions referencing mbedtls_mpi_shift_l_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001307
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02001308#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001309/*
1310 * For curves in short Weierstrass form, we do all the internal operations in
1311 * Jacobian coordinates.
1312 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001313 * For multiplication, we'll use a comb method with countermeasures against
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001314 * SPA, hence timing attacks.
1315 */
1316
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001317/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001318 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001319 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001320 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001321static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001322{
David Horstmannceeaeb92023-01-05 15:44:23 +00001323 if (mbedtls_mpi_cmp_int(&pt->Z, 0) == 0) {
1324 return 0;
1325 }
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001326
Janos Follathb0697532016-08-18 12:38:46 +01001327#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001328 if (mbedtls_internal_ecp_grp_capable(grp)) {
1329 return mbedtls_internal_ecp_normalize_jac(grp, pt);
1330 }
Janos Follath372697b2016-10-28 16:53:11 +01001331#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001332
1333#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001334 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001335#else
1336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1337 mbedtls_mpi Zi, ZZi;
David Horstmannceeaeb92023-01-05 15:44:23 +00001338 mbedtls_mpi_init(&Zi); mbedtls_mpi_init(&ZZi);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001339
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001340 /*
1341 * X = X / Z^2 mod p
1342 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001343 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&Zi, &pt->Z, &grp->P));
1344 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ZZi, &Zi, &Zi));
1345 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->X, &pt->X, &ZZi));
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001346
1347 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001348 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001349 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001350 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &ZZi));
1351 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &Zi));
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001352
1353 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001354 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001355 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001356 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001357
1358cleanup:
1359
David Horstmannceeaeb92023-01-05 15:44:23 +00001360 mbedtls_mpi_free(&Zi); mbedtls_mpi_free(&ZZi);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001361
David Horstmannceeaeb92023-01-05 15:44:23 +00001362 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001363#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001364}
1365
1366/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001367 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001368 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001369 * (See for example Cohen's "A Course in Computational Algebraic Number
1370 * Theory", Algorithm 10.3.4.)
1371 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001372 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001373 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001374 *
1375 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001376 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001377static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
1378 mbedtls_ecp_point *T[], size_t T_size)
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001379{
David Horstmannceeaeb92023-01-05 15:44:23 +00001380 if (T_size < 2) {
1381 return ecp_normalize_jac(grp, *T);
1382 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001383
Janos Follathb0697532016-08-18 12:38:46 +01001384#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001385 if (mbedtls_internal_ecp_grp_capable(grp)) {
1386 return mbedtls_internal_ecp_normalize_jac_many(grp, T, T_size);
1387 }
Janos Follathb0697532016-08-18 12:38:46 +01001388#endif
1389
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001390#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001391 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001392#else
1393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1394 size_t i;
1395 mbedtls_mpi *c, u, Zi, ZZi;
1396
David Horstmannceeaeb92023-01-05 15:44:23 +00001397 if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) {
1398 return MBEDTLS_ERR_ECP_ALLOC_FAILED;
1399 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001400
David Horstmannceeaeb92023-01-05 15:44:23 +00001401 for (i = 0; i < T_size; i++) {
1402 mbedtls_mpi_init(&c[i]);
1403 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02001404
David Horstmannceeaeb92023-01-05 15:44:23 +00001405 mbedtls_mpi_init(&u); mbedtls_mpi_init(&Zi); mbedtls_mpi_init(&ZZi);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001406
1407 /*
1408 * c[i] = Z_0 * ... * Z_i
1409 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001410 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&c[0], &T[0]->Z));
1411 for (i = 1; i < T_size; i++) {
1412 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 +01001413 }
1414
1415 /*
1416 * u = 1 / (Z_0 * ... * Z_n) mod P
1417 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001418 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&u, &c[T_size-1], &grp->P));
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001419
David Horstmannceeaeb92023-01-05 15:44:23 +00001420 for (i = T_size - 1;; i--) {
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001421 /*
1422 * Zi = 1 / Z_i mod p
1423 * u = 1 / (Z_0 * ... * Z_i) mod P
1424 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001425 if (i == 0) {
1426 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Zi, &u));
1427 } else {
1428 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &Zi, &u, &c[i-1]));
1429 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &u, &u, &T[i]->Z));
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001430 }
1431
1432 /*
1433 * proceed as in normalize()
1434 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001435 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ZZi, &Zi, &Zi));
1436 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->X, &T[i]->X, &ZZi));
1437 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->Y, &T[i]->Y, &ZZi));
1438 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->Y, &T[i]->Y, &Zi));
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001439
1440 /*
1441 * Post-precessing: reclaim some memory by shrinking coordinates
1442 * - not storing Z (always 1)
1443 * - shrinking other coordinates, but still keeping the same number of
1444 * limbs as P, as otherwise it will too likely be regrown too fast.
1445 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001446 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n));
1447 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n));
1448 mbedtls_mpi_free(&T[i]->Z);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001449
David Horstmannceeaeb92023-01-05 15:44:23 +00001450 if (i == 0) {
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001451 break;
David Horstmannceeaeb92023-01-05 15:44:23 +00001452 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001453 }
1454
1455cleanup:
1456
David Horstmannceeaeb92023-01-05 15:44:23 +00001457 mbedtls_mpi_free(&u); mbedtls_mpi_free(&Zi); mbedtls_mpi_free(&ZZi);
1458 for (i = 0; i < T_size; i++) {
1459 mbedtls_mpi_free(&c[i]);
1460 }
1461 mbedtls_free(c);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001462
David Horstmannceeaeb92023-01-05 15:44:23 +00001463 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001464#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) */
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001465}
1466
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001467/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001468 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1469 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1470 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001471static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
1472 mbedtls_ecp_point *Q,
1473 unsigned char inv)
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001474{
Janos Follath24eed8d2019-11-22 13:21:35 +00001475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001476 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001478
David Horstmannceeaeb92023-01-05 15:44:23 +00001479 mbedtls_mpi_init(&mQY);
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001480
1481 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001482 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mQY, &grp->P, &Q->Y));
1483 nonzero = mbedtls_mpi_cmp_int(&Q->Y, 0) != 0;
1484 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&Q->Y, &mQY, inv & nonzero));
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001485
1486cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001487 mbedtls_mpi_free(&mQY);
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001488
David Horstmannceeaeb92023-01-05 15:44:23 +00001489 return ret;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001490}
1491
1492/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001493 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001494 *
Peter Dettmance661b22015-02-07 14:43:51 +07001495 * 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 +01001496 *
Peter Dettmance661b22015-02-07 14:43:51 +07001497 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1498 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1499 *
1500 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1501 *
1502 * Cost: 1D := 3M + 4S (A == 0)
1503 * 4M + 4S (A == -3)
1504 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001505 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001506static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1507 const mbedtls_ecp_point *P)
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001510 dbl_count++;
1511#endif
1512
Janos Follathb0697532016-08-18 12:38:46 +01001513#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001514 if (mbedtls_internal_ecp_grp_capable(grp)) {
1515 return mbedtls_internal_ecp_double_jac(grp, R, P);
1516 }
Janos Follath372697b2016-10-28 16:53:11 +01001517#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001518
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001519#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001520 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001521#else
1522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1523 mbedtls_mpi M, S, T, U;
1524
David Horstmannceeaeb92023-01-05 15:44:23 +00001525 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 +01001526
1527 /* Special case for A = -3 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001528 if (grp->A.p == NULL) {
Peter Dettmance661b22015-02-07 14:43:51 +07001529 /* M = 3(X + Z^2)(X - Z^2) */
David Horstmannceeaeb92023-01-05 15:44:23 +00001530 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->Z, &P->Z));
1531 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &T, &P->X, &S));
1532 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &U, &P->X, &S));
1533 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &T, &U));
1534 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&M, &S, 3)); MOD_ADD(M);
1535 } else {
Peter Dettmance661b22015-02-07 14:43:51 +07001536 /* M = 3.X^2 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001537 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->X, &P->X));
1538 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&M, &S, 3)); MOD_ADD(M);
Peter Dettmance661b22015-02-07 14:43:51 +07001539
1540 /* Optimize away for "koblitz" curves with A = 0 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001541 if (mbedtls_mpi_cmp_int(&grp->A, 0) != 0) {
Peter Dettmance661b22015-02-07 14:43:51 +07001542 /* M += A.Z^4 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001543 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->Z, &P->Z));
1544 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &S, &S));
1545 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &T, &grp->A));
1546 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &M, &M, &S));
Peter Dettmance661b22015-02-07 14:43:51 +07001547 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001548 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001549
Peter Dettmance661b22015-02-07 14:43:51 +07001550 /* S = 4.X.Y^2 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001551 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &P->Y, &P->Y));
1552 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &T, 1));
1553 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->X, &T));
1554 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &S, 1));
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001555
Peter Dettmance661b22015-02-07 14:43:51 +07001556 /* U = 8.Y^4 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001557 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &U, &T, &T));
1558 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &U, 1));
Peter Dettmance661b22015-02-07 14:43:51 +07001559
1560 /* T = M^2 - 2.S */
David Horstmannceeaeb92023-01-05 15:44:23 +00001561 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &M, &M));
1562 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T, &T, &S));
1563 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T, &T, &S));
Peter Dettmance661b22015-02-07 14:43:51 +07001564
1565 /* S = M(S - T) - U */
David Horstmannceeaeb92023-01-05 15:44:23 +00001566 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S, &S, &T));
1567 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &S, &M));
1568 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S, &S, &U));
Peter Dettmance661b22015-02-07 14:43:51 +07001569
1570 /* U = 2.Y.Z */
David Horstmannceeaeb92023-01-05 15:44:23 +00001571 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &U, &P->Y, &P->Z));
1572 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &U, 1));
Peter Dettmance661b22015-02-07 14:43:51 +07001573
David Horstmannceeaeb92023-01-05 15:44:23 +00001574 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->X, &T));
1575 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Y, &S));
1576 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Z, &U));
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001577
1578cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001579 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 +02001580
David Horstmannceeaeb92023-01-05 15:44:23 +00001581 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001582#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) */
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001583}
1584
1585/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001586 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001587 *
1588 * The coordinates of Q must be normalized (= affine),
1589 * but those of P don't need to. R is not normalized.
1590 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001591 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001592 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001593 * - at each step, P, Q and R are multiples of the base point, the factor
1594 * being less than its order, so none of them is zero;
1595 * - Q is an odd multiple of the base point, P an even multiple,
1596 * due to the choice of precomputed points in the modified comb method.
1597 * So branches for these cases do not leak secret information.
1598 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001599 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1600 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001601 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001602 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001603static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1604 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001607 add_count++;
1608#endif
1609
Janos Follathb0697532016-08-18 12:38:46 +01001610#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001611 if (mbedtls_internal_ecp_grp_capable(grp)) {
1612 return mbedtls_internal_ecp_add_mixed(grp, R, P, Q);
1613 }
Janos Follath372697b2016-10-28 16:53:11 +01001614#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001615
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001616#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_ADD_MIXED_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001617 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001618#else
1619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1620 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
1621
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001622 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001623 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001624 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001625 if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
1626 return mbedtls_ecp_copy(R, Q);
1627 }
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001628
David Horstmannceeaeb92023-01-05 15:44:23 +00001629 if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 0) == 0) {
1630 return mbedtls_ecp_copy(R, P);
1631 }
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001632
1633 /*
1634 * Make sure Q coordinates are normalized
1635 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001636 if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 1) != 0) {
1637 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1638 }
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001639
David Horstmannceeaeb92023-01-05 15:44:23 +00001640 mbedtls_mpi_init(&T1); mbedtls_mpi_init(&T2); mbedtls_mpi_init(&T3); mbedtls_mpi_init(&T4);
1641 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001642
David Horstmannceeaeb92023-01-05 15:44:23 +00001643 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T1, &P->Z, &P->Z));
1644 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T2, &T1, &P->Z));
1645 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T1, &T1, &Q->X));
1646 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T2, &T2, &Q->Y));
1647 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T1, &T1, &P->X));
1648 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T2, &T2, &P->Y));
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001649
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001650 /* Special cases (2) and (3) */
David Horstmannceeaeb92023-01-05 15:44:23 +00001651 if (mbedtls_mpi_cmp_int(&T1, 0) == 0) {
1652 if (mbedtls_mpi_cmp_int(&T2, 0) == 0) {
1653 ret = ecp_double_jac(grp, R, P);
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001654 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +00001655 } else {
1656 ret = mbedtls_ecp_set_zero(R);
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001657 goto cleanup;
1658 }
1659 }
1660
David Horstmannceeaeb92023-01-05 15:44:23 +00001661 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &Z, &P->Z, &T1));
1662 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T1, &T1));
1663 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T4, &T3, &T1));
1664 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T3, &P->X));
1665 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &T3));
1666 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &T1, 1));
1667 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &X, &T2, &T2));
1668 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &X, &X, &T1));
1669 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &X, &X, &T4));
1670 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T3, &T3, &X));
1671 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T3, &T2));
1672 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T4, &T4, &P->Y));
1673 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &Y, &T3, &T4));
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001674
David Horstmannceeaeb92023-01-05 15:44:23 +00001675 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->X, &X));
1676 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Y, &Y));
1677 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Z, &Z));
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001678
1679cleanup:
1680
David Horstmannceeaeb92023-01-05 15:44:23 +00001681 mbedtls_mpi_free(&T1); mbedtls_mpi_free(&T2); mbedtls_mpi_free(&T3); mbedtls_mpi_free(&T4);
1682 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001683
David Horstmannceeaeb92023-01-05 15:44:23 +00001684 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001685#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001686}
1687
1688/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001689 * Randomize jacobian coordinates:
1690 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001691 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001692 *
1693 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001694 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001695static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
1696 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001697{
Janos Follathb0697532016-08-18 12:38:46 +01001698#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001699 if (mbedtls_internal_ecp_grp_capable(grp)) {
1700 return mbedtls_internal_ecp_randomize_jac(grp, pt, f_rng, p_rng);
1701 }
Janos Follath372697b2016-10-28 16:53:11 +01001702#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001703
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001704#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00001705 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001706#else
1707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1708 mbedtls_mpi l, ll;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001709
David Horstmannceeaeb92023-01-05 15:44:23 +00001710 mbedtls_mpi_init(&l); mbedtls_mpi_init(&ll);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001711
1712 /* Generate l such that 1 < l < p */
David Horstmannceeaeb92023-01-05 15:44:23 +00001713 MBEDTLS_MPI_CHK(mbedtls_mpi_random(&l, 2, &grp->P, f_rng, p_rng));
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001714
1715 /* Z = l * Z */
David Horstmannceeaeb92023-01-05 15:44:23 +00001716 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Z, &pt->Z, &l));
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001717
1718 /* X = l^2 * X */
David Horstmannceeaeb92023-01-05 15:44:23 +00001719 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ll, &l, &l));
1720 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->X, &pt->X, &ll));
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001721
1722 /* Y = l^3 * Y */
David Horstmannceeaeb92023-01-05 15:44:23 +00001723 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ll, &ll, &l));
1724 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &ll));
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001725
1726cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00001727 mbedtls_mpi_free(&l); mbedtls_mpi_free(&ll);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001728
David Horstmannceeaeb92023-01-05 15:44:23 +00001729 if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Gilles Peskine6466d342021-03-29 22:28:50 +02001730 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
David Horstmannceeaeb92023-01-05 15:44:23 +00001731 }
1732 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001733#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) */
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001734}
1735
1736/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001737 * Check and define parameters used by the comb method (see below for details)
1738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1740#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001741#endif
1742
1743/* d = ceil( n / w ) */
David Horstmannceeaeb92023-01-05 15:44:23 +00001744#define COMB_MAX_D (MBEDTLS_ECP_MAX_BITS + 1) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001745
1746/* number of precomputed points */
David Horstmannceeaeb92023-01-05 15:44:23 +00001747#define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001748
1749/*
1750 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001751 *
1752 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001753 * modified version that provides resistance to SPA by avoiding zero
1754 * digits in the representation as in [3]. We modify the method further by
1755 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001756 * representation uses one more K_i, due to carries, but saves on the size of
1757 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001758 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001759 * Summary of the comb method and its modifications:
1760 *
1761 * - The goal is to compute m*P for some w*d-bit integer m.
1762 *
1763 * - The basic comb method splits m into the w-bit integers
1764 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1765 * index has residue i modulo d, and computes m * P as
1766 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1767 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1768 *
1769 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1770 * .. + 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]] ..,
1771 * thereby successively converting it into a form where all summands
1772 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1773 *
1774 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1775 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1776 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1777 * Performing and iterating this procedure for those x[i] that are even
1778 * (keeping track of carry), we can transform the original sum into one of the form
1779 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1780 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1781 * which is why we are only computing half of it in the first place in
1782 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1783 *
1784 * - For the sake of compactness, only the seven low-order bits of x[i]
1785 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001786 * 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 +02001787 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001788 *
1789 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001790 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001791 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001793 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1794 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001795 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001796static void ecp_comb_recode_core(unsigned char x[], size_t d,
1797 unsigned char w, const mbedtls_mpi *m)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001798{
1799 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001800 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001801
David Horstmannceeaeb92023-01-05 15:44:23 +00001802 memset(x, 0, d+1);
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001803
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001804 /* First get the classical comb values (except for x_d = 0) */
David Horstmannceeaeb92023-01-05 15:44:23 +00001805 for (i = 0; i < d; i++) {
1806 for (j = 0; j < w; j++) {
1807 x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j;
1808 }
1809 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001810
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001811 /* Now make sure x_1 .. x_d are odd */
1812 c = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00001813 for (i = 1; i <= d; i++) {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001814 /* Add carry and update it */
1815 cc = x[i] & c;
1816 x[i] = x[i] ^ c;
1817 c = cc;
1818
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001819 /* Adjust if needed, avoiding branches */
David Horstmannceeaeb92023-01-05 15:44:23 +00001820 adjust = 1 - (x[i] & 0x01);
1821 c |= x[i] & (x[i-1] * adjust);
1822 x[i] = x[i] ^ (x[i-1] * adjust);
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001823 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001824 }
1825}
1826
1827/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001828 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001829 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001830 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001831 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001832 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1833 * 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 +01001834 *
1835 * 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 +02001836 *
1837 * Note: Even comb values (those where P would be omitted from the
1838 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001839 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001840 *
1841 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001842 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001843 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1844 * (3) [add] Computation of all T[i]
1845 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001846 *
1847 * Step 1 can be interrupted but not the others; together with the final
1848 * coordinate normalization they are the largest steps done at once, depending
1849 * on the window size. Here are operation counts for P-256:
1850 *
1851 * step (2) (3) (4)
1852 * w = 5 142 165 208
1853 * w = 4 136 77 160
1854 * w = 3 130 33 136
1855 * w = 2 124 11 124
1856 *
1857 * So if ECC operations are blocking for too long even with a low max_ops
1858 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1859 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001860 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001861static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
1862 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
1863 unsigned char w, size_t d,
1864 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001865{
Janos Follath24eed8d2019-11-22 13:21:35 +00001866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001867 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001868 size_t j = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00001869 const unsigned char T_size = 1U << (w - 1);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001871
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001872#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00001873 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1874 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001875 goto dbl;
David Horstmannceeaeb92023-01-05 15:44:23 +00001876 }
1877 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) {
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001878 goto norm_dbl;
David Horstmannceeaeb92023-01-05 15:44:23 +00001879 }
1880 if (rs_ctx->rsm->state == ecp_rsm_pre_add) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001881 goto add;
David Horstmannceeaeb92023-01-05 15:44:23 +00001882 }
1883 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001884 goto norm_add;
David Horstmannceeaeb92023-01-05 15:44:23 +00001885 }
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001886 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001887#else
1888 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001889#endif
1890
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001891#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00001892 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001893 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1894
1895 /* initial state for the loop */
1896 rs_ctx->rsm->i = 0;
1897 }
1898
1899dbl:
1900#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001901 /*
1902 * Set T[0] = P and
1903 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1904 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001905 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&T[0], P));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001906
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001907#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00001908 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001909 j = rs_ctx->rsm->i;
David Horstmannceeaeb92023-01-05 15:44:23 +00001910 } else
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001911#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00001912 j = 0;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001913
David Horstmannceeaeb92023-01-05 15:44:23 +00001914 for (; j < d * (w - 1); j++) {
1915 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL);
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001916
David Horstmannceeaeb92023-01-05 15:44:23 +00001917 i = 1U << (j / d);
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001918 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001919
David Horstmannceeaeb92023-01-05 15:44:23 +00001920 if (j % d == 0) {
1921 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(cur, T + (i >> 1)));
1922 }
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001923
David Horstmannceeaeb92023-01-05 15:44:23 +00001924 MBEDTLS_MPI_CHK(ecp_double_jac(grp, cur, cur));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001925 }
1926
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001927#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00001928 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001929 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
David Horstmannceeaeb92023-01-05 15:44:23 +00001930 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001931
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001932norm_dbl:
1933#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001934 /*
1935 * Normalize current elements in T. As T has holes,
1936 * use an auxiliary array of pointers to elements in T.
1937 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001938 j = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00001939 for (i = 1; i < T_size; i <<= 1) {
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001940 TT[j++] = T + i;
David Horstmannceeaeb92023-01-05 15:44:23 +00001941 }
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001942
David Horstmannceeaeb92023-01-05 15:44:23 +00001943 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001944
David Horstmannceeaeb92023-01-05 15:44:23 +00001945 MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001946
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001947#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00001948 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001949 rs_ctx->rsm->state = ecp_rsm_pre_add;
David Horstmannceeaeb92023-01-05 15:44:23 +00001950 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001951
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001952add:
1953#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001954 /*
1955 * Compute the remaining ones using the minimal number of additions
1956 * Be careful to update T[2^l] only after using it!
1957 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001958 MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001959
David Horstmannceeaeb92023-01-05 15:44:23 +00001960 for (i = 1; i < T_size; i <<= 1) {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001961 j = i;
David Horstmannceeaeb92023-01-05 15:44:23 +00001962 while (j--) {
1963 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, &T[i + j], &T[j], &T[i]));
1964 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001965 }
1966
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001967#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00001968 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001969 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
David Horstmannceeaeb92023-01-05 15:44:23 +00001970 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001971
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001972norm_add:
1973#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001974 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001975 * Normalize final elements in T. Even though there are no holes now, we
1976 * still need the auxiliary array for homogeneity with the previous
1977 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001978 */
David Horstmannceeaeb92023-01-05 15:44:23 +00001979 for (j = 0; j + 1 < T_size; j++) {
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001980 TT[j] = T + j + 1;
David Horstmannceeaeb92023-01-05 15:44:23 +00001981 }
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001982
David Horstmannceeaeb92023-01-05 15:44:23 +00001983 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001984
David Horstmannceeaeb92023-01-05 15:44:23 +00001985 MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001986
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001987cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001988#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00001989 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
1990 ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
1991 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001992 rs_ctx->rsm->i = j;
David Horstmannceeaeb92023-01-05 15:44:23 +00001993 }
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001994 }
1995#endif
Janos Follathb0697532016-08-18 12:38:46 +01001996
David Horstmannceeaeb92023-01-05 15:44:23 +00001997 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001998}
1999
2000/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01002001 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002002 *
2003 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002004 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002005static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2006 const mbedtls_ecp_point T[], unsigned char T_size,
2007 unsigned char i)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002008{
Janos Follath24eed8d2019-11-22 13:21:35 +00002009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002010 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002011
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002012 /* Ignore the "sign" bit and scale down */
David Horstmannceeaeb92023-01-05 15:44:23 +00002013 ii = (i & 0x7Fu) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002014
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002015 /* Read the whole table to thwart cache-based timing attacks */
David Horstmannceeaeb92023-01-05 15:44:23 +00002016 for (j = 0; j < T_size; j++) {
2017 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&R->X, &T[j].X, j == ii));
2018 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&R->Y, &T[j].Y, j == ii));
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002019 }
2020
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01002021 /* Safely invert result if i is "negative" */
David Horstmannceeaeb92023-01-05 15:44:23 +00002022 MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, R, i >> 7));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002023
2024cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00002025 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002026}
2027
2028/*
2029 * Core multiplication algorithm for the (modified) comb method.
2030 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01002031 *
2032 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002033 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002034static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2035 const mbedtls_ecp_point T[], unsigned char T_size,
2036 const unsigned char x[], size_t d,
2037 int (*f_rng)(void *, unsigned char *, size_t),
2038 void *p_rng,
2039 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002040{
Janos Follath24eed8d2019-11-22 13:21:35 +00002041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002043 size_t i;
2044
David Horstmannceeaeb92023-01-05 15:44:23 +00002045 mbedtls_ecp_point_init(&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é-Gonnard3cade222017-04-20 09:31:00 +02002048 (void) rs_ctx;
2049#endif
2050
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002051#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002052 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
2053 rs_ctx->rsm->state != ecp_rsm_comb_core) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002054 rs_ctx->rsm->i = 0;
2055 rs_ctx->rsm->state = ecp_rsm_comb_core;
2056 }
2057
2058 /* new 'if' instead of nested for the sake of the 'else' branch */
David Horstmannceeaeb92023-01-05 15:44:23 +00002059 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002060 /* restore current index (R already pointing to rs_ctx->rsm->R) */
2061 i = rs_ctx->rsm->i;
David Horstmannceeaeb92023-01-05 15:44:23 +00002062 } else
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002063#endif
2064 {
2065 /* Start with a non-zero point and randomize its coordinates */
2066 i = d;
David Horstmannceeaeb92023-01-05 15:44:23 +00002067 MBEDTLS_MPI_CHK(ecp_select_comb(grp, R, T, T_size, x[i]));
2068 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->Z, 1));
David Horstmannef661c52022-10-26 17:55:53 +01002069
2070 int have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002071#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002072 if (f_rng == NULL) {
David Horstmannef661c52022-10-26 17:55:53 +01002073 have_rng = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00002074 }
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002075#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002076 if (have_rng) {
2077 MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, R, f_rng, p_rng));
2078 }
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002079 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002080
David Horstmannceeaeb92023-01-05 15:44:23 +00002081 while (i != 0) {
2082 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD);
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002083 --i;
2084
David Horstmannceeaeb92023-01-05 15:44:23 +00002085 MBEDTLS_MPI_CHK(ecp_double_jac(grp, R, R));
2086 MBEDTLS_MPI_CHK(ecp_select_comb(grp, &Txi, T, T_size, x[i]));
2087 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, R, R, &Txi));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002088 }
2089
2090cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002091
David Horstmannceeaeb92023-01-05 15:44:23 +00002092 mbedtls_ecp_point_free(&Txi);
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002093
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002094#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002095 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
2096 ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002097 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002098 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002099 }
2100#endif
2101
David Horstmannceeaeb92023-01-05 15:44:23 +00002102 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002103}
2104
2105/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002106 * Recode the scalar to get constant-time comb multiplication
2107 *
2108 * As the actual scalar recoding needs an odd scalar as a starting point,
2109 * this wrapper ensures that by replacing m by N - m if necessary, and
2110 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002111 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002112 * This works because we only support large prime order for Short Weierstrass
2113 * curves, so N is always odd hence either m or N - m is.
2114 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002115 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002116 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002117static int ecp_comb_recode_scalar(const mbedtls_ecp_group *grp,
2118 const mbedtls_mpi *m,
2119 unsigned char k[COMB_MAX_D + 1],
2120 size_t d,
2121 unsigned char w,
2122 unsigned char *parity_trick)
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +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 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002126
David Horstmannceeaeb92023-01-05 15:44:23 +00002127 mbedtls_mpi_init(&M);
2128 mbedtls_mpi_init(&mm);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002129
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002130 /* N is always odd (see above), just make extra sure */
David Horstmannceeaeb92023-01-05 15:44:23 +00002131 if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) {
2132 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2133 }
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002134
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002135 /* do we need the parity trick? */
David Horstmannceeaeb92023-01-05 15:44:23 +00002136 *parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0);
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002137
2138 /* execute parity fix in constant time */
David Horstmannceeaeb92023-01-05 15:44:23 +00002139 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m));
2140 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m));
2141 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick));
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002142
2143 /* actual scalar recoding */
David Horstmannceeaeb92023-01-05 15:44:23 +00002144 ecp_comb_recode_core(k, d, w, &M);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002145
2146cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00002147 mbedtls_mpi_free(&mm);
2148 mbedtls_mpi_free(&M);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002149
David Horstmannceeaeb92023-01-05 15:44:23 +00002150 return ret;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002151}
2152
2153/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002154 * Perform comb multiplication (for short Weierstrass curves)
2155 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002156 *
2157 * Scalar recoding may use a parity trick that makes us compute -m * P,
2158 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002159 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002160static int ecp_mul_comb_after_precomp(const mbedtls_ecp_group *grp,
2161 mbedtls_ecp_point *R,
2162 const mbedtls_mpi *m,
2163 const mbedtls_ecp_point *T,
2164 unsigned char T_size,
2165 unsigned char w,
2166 size_t d,
2167 int (*f_rng)(void *, unsigned char *, size_t),
2168 void *p_rng,
2169 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002170{
Janos Follath24eed8d2019-11-22 13:21:35 +00002171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002172 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002173 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002174 mbedtls_ecp_point *RR = R;
2175
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002176#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002177 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002178 RR = &rs_ctx->rsm->R;
2179
David Horstmannceeaeb92023-01-05 15:44:23 +00002180 if (rs_ctx->rsm->state == ecp_rsm_final_norm) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002181 goto final_norm;
David Horstmannceeaeb92023-01-05 15:44:23 +00002182 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002183 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002184#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002185
David Horstmannceeaeb92023-01-05 15:44:23 +00002186 MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w,
2187 &parity_trick));
2188 MBEDTLS_MPI_CHK(ecp_mul_comb_core(grp, RR, T, T_size, k, d,
2189 f_rng, p_rng, rs_ctx));
2190 MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, RR, parity_trick));
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002191
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002192#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002193 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002194 rs_ctx->rsm->state = ecp_rsm_final_norm;
David Horstmannceeaeb92023-01-05 15:44:23 +00002195 }
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002196
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002197final_norm:
David Horstmannceeaeb92023-01-05 15:44:23 +00002198 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002199#endif
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002200 /*
2201 * Knowledge of the jacobian coordinates may leak the last few bits of the
2202 * scalar [1], and since our MPI implementation isn't constant-flow,
2203 * inversion (used for coordinate normalization) may leak the full value
2204 * of its input via side-channels [2].
2205 *
2206 * [1] https://eprint.iacr.org/2003/191
2207 * [2] https://eprint.iacr.org/2020/055
2208 *
2209 * Avoid the leak by randomizing coordinates before we normalize them.
2210 */
David Horstmannef661c52022-10-26 17:55:53 +01002211 int have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002212#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002213 if (f_rng == NULL) {
David Horstmannef661c52022-10-26 17:55:53 +01002214 have_rng = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00002215 }
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002216#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002217 if (have_rng) {
2218 MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, RR, f_rng, p_rng));
2219 }
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002220
David Horstmannceeaeb92023-01-05 15:44:23 +00002221 MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, RR));
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002222
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002223#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002224 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
2225 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, RR));
2226 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002227#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002228
2229cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00002230 return ret;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002231}
2232
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002233/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002234 * Pick window size based on curve size and whether we optimize for base point
2235 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002236static unsigned char ecp_pick_window_size(const mbedtls_ecp_group *grp,
2237 unsigned char p_eq_g)
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002238{
2239 unsigned char w;
2240
2241 /*
2242 * Minimize the number of multiplications, that is minimize
2243 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2244 * (see costs of the various parts, with 1S = 1M)
2245 */
2246 w = grp->nbits >= 384 ? 5 : 4;
2247
2248 /*
2249 * If P == G, pre-compute a bit more, since this may be re-used later.
2250 * Just adding one avoids upping the cost of the first mul too much,
2251 * and the memory cost too.
2252 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002253 if (p_eq_g) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002254 w++;
David Horstmannceeaeb92023-01-05 15:44:23 +00002255 }
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002256
2257 /*
2258 * Make sure w is within bounds.
2259 * (The last test is useful only for very small curves in the test suite.)
2260 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002261#if (MBEDTLS_ECP_WINDOW_SIZE < 6)
2262 if (w > MBEDTLS_ECP_WINDOW_SIZE) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002263 w = MBEDTLS_ECP_WINDOW_SIZE;
David Horstmannceeaeb92023-01-05 15:44:23 +00002264 }
k-stachowiak653a4a22019-07-03 14:31:09 +02002265#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002266 if (w >= grp->nbits) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002267 w = 2;
David Horstmannceeaeb92023-01-05 15:44:23 +00002268 }
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002269
David Horstmannceeaeb92023-01-05 15:44:23 +00002270 return w;
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002271}
2272
2273/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002274 * Multiplication using the comb method - for curves in short Weierstrass form
2275 *
2276 * This function is mainly responsible for administrative work:
2277 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002278 * - managing the table of precomputed points (passed between the below two
Shaun Case0e7791f2021-12-20 21:14:10 -08002279 * functions): allocation, computation, ownership transfer, freeing.
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002280 *
2281 * It delegates the actual arithmetic work to:
2282 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2283 *
2284 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002285 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002286static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2287 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2288 int (*f_rng)(void *, unsigned char *, size_t),
2289 void *p_rng,
2290 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002291{
Janos Follath24eed8d2019-11-22 13:21:35 +00002292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002293 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002294 size_t d;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002295 unsigned char T_size = 0, T_ok = 0;
2296 mbedtls_ecp_point *T = NULL;
2297#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2298 ecp_drbg_context drbg_ctx;
2299
David Horstmannceeaeb92023-01-05 15:44:23 +00002300 ecp_drbg_init(&drbg_ctx);
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002301#endif
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002302
David Horstmannceeaeb92023-01-05 15:44:23 +00002303 ECP_RS_ENTER(rsm);
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002304
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002305#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002306 if (f_rng == NULL) {
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002307 /* Adjust pointers */
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002308 f_rng = &ecp_drbg_random;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002309#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002310 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002311 p_rng = &rs_ctx->rsm->drbg_ctx;
David Horstmannceeaeb92023-01-05 15:44:23 +00002312 } else
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002313#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002314 p_rng = &drbg_ctx;
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002315
2316 /* Initialize internal DRBG if necessary */
2317#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002318 if (rs_ctx == NULL || rs_ctx->rsm == NULL ||
2319 rs_ctx->rsm->drbg_seeded == 0)
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002320#endif
2321 {
David Horstmannceeaeb92023-01-05 15:44:23 +00002322 const size_t m_len = (grp->nbits + 7) / 8;
2323 MBEDTLS_MPI_CHK(ecp_drbg_seed(p_rng, m, m_len));
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002324 }
2325#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002326 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002327 rs_ctx->rsm->drbg_seeded = 1;
David Horstmannceeaeb92023-01-05 15:44:23 +00002328 }
Manuel Pégourié-Gonnard53fb66d2020-06-04 09:43:14 +02002329#endif
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002330 }
2331#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2332
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002333 /* Is P the base point ? */
2334#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
David Horstmannceeaeb92023-01-05 15:44:23 +00002335 p_eq_g = (mbedtls_mpi_cmp_mpi(&P->Y, &grp->G.Y) == 0 &&
2336 mbedtls_mpi_cmp_mpi(&P->X, &grp->G.X) == 0);
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002337#else
2338 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002339#endif
2340
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002341 /* Pick window size and deduce related sizes */
David Horstmannceeaeb92023-01-05 15:44:23 +00002342 w = ecp_pick_window_size(grp, p_eq_g);
2343 T_size = 1U << (w - 1);
2344 d = (grp->nbits + w - 1) / w;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002345
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002346 /* Pre-computed table: do we have it already for the base point? */
David Horstmannceeaeb92023-01-05 15:44:23 +00002347 if (p_eq_g && grp->T != NULL) {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002348 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002349 T = grp->T;
2350 T_ok = 1;
David Horstmannceeaeb92023-01-05 15:44:23 +00002351 } else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002352#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002353 /* Pre-computed table: do we have one in progress? complete? */
David Horstmannceeaeb92023-01-05 15:44:23 +00002354 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002355 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002356 T = rs_ctx->rsm->T;
2357 rs_ctx->rsm->T = NULL;
2358 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002359
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002360 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002361 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
David Horstmannceeaeb92023-01-05 15:44:23 +00002362 } else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002363#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002364 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002365 {
David Horstmannceeaeb92023-01-05 15:44:23 +00002366 T = mbedtls_calloc(T_size, sizeof(mbedtls_ecp_point));
2367 if (T == NULL) {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002368 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002369 goto cleanup;
2370 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002371
David Horstmannceeaeb92023-01-05 15:44:23 +00002372 for (i = 0; i < T_size; i++) {
2373 mbedtls_ecp_point_init(&T[i]);
2374 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002375
2376 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002377 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002378
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002379 /* Compute table (or finish computing it) if not done already */
David Horstmannceeaeb92023-01-05 15:44:23 +00002380 if (!T_ok) {
2381 MBEDTLS_MPI_CHK(ecp_precompute_comb(grp, T, P, w, d, rs_ctx));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002382
David Horstmannceeaeb92023-01-05 15:44:23 +00002383 if (p_eq_g) {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002384 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002385 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002386 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002387 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002388 }
2389 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002390
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002391 /* Actual comb multiplication using precomputed points */
David Horstmannceeaeb92023-01-05 15:44:23 +00002392 MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m,
2393 T, T_size, w, d,
2394 f_rng, p_rng, rs_ctx));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002395
2396cleanup:
2397
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002398#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002399 ecp_drbg_free(&drbg_ctx);
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002400#endif
2401
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002402 /* does T belong to the group? */
David Horstmannceeaeb92023-01-05 15:44:23 +00002403 if (T == grp->T) {
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002404 T = NULL;
David Horstmannceeaeb92023-01-05 15:44:23 +00002405 }
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002406
2407 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002408#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002409 if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002410 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002411 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002412 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002413 T = NULL;
2414 }
2415#endif
2416
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002417 /* did T belong to us? then let's destroy it! */
David Horstmannceeaeb92023-01-05 15:44:23 +00002418 if (T != NULL) {
2419 for (i = 0; i < T_size; i++) {
2420 mbedtls_ecp_point_free(&T[i]);
2421 }
2422 mbedtls_free(T);
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002423 }
2424
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002425 /* prevent caller from using invalid value */
David Horstmannceeaeb92023-01-05 15:44:23 +00002426 int should_free_R = (ret != 0);
David Horstmannb95ee002022-10-06 19:11:04 +01002427#if defined(MBEDTLS_ECP_RESTARTABLE)
2428 /* don't free R while in progress in case R == P */
David Horstmannceeaeb92023-01-05 15:44:23 +00002429 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
David Horstmanne9af9e32022-10-25 10:32:08 +01002430 should_free_R = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00002431 }
David Horstmannb95ee002022-10-06 19:11:04 +01002432#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002433 if (should_free_R) {
2434 mbedtls_ecp_point_free(R);
2435 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002436
David Horstmannceeaeb92023-01-05 15:44:23 +00002437 ECP_RS_LEAVE(rsm);
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002438
David Horstmannceeaeb92023-01-05 15:44:23 +00002439 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002440}
2441
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002442#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002443
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002444#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002445/*
2446 * For Montgomery curves, we do all the internal arithmetic in projective
2447 * coordinates. Import/export of points uses only the x coordinates, which is
Shaun Case0e7791f2021-12-20 21:14:10 -08002448 * internally represented as X / Z.
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002449 *
2450 * For scalar multiplication, we'll use a Montgomery ladder.
2451 */
2452
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002453/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002454 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2455 * Cost: 1M + 1I
2456 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002457static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002458{
Janos Follathb0697532016-08-18 12:38:46 +01002459#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002460 if (mbedtls_internal_ecp_grp_capable(grp)) {
2461 return mbedtls_internal_ecp_normalize_mxz(grp, P);
2462 }
Janos Follath372697b2016-10-28 16:53:11 +01002463#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002464
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002465#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002466 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002467#else
2468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00002469 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&P->Z, &P->Z, &grp->P));
2470 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->X, &P->X, &P->Z));
2471 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002472
2473cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00002474 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002475#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002476}
2477
2478/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002479 * Randomize projective x/z coordinates:
2480 * (X, Z) -> (l X, l Z) for random l
2481 * This is sort of the reverse operation of ecp_normalize_mxz().
2482 *
2483 * This countermeasure was first suggested in [2].
2484 * Cost: 2M
2485 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002486static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
2487 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002488{
Janos Follathb0697532016-08-18 12:38:46 +01002489#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002490 if (mbedtls_internal_ecp_grp_capable(grp)) {
2491 return mbedtls_internal_ecp_randomize_mxz(grp, P, f_rng, p_rng);
2492 }
Janos Follath372697b2016-10-28 16:53:11 +01002493#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002494
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002495#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002496 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002497#else
2498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2499 mbedtls_mpi l;
David Horstmannceeaeb92023-01-05 15:44:23 +00002500 mbedtls_mpi_init(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002501
2502 /* Generate l such that 1 < l < p */
David Horstmannceeaeb92023-01-05 15:44:23 +00002503 MBEDTLS_MPI_CHK(mbedtls_mpi_random(&l, 2, &grp->P, f_rng, p_rng));
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002504
David Horstmannceeaeb92023-01-05 15:44:23 +00002505 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->X, &P->X, &l));
2506 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->Z, &P->Z, &l));
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002507
2508cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00002509 mbedtls_mpi_free(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002510
David Horstmannceeaeb92023-01-05 15:44:23 +00002511 if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Gilles Peskine6466d342021-03-29 22:28:50 +02002512 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
David Horstmannceeaeb92023-01-05 15:44:23 +00002513 }
2514 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002515#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002516}
2517
2518/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002519 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2520 * for Montgomery curves in x/z coordinates.
2521 *
2522 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2523 * with
2524 * d = X1
2525 * P = (X2, Z2)
2526 * Q = (X3, Z3)
2527 * R = (X4, Z4)
2528 * S = (X5, Z5)
2529 * and eliminating temporary variables tO, ..., t4.
2530 *
2531 * Cost: 5M + 4S
2532 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002533static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
2534 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2535 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2536 const mbedtls_mpi *d)
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002537{
Janos Follathb0697532016-08-18 12:38:46 +01002538#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002539 if (mbedtls_internal_ecp_grp_capable(grp)) {
2540 return mbedtls_internal_ecp_double_add_mxz(grp, R, S, P, Q, d);
2541 }
Janos Follath372697b2016-10-28 16:53:11 +01002542#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002543
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002544#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002545 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002546#else
2547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2548 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
2549
David Horstmannceeaeb92023-01-05 15:44:23 +00002550 mbedtls_mpi_init(&A); mbedtls_mpi_init(&AA); mbedtls_mpi_init(&B);
2551 mbedtls_mpi_init(&BB); mbedtls_mpi_init(&E); mbedtls_mpi_init(&C);
2552 mbedtls_mpi_init(&D); mbedtls_mpi_init(&DA); mbedtls_mpi_init(&CB);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002553
David Horstmannceeaeb92023-01-05 15:44:23 +00002554 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &A, &P->X, &P->Z));
2555 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &AA, &A, &A));
2556 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &B, &P->X, &P->Z));
2557 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &BB, &B, &B));
2558 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &E, &AA, &BB));
2559 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &C, &Q->X, &Q->Z));
2560 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &D, &Q->X, &Q->Z));
2561 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &DA, &D, &A));
2562 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &CB, &C, &B));
2563 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &S->X, &DA, &CB));
2564 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->X, &S->X, &S->X));
2565 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S->Z, &DA, &CB));
2566 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->Z, &S->Z, &S->Z));
2567 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->Z, d, &S->Z));
2568 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->X, &AA, &BB));
2569 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->Z, &grp->A, &E));
2570 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &R->Z, &BB, &R->Z));
2571 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->Z, &E, &R->Z));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002572
2573cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00002574 mbedtls_mpi_free(&A); mbedtls_mpi_free(&AA); mbedtls_mpi_free(&B);
2575 mbedtls_mpi_free(&BB); mbedtls_mpi_free(&E); mbedtls_mpi_free(&C);
2576 mbedtls_mpi_free(&D); mbedtls_mpi_free(&DA); mbedtls_mpi_free(&CB);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002577
David Horstmannceeaeb92023-01-05 15:44:23 +00002578 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002579#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002580}
2581
2582/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002583 * Multiplication with Montgomery ladder in x/z coordinates,
2584 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002585 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002586static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2587 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2588 int (*f_rng)(void *, unsigned char *, size_t),
2589 void *p_rng)
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002590{
Janos Follath24eed8d2019-11-22 13:21:35 +00002591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002592 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002593 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 mbedtls_ecp_point RP;
2595 mbedtls_mpi PX;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002596#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
2597 ecp_drbg_context drbg_ctx;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002598
David Horstmannceeaeb92023-01-05 15:44:23 +00002599 ecp_drbg_init(&drbg_ctx);
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002600#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002601 mbedtls_ecp_point_init(&RP); mbedtls_mpi_init(&PX);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002602
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002603#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002604 if (f_rng == NULL) {
2605 const size_t m_len = (grp->nbits + 7) / 8;
2606 MBEDTLS_MPI_CHK(ecp_drbg_seed(&drbg_ctx, m, m_len));
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002607 f_rng = &ecp_drbg_random;
2608 p_rng = &drbg_ctx;
2609 }
2610#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
2611
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002612 /* Save PX and read from P before writing to R, in case P == R */
David Horstmannceeaeb92023-01-05 15:44:23 +00002613 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&PX, &P->X));
2614 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&RP, P));
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002615
2616 /* Set R to zero in modified x/z coordinates */
David Horstmannceeaeb92023-01-05 15:44:23 +00002617 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->X, 1));
2618 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->Z, 0));
2619 mbedtls_mpi_free(&R->Y);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002620
Shaun Case0e7791f2021-12-20 21:14:10 -08002621 /* RP.X might be slightly larger than P, so reduce it */
David Horstmannceeaeb92023-01-05 15:44:23 +00002622 MOD_ADD(RP.X);
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002623
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002624 /* Randomize coordinates of the starting point */
David Horstmannef661c52022-10-26 17:55:53 +01002625 int have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002626#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002627 if (f_rng == NULL) {
David Horstmannef661c52022-10-26 17:55:53 +01002628 have_rng = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00002629 }
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002630#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002631 if (have_rng) {
2632 MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, &RP, f_rng, p_rng));
2633 }
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002634
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002635 /* Loop invariant: R = result so far, RP = R + P */
Aurelien Jarnoedc110d2022-05-15 13:24:05 +02002636 i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */
David Horstmannceeaeb92023-01-05 15:44:23 +00002637 while (i-- > 0) {
2638 b = mbedtls_mpi_get_bit(m, i);
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002639 /*
2640 * if (b) R = 2R + P else R = 2R,
2641 * which is:
2642 * if (b) double_add( RP, R, RP, R )
2643 * else double_add( R, RP, R, RP )
2644 * but using safe conditional swaps to avoid leaks
2645 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002646 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->X, &RP.X, b));
2647 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->Z, &RP.Z, b));
2648 MBEDTLS_MPI_CHK(ecp_double_add_mxz(grp, R, &RP, R, &RP, &PX));
2649 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->X, &RP.X, b));
2650 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->Z, &RP.Z, b));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002651 }
2652
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002653 /*
2654 * Knowledge of the projective coordinates may leak the last few bits of the
2655 * scalar [1], and since our MPI implementation isn't constant-flow,
2656 * inversion (used for coordinate normalization) may leak the full value
2657 * of its input via side-channels [2].
2658 *
2659 * [1] https://eprint.iacr.org/2003/191
2660 * [2] https://eprint.iacr.org/2020/055
2661 *
2662 * Avoid the leak by randomizing coordinates before we normalize them.
2663 */
David Horstmannef661c52022-10-26 17:55:53 +01002664 have_rng = 1;
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002665#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002666 if (f_rng == NULL) {
David Horstmannef661c52022-10-26 17:55:53 +01002667 have_rng = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00002668 }
Manuel Pégourié-Gonnard22b1de32020-06-04 10:43:29 +02002669#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002670 if (have_rng) {
2671 MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, R, f_rng, p_rng));
2672 }
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002673
David Horstmannceeaeb92023-01-05 15:44:23 +00002674 MBEDTLS_MPI_CHK(ecp_normalize_mxz(grp, R));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002675
2676cleanup:
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002677#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +00002678 ecp_drbg_free(&drbg_ctx);
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002679#endif
2680
David Horstmannceeaeb92023-01-05 15:44:23 +00002681 mbedtls_ecp_point_free(&RP); mbedtls_mpi_free(&PX);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002682
David Horstmannceeaeb92023-01-05 15:44:23 +00002683 return ret;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002684}
2685
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002686#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002687
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002688/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002689 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002690 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002691int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2692 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2693 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2694 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002695{
Janos Follathb0697532016-08-18 12:38:46 +01002696 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002697#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2698 char is_grp_capable = 0;
2699#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002700 ECP_VALIDATE_RET(grp != NULL);
2701 ECP_VALIDATE_RET(R != NULL);
2702 ECP_VALIDATE_RET(m != NULL);
2703 ECP_VALIDATE_RET(P != NULL);
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002704
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002705#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002706 /* reset ops count for this call if top-level */
David Horstmannceeaeb92023-01-05 15:44:23 +00002707 if (rs_ctx != NULL && rs_ctx->depth++ == 0) {
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002708 rs_ctx->ops_done = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00002709 }
Gilles Peskine59970052019-02-28 13:12:06 +01002710#else
2711 (void) rs_ctx;
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002712#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002713
Janos Follathc44ab972016-11-18 16:38:23 +00002714#if defined(MBEDTLS_ECP_INTERNAL_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002715 if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
2716 MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
2717 }
Janos Follathc44ab972016-11-18 16:38:23 +00002718#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002719
David Horstmannb95ee002022-10-06 19:11:04 +01002720 int restarting = 0;
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002721#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002722 restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002723#endif
David Horstmannb95ee002022-10-06 19:11:04 +01002724 /* skip argument check when restarting */
David Horstmannceeaeb92023-01-05 15:44:23 +00002725 if (!restarting) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002726 /* check_privkey is free */
David Horstmannceeaeb92023-01-05 15:44:23 +00002727 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_CHK);
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002728
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002729 /* Common sanity checks */
David Horstmannceeaeb92023-01-05 15:44:23 +00002730 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m));
2731 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002732 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002733
2734 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002735#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00002736 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2737 MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng));
2738 }
Janos Follath430d3372016-11-03 14:25:37 +00002739#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002740#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00002741 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2742 MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx));
2743 }
Janos Follath430d3372016-11-03 14:25:37 +00002744#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002745
Janos Follath6c8ccd52016-11-29 15:37:09 +00002746cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002747
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002748#if defined(MBEDTLS_ECP_INTERNAL_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002749 if (is_grp_capable) {
2750 mbedtls_internal_ecp_free(grp);
2751 }
Janos Follathc44ab972016-11-18 16:38:23 +00002752#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002753
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002754#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002755 if (rs_ctx != NULL) {
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002756 rs_ctx->depth--;
David Horstmannceeaeb92023-01-05 15:44:23 +00002757 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002758#endif
2759
David Horstmannceeaeb92023-01-05 15:44:23 +00002760 return ret;
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002761}
2762
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002763/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002764 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002765 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002766int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2767 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2768 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002769{
David Horstmannceeaeb92023-01-05 15:44:23 +00002770 ECP_VALIDATE_RET(grp != NULL);
2771 ECP_VALIDATE_RET(R != NULL);
2772 ECP_VALIDATE_RET(m != NULL);
2773 ECP_VALIDATE_RET(P != NULL);
2774 return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002775}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002776
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002777#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002778/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002779 * Check that an affine point is valid as a public key,
2780 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002781 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002782static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002783{
Janos Follath24eed8d2019-11-22 13:21:35 +00002784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002786
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002787 /* pt coordinates must be normalized for our checks */
David Horstmannceeaeb92023-01-05 15:44:23 +00002788 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
2789 mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
2790 mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
2791 mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) {
2792 return MBEDTLS_ERR_ECP_INVALID_KEY;
2793 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002794
David Horstmannceeaeb92023-01-05 15:44:23 +00002795 mbedtls_mpi_init(&YY); mbedtls_mpi_init(&RHS);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002796
2797 /*
2798 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002799 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002800 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002801 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &YY, &pt->Y, &pt->Y));
2802 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &RHS, &pt->X, &pt->X));
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002803
2804 /* Special case for A = -3 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002805 if (grp->A.p == NULL) {
2806 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&RHS, &RHS, 3)); MOD_SUB(RHS);
2807 } else {
2808 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &RHS, &RHS, &grp->A));
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002809 }
2810
David Horstmannceeaeb92023-01-05 15:44:23 +00002811 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &RHS, &RHS, &pt->X));
2812 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &RHS, &RHS, &grp->B));
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002813
David Horstmannceeaeb92023-01-05 15:44:23 +00002814 if (mbedtls_mpi_cmp_mpi(&YY, &RHS) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
David Horstmannceeaeb92023-01-05 15:44:23 +00002816 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002817
2818cleanup:
2819
David Horstmannceeaeb92023-01-05 15:44:23 +00002820 mbedtls_mpi_free(&YY); mbedtls_mpi_free(&RHS);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002821
David Horstmannceeaeb92023-01-05 15:44:23 +00002822 return ret;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002823}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002824#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002825
Gilles Peskine9b99a892018-09-14 18:32:19 +02002826#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002827/*
TRodziewicz9edff742021-03-04 17:59:39 +01002828 * R = m * P with shortcuts for m == 0, m == 1 and m == -1
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002829 * NOT constant-time - ONLY for short Weierstrass!
2830 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002831static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
2832 mbedtls_ecp_point *R,
2833 const mbedtls_mpi *m,
2834 const mbedtls_ecp_point *P,
2835 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002836{
Janos Follath24eed8d2019-11-22 13:21:35 +00002837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002838
David Horstmannceeaeb92023-01-05 15:44:23 +00002839 if (mbedtls_mpi_cmp_int(m, 0) == 0) {
2840 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2841 MBEDTLS_MPI_CHK(mbedtls_ecp_set_zero(R));
2842 } else if (mbedtls_mpi_cmp_int(m, 1) == 0) {
2843 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2844 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2845 } else if (mbedtls_mpi_cmp_int(m, -1) == 0) {
2846 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2847 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2848 if (mbedtls_mpi_cmp_int(&R->Y, 0) != 0) {
2849 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&R->Y, &grp->P, &R->Y));
2850 }
2851 } else {
2852 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, R, m, P,
2853 NULL, NULL, rs_ctx));
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002854 }
2855
2856cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00002857 return ret;
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002858}
2859
2860/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002861 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002862 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002863 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002864int mbedtls_ecp_muladd_restartable(
David Horstmannceeaeb92023-01-05 15:44:23 +00002865 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2866 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2867 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2868 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002869{
Janos Follath24eed8d2019-11-22 13:21:35 +00002870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002871 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002872 mbedtls_ecp_point *pmP = &mP;
2873 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002874#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2875 char is_grp_capable = 0;
2876#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002877 ECP_VALIDATE_RET(grp != NULL);
2878 ECP_VALIDATE_RET(R != NULL);
2879 ECP_VALIDATE_RET(m != NULL);
2880 ECP_VALIDATE_RET(P != NULL);
2881 ECP_VALIDATE_RET(n != NULL);
2882 ECP_VALIDATE_RET(Q != NULL);
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002883
David Horstmannceeaeb92023-01-05 15:44:23 +00002884 if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2885 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
2886 }
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002887
David Horstmannceeaeb92023-01-05 15:44:23 +00002888 mbedtls_ecp_point_init(&mP);
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002889
David Horstmannceeaeb92023-01-05 15:44:23 +00002890 ECP_RS_ENTER(ma);
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002891
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002892#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002893 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002894 /* redirect intermediate results to restart context */
2895 pmP = &rs_ctx->ma->mP;
2896 pR = &rs_ctx->ma->R;
2897
2898 /* jump to next operation */
David Horstmannceeaeb92023-01-05 15:44:23 +00002899 if (rs_ctx->ma->state == ecp_rsma_mul2) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002900 goto mul2;
David Horstmannceeaeb92023-01-05 15:44:23 +00002901 }
2902 if (rs_ctx->ma->state == ecp_rsma_add) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002903 goto add;
David Horstmannceeaeb92023-01-05 15:44:23 +00002904 }
2905 if (rs_ctx->ma->state == ecp_rsma_norm) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002906 goto norm;
David Horstmannceeaeb92023-01-05 15:44:23 +00002907 }
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002908 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002909#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002910
David Horstmannceeaeb92023-01-05 15:44:23 +00002911 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx));
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002912#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002913 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002914 rs_ctx->ma->state = ecp_rsma_mul2;
David Horstmannceeaeb92023-01-05 15:44:23 +00002915 }
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002916
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002917mul2:
2918#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002919 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pR, n, Q, rs_ctx));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002920
2921#if defined(MBEDTLS_ECP_INTERNAL_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002922 if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
2923 MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
2924 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002925#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2926
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002927#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002928 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002929 rs_ctx->ma->state = ecp_rsma_add;
David Horstmannceeaeb92023-01-05 15:44:23 +00002930 }
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002931
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002932add:
2933#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002934 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_ADD);
2935 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, pR, pmP, pR));
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002936#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002937 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002938 rs_ctx->ma->state = ecp_rsma_norm;
David Horstmannceeaeb92023-01-05 15:44:23 +00002939 }
Janos Follath430d3372016-11-03 14:25:37 +00002940
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002941norm:
2942#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00002943 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
2944 MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, pR));
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002945
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002946#if defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmannceeaeb92023-01-05 15:44:23 +00002947 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2948 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, pR));
2949 }
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002950#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002951
2952cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002953#if defined(MBEDTLS_ECP_INTERNAL_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +00002954 if (is_grp_capable) {
2955 mbedtls_internal_ecp_free(grp);
2956 }
Janos Follathc44ab972016-11-18 16:38:23 +00002957#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002958
David Horstmannceeaeb92023-01-05 15:44:23 +00002959 mbedtls_ecp_point_free(&mP);
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002960
David Horstmannceeaeb92023-01-05 15:44:23 +00002961 ECP_RS_LEAVE(ma);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002962
David Horstmannceeaeb92023-01-05 15:44:23 +00002963 return ret;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002964}
2965
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002966/*
2967 * Linear combination
2968 * NOT constant-time
2969 */
David Horstmannceeaeb92023-01-05 15:44:23 +00002970int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2971 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2972 const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002973{
David Horstmannceeaeb92023-01-05 15:44:23 +00002974 ECP_VALIDATE_RET(grp != NULL);
2975 ECP_VALIDATE_RET(R != NULL);
2976 ECP_VALIDATE_RET(m != NULL);
2977 ECP_VALIDATE_RET(P != NULL);
2978 ECP_VALIDATE_RET(n != NULL);
2979 ECP_VALIDATE_RET(Q != NULL);
2980 return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002981}
Gilles Peskine9b99a892018-09-14 18:32:19 +02002982#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002983
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002984#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02002985#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00002986#define ECP_MPI_INIT(s, n, p) { s, (n), (mbedtls_mpi_uint *) (p) }
Manuel Pégourié-Gonnard520f0a02021-06-23 12:53:18 +02002987#define ECP_MPI_INIT_ARRAY(x) \
2988 ECP_MPI_INIT(1, sizeof(x) / sizeof(mbedtls_mpi_uint), x)
2989/*
2990 * Constants for the two points other than 0, 1, -1 (mod p) in
2991 * https://cr.yp.to/ecdh.html#validate
2992 * See ecp_check_pubkey_x25519().
2993 */
2994static const mbedtls_mpi_uint x25519_bad_point_1[] = {
David Horstmannceeaeb92023-01-05 15:44:23 +00002995 MBEDTLS_BYTES_TO_T_UINT_8(0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae),
2996 MBEDTLS_BYTES_TO_T_UINT_8(0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a),
2997 MBEDTLS_BYTES_TO_T_UINT_8(0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd),
2998 MBEDTLS_BYTES_TO_T_UINT_8(0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00),
Manuel Pégourié-Gonnard520f0a02021-06-23 12:53:18 +02002999};
3000static const mbedtls_mpi_uint x25519_bad_point_2[] = {
David Horstmannceeaeb92023-01-05 15:44:23 +00003001 MBEDTLS_BYTES_TO_T_UINT_8(0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24),
3002 MBEDTLS_BYTES_TO_T_UINT_8(0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b),
3003 MBEDTLS_BYTES_TO_T_UINT_8(0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86),
3004 MBEDTLS_BYTES_TO_T_UINT_8(0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57),
Manuel Pégourié-Gonnard520f0a02021-06-23 12:53:18 +02003005};
3006static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
David Horstmannceeaeb92023-01-05 15:44:23 +00003007 x25519_bad_point_1);
Manuel Pégourié-Gonnard520f0a02021-06-23 12:53:18 +02003008static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
David Horstmannceeaeb92023-01-05 15:44:23 +00003009 x25519_bad_point_2);
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003010#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
Manuel Pégourié-Gonnard10b8e5a2021-06-23 12:25:48 +02003011
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003012/*
3013 * Check that the input point is not one of the low-order points.
3014 * This is recommended by the "May the Fourth" paper:
3015 * https://eprint.iacr.org/2017/806.pdf
3016 * Those points are never sent by an honest peer.
3017 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003018static int ecp_check_bad_points_mx(const mbedtls_mpi *X, const mbedtls_mpi *P,
3019 const mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003020{
3021 int ret;
Manuel Pégourié-Gonnard10b8e5a2021-06-23 12:25:48 +02003022 mbedtls_mpi XmP;
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003023
David Horstmannceeaeb92023-01-05 15:44:23 +00003024 mbedtls_mpi_init(&XmP);
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003025
3026 /* Reduce X mod P so that we only need to check values less than P.
3027 * We know X < 2^256 so we can proceed by subtraction. */
David Horstmannceeaeb92023-01-05 15:44:23 +00003028 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&XmP, X));
3029 while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) {
3030 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&XmP, &XmP, P));
3031 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003032
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003033 /* Check against the known bad values that are less than P. For Curve448
3034 * these are 0, 1 and -1. For Curve25519 we check the values less than P
3035 * from the following list: https://cr.yp.to/ecdh.html#validate */
David Horstmannceeaeb92023-01-05 15:44:23 +00003036 if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) { /* takes care of 0 and 1 */
Janos Follathb4c676e2021-06-24 14:24:13 +01003037 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3038 goto cleanup;
3039 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003040
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003041#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003042 if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
3043 if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) {
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003044 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3045 goto cleanup;
3046 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003047
David Horstmannceeaeb92023-01-05 15:44:23 +00003048 if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) {
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003049 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3050 goto cleanup;
3051 }
Janos Follathb4c676e2021-06-24 14:24:13 +01003052 }
Janos Follath2667fb72021-06-25 15:29:56 +01003053#else
3054 (void) grp_id;
Janos Follath7d4ebdd2021-06-24 15:34:59 +01003055#endif
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003056
Manuel Pégourié-Gonnard10b8e5a2021-06-23 12:25:48 +02003057 /* Final check: check if XmP + 1 is P (final because it changes XmP!) */
David Horstmannceeaeb92023-01-05 15:44:23 +00003058 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&XmP, &XmP, 1));
3059 if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) {
Janos Follathb4c676e2021-06-24 14:24:13 +01003060 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3061 goto cleanup;
3062 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003063
3064 ret = 0;
3065
3066cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00003067 mbedtls_mpi_free(&XmP);
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003068
David Horstmannceeaeb92023-01-05 15:44:23 +00003069 return ret;
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003070}
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003071
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003072/*
3073 * Check validity of a public key for Montgomery curves with x-only schemes
3074 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003075static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003076{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02003077 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003078 /* Allow any public value, if it's too big then we'll just reduce it mod p
3079 * (RFC 7748 sec. 5 para. 3). */
David Horstmannceeaeb92023-01-05 15:44:23 +00003080 if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) {
3081 return MBEDTLS_ERR_ECP_INVALID_KEY;
3082 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003083
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003084 /* Implicit in all standards (as they don't consider negative numbers):
3085 * X must be non-negative. This is normally ensured by the way it's
3086 * encoded for transmission, but let's be extra sure. */
David Horstmannceeaeb92023-01-05 15:44:23 +00003087 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) {
3088 return MBEDTLS_ERR_ECP_INVALID_KEY;
3089 }
Manuel Pégourié-Gonnardf2268d12021-06-23 10:14:58 +02003090
David Horstmannceeaeb92023-01-05 15:44:23 +00003091 return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id);
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003092}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003093#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003094
3095/*
3096 * Check that a point is valid as a public key
3097 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003098int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
3099 const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003100{
David Horstmannceeaeb92023-01-05 15:44:23 +00003101 ECP_VALIDATE_RET(grp != NULL);
3102 ECP_VALIDATE_RET(pt != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003103
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003104 /* Must use affine coordinates */
David Horstmannceeaeb92023-01-05 15:44:23 +00003105 if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) {
3106 return MBEDTLS_ERR_ECP_INVALID_KEY;
3107 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003108
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003109#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003110 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3111 return ecp_check_pubkey_mx(grp, pt);
3112 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003113#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003114#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003115 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3116 return ecp_check_pubkey_sw(grp, pt);
3117 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003118#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00003119 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003120}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003121
3122/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003124 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003125int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
3126 const mbedtls_mpi *d)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003127{
David Horstmannceeaeb92023-01-05 15:44:23 +00003128 ECP_VALIDATE_RET(grp != NULL);
3129 ECP_VALIDATE_RET(d != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003130
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003131#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003132 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003133 /* see RFC 7748 sec. 5 para. 5 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003134 if (mbedtls_mpi_get_bit(d, 0) != 0 ||
3135 mbedtls_mpi_get_bit(d, 1) != 0 ||
3136 mbedtls_mpi_bitlen(d) - 1 != grp->nbits) { /* mbedtls_mpi_bitlen is one-based! */
3137 return MBEDTLS_ERR_ECP_INVALID_KEY;
3138 }
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003139
3140 /* see [Curve25519] page 5 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003141 if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) {
3142 return MBEDTLS_ERR_ECP_INVALID_KEY;
3143 }
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003144
David Horstmannceeaeb92023-01-05 15:44:23 +00003145 return 0;
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003146 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003147#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3148#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003149 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003150 /* see SEC1 3.2 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003151 if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
3152 mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
3153 return MBEDTLS_ERR_ECP_INVALID_KEY;
3154 } else {
3155 return 0;
3156 }
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003157 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003158#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003159
David Horstmannceeaeb92023-01-05 15:44:23 +00003160 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003161}
3162
Gilles Peskinede332132021-03-23 22:31:31 +01003163#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3164MBEDTLS_STATIC_TESTABLE
David Horstmannceeaeb92023-01-05 15:44:23 +00003165int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
3166 mbedtls_mpi *d,
3167 int (*f_rng)(void *, unsigned char *, size_t),
3168 void *p_rng)
Gilles Peskinede332132021-03-23 22:31:31 +01003169{
3170 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskineeadf31d2021-03-24 12:46:46 +01003171 size_t n_random_bytes = high_bit / 8 + 1;
Gilles Peskinede332132021-03-23 22:31:31 +01003172
3173 /* [Curve25519] page 5 */
Gilles Peskineeadf31d2021-03-24 12:46:46 +01003174 /* Generate a (high_bit+1)-bit random number by generating just enough
3175 * random bytes, then shifting out extra bits from the top (necessary
3176 * when (high_bit+1) is not a multiple of 8). */
David Horstmannceeaeb92023-01-05 15:44:23 +00003177 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(d, n_random_bytes,
3178 f_rng, p_rng));
3179 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1));
Gilles Peskinede332132021-03-23 22:31:31 +01003180
David Horstmannceeaeb92023-01-05 15:44:23 +00003181 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, high_bit, 1));
Gilles Peskinede332132021-03-23 22:31:31 +01003182
3183 /* Make sure the last two bits are unset for Curve448, three bits for
3184 Curve25519 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003185 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 0, 0));
3186 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 1, 0));
3187 if (high_bit == 254) {
3188 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 2, 0));
Gilles Peskinede332132021-03-23 22:31:31 +01003189 }
3190
3191cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00003192 return ret;
Gilles Peskinede332132021-03-23 22:31:31 +01003193}
3194#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3195
Gilles Peskineaeab0fb2021-03-29 22:28:21 +02003196#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3197static int mbedtls_ecp_gen_privkey_sw(
3198 const mbedtls_mpi *N, mbedtls_mpi *d,
David Horstmannceeaeb92023-01-05 15:44:23 +00003199 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskineaeab0fb2021-03-29 22:28:21 +02003200{
David Horstmannceeaeb92023-01-05 15:44:23 +00003201 int ret = mbedtls_mpi_random(d, 1, N, f_rng, p_rng);
3202 switch (ret) {
Gilles Peskineaeab0fb2021-03-29 22:28:21 +02003203 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
David Horstmannceeaeb92023-01-05 15:44:23 +00003204 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Gilles Peskineaeab0fb2021-03-29 22:28:21 +02003205 default:
David Horstmannceeaeb92023-01-05 15:44:23 +00003206 return ret;
Gilles Peskineaeab0fb2021-03-29 22:28:21 +02003207 }
3208}
3209#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3210
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003211/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003212 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003213 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003214int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
3215 mbedtls_mpi *d,
3216 int (*f_rng)(void *, unsigned char *, size_t),
3217 void *p_rng)
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003218{
David Horstmannceeaeb92023-01-05 15:44:23 +00003219 ECP_VALIDATE_RET(grp != NULL);
3220 ECP_VALIDATE_RET(d != NULL);
3221 ECP_VALIDATE_RET(f_rng != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003222
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003223#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003224 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3225 return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng);
3226 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003227#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003228
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003229#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003230 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3231 return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng);
3232 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003233#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003234
David Horstmannceeaeb92023-01-05 15:44:23 +00003235 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003236}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01003237
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003238/*
3239 * Generate a keypair with configurable base point
3240 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003241int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
3242 const mbedtls_ecp_point *G,
3243 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3244 int (*f_rng)(void *, unsigned char *, size_t),
3245 void *p_rng)
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003246{
Janos Follath24eed8d2019-11-22 13:21:35 +00003247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00003248 ECP_VALIDATE_RET(grp != NULL);
3249 ECP_VALIDATE_RET(d != NULL);
3250 ECP_VALIDATE_RET(G != NULL);
3251 ECP_VALIDATE_RET(Q != NULL);
3252 ECP_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003253
David Horstmannceeaeb92023-01-05 15:44:23 +00003254 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
3255 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, Q, d, G, f_rng, p_rng));
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003256
3257cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00003258 return ret;
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003259}
3260
3261/*
3262 * Generate key pair, wrapper for conventional base point
3263 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003264int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
3265 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3266 int (*f_rng)(void *, unsigned char *, size_t),
3267 void *p_rng)
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003268{
David Horstmannceeaeb92023-01-05 15:44:23 +00003269 ECP_VALIDATE_RET(grp != NULL);
3270 ECP_VALIDATE_RET(d != NULL);
3271 ECP_VALIDATE_RET(Q != NULL);
3272 ECP_VALIDATE_RET(f_rng != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003273
David Horstmannceeaeb92023-01-05 15:44:23 +00003274 return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003275}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003276
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003277/*
3278 * Generate a keypair, prettier wrapper
3279 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003280int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3281 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003282{
Janos Follath24eed8d2019-11-22 13:21:35 +00003283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmannceeaeb92023-01-05 15:44:23 +00003284 ECP_VALIDATE_RET(key != NULL);
3285 ECP_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003286
David Horstmannceeaeb92023-01-05 15:44:23 +00003287 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3288 return ret;
3289 }
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003290
David Horstmannceeaeb92023-01-05 15:44:23 +00003291 return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng);
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003292}
3293
Janos Follath77800962019-02-25 16:32:08 +00003294#define ECP_CURVE25519_KEY_SIZE 32
Janos Follath171a7ef2019-02-15 16:17:45 +00003295/*
3296 * Read a private key.
3297 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003298int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3299 const unsigned char *buf, size_t buflen)
Janos Follath171a7ef2019-02-15 16:17:45 +00003300{
3301 int ret = 0;
3302
David Horstmannceeaeb92023-01-05 15:44:23 +00003303 ECP_VALIDATE_RET(key != NULL);
3304 ECP_VALIDATE_RET(buf != NULL);
Janos Follath28eb06d2019-02-26 10:53:34 +00003305
David Horstmannceeaeb92023-01-05 15:44:23 +00003306 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3307 return ret;
3308 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003309
Janos Follath28eb06d2019-02-26 10:53:34 +00003310 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3311
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003312#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003313 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Janos Follath28eb06d2019-02-26 10:53:34 +00003314 /*
3315 * If it is Curve25519 curve then mask the key as mandated by RFC7748
3316 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003317 if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
3318 if (buflen != ECP_CURVE25519_KEY_SIZE) {
Janos Follath28eb06d2019-02-26 10:53:34 +00003319 return MBEDTLS_ERR_ECP_INVALID_KEY;
David Horstmannceeaeb92023-01-05 15:44:23 +00003320 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003321
David Horstmannceeaeb92023-01-05 15:44:23 +00003322 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
Janos Follath171a7ef2019-02-15 16:17:45 +00003323
Janos Follath28eb06d2019-02-26 10:53:34 +00003324 /* Set the three least significant bits to 0 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003325 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
3326 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
3327 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0));
Janos Follath171a7ef2019-02-15 16:17:45 +00003328
Janos Follath28eb06d2019-02-26 10:53:34 +00003329 /* Set the most significant bit to 0 */
3330 MBEDTLS_MPI_CHK(
David Horstmannceeaeb92023-01-05 15:44:23 +00003331 mbedtls_mpi_set_bit(&key->d,
3332 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0)
3333 );
Janos Follath171a7ef2019-02-15 16:17:45 +00003334
Janos Follath28eb06d2019-02-26 10:53:34 +00003335 /* Set the second most significant bit to 1 */
3336 MBEDTLS_MPI_CHK(
David Horstmannceeaeb92023-01-05 15:44:23 +00003337 mbedtls_mpi_set_bit(&key->d,
3338 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1)
3339 );
3340 } else {
Janos Follath28eb06d2019-02-26 10:53:34 +00003341 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
David Horstmannceeaeb92023-01-05 15:44:23 +00003342 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003343 }
3344
3345#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003346#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003347 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3348 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen));
Janos Follath171a7ef2019-02-15 16:17:45 +00003349
David Horstmannceeaeb92023-01-05 15:44:23 +00003350 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d));
Janos Follath171a7ef2019-02-15 16:17:45 +00003351 }
3352
3353#endif
3354cleanup:
3355
David Horstmannceeaeb92023-01-05 15:44:23 +00003356 if (ret != 0) {
3357 mbedtls_mpi_free(&key->d);
3358 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003359
David Horstmannceeaeb92023-01-05 15:44:23 +00003360 return ret;
Janos Follath171a7ef2019-02-15 16:17:45 +00003361}
3362
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003363/*
Steven Cooremande8593f2020-06-09 19:55:26 +02003364 * Write a private key.
3365 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003366int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
3367 unsigned char *buf, size_t buflen)
Steven Cooremande8593f2020-06-09 19:55:26 +02003368{
Steven Cooreman0024df62020-07-13 10:59:40 +02003369 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooremande8593f2020-06-09 19:55:26 +02003370
David Horstmannceeaeb92023-01-05 15:44:23 +00003371 ECP_VALIDATE_RET(key != NULL);
3372 ECP_VALIDATE_RET(buf != NULL);
Steven Cooremande8593f2020-06-09 19:55:26 +02003373
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003374#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003375 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3376 if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) {
3377 if (buflen < ECP_CURVE25519_KEY_SIZE) {
Steven Cooremande8593f2020-06-09 19:55:26 +02003378 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
David Horstmannceeaeb92023-01-05 15:44:23 +00003379 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003380
David Horstmannceeaeb92023-01-05 15:44:23 +00003381 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen));
3382 } else {
Steven Cooremande8593f2020-06-09 19:55:26 +02003383 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
David Horstmannceeaeb92023-01-05 15:44:23 +00003384 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003385 }
3386
3387#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003388#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003389 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3390 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen));
Steven Cooremande8593f2020-06-09 19:55:26 +02003391 }
3392
3393#endif
3394cleanup:
3395
David Horstmannceeaeb92023-01-05 15:44:23 +00003396 return ret;
Steven Cooremande8593f2020-06-09 19:55:26 +02003397}
3398
3399
3400/*
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003401 * Check a public-private key pair
3402 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003403int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv)
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003404{
Janos Follath24eed8d2019-11-22 13:21:35 +00003405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 mbedtls_ecp_point Q;
3407 mbedtls_ecp_group grp;
David Horstmannceeaeb92023-01-05 15:44:23 +00003408 ECP_VALIDATE_RET(pub != NULL);
3409 ECP_VALIDATE_RET(prv != NULL);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003410
David Horstmannceeaeb92023-01-05 15:44:23 +00003411 if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003412 pub->grp.id != prv->grp.id ||
David Horstmannceeaeb92023-01-05 15:44:23 +00003413 mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
3414 mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
3415 mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
3416 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003417 }
3418
David Horstmannceeaeb92023-01-05 15:44:23 +00003419 mbedtls_ecp_point_init(&Q);
3420 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 /* mbedtls_ecp_mul() needs a non-const group... */
David Horstmannceeaeb92023-01-05 15:44:23 +00003423 mbedtls_ecp_group_copy(&grp, &prv->grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003424
3425 /* Also checks d is valid */
David Horstmannceeaeb92023-01-05 15:44:23 +00003426 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, NULL, NULL));
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003427
David Horstmannceeaeb92023-01-05 15:44:23 +00003428 if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
3429 mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
3430 mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003432 goto cleanup;
3433 }
3434
3435cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00003436 mbedtls_ecp_point_free(&Q);
3437 mbedtls_ecp_group_free(&grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003438
David Horstmannceeaeb92023-01-05 15:44:23 +00003439 return ret;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003440}
3441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003443
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003444/* Adjust the exponent to be a valid private point for the specified curve.
3445 * This is sometimes necessary because we use a single set of exponents
3446 * for all curves but the validity of values depends on the curve. */
David Horstmannceeaeb92023-01-05 15:44:23 +00003447static int self_test_adjust_exponent(const mbedtls_ecp_group *grp,
3448 mbedtls_mpi *m)
Gilles Peskinea088c812018-09-17 18:31:15 +02003449{
3450 int ret = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +00003451 switch (grp->id) {
3452 /* If Curve25519 is available, then that's what we use for the
3453 * Montgomery test, so we don't need the adjustment code. */
3454#if !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Gilles Peskinea088c812018-09-17 18:31:15 +02003455#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3456 case MBEDTLS_ECP_DP_CURVE448:
3457 /* Move highest bit from 254 to N-1. Setting bit N-1 is
3458 * necessary to enforce the highest-bit-set constraint. */
David Horstmannceeaeb92023-01-05 15:44:23 +00003459 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0));
3460 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1));
Gilles Peskinea088c812018-09-17 18:31:15 +02003461 /* Copy second-highest bit from 253 to N-2. This is not
3462 * necessary but improves the test variety a bit. */
3463 MBEDTLS_MPI_CHK(
David Horstmannceeaeb92023-01-05 15:44:23 +00003464 mbedtls_mpi_set_bit(m, grp->nbits - 1,
3465 mbedtls_mpi_get_bit(m, 253)));
Gilles Peskinea088c812018-09-17 18:31:15 +02003466 break;
3467#endif
3468#endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */
3469 default:
3470 /* Non-Montgomery curves and Curve25519 need no adjustment. */
3471 (void) grp;
3472 (void) m;
3473 goto cleanup;
3474 }
3475cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00003476 return ret;
Gilles Peskinea088c812018-09-17 18:31:15 +02003477}
3478
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003479/* Calculate R = m.P for each m in exponents. Check that the number of
3480 * basic operations doesn't depend on the value of m. */
David Horstmannceeaeb92023-01-05 15:44:23 +00003481static int self_test_point(int verbose,
3482 mbedtls_ecp_group *grp,
3483 mbedtls_ecp_point *R,
3484 mbedtls_mpi *m,
3485 const mbedtls_ecp_point *P,
3486 const char *const *exponents,
3487 size_t n_exponents)
Gilles Peskinec95696f2018-09-17 15:59:01 +02003488{
3489 int ret = 0;
Gilles Peskine24666792018-09-17 18:29:49 +02003490 size_t i = 0;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003491 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
3492 add_count = 0;
3493 dbl_count = 0;
3494 mul_count = 0;
Gilles Peskinea088c812018-09-17 18:31:15 +02003495
David Horstmannceeaeb92023-01-05 15:44:23 +00003496 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0]));
3497 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3498 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, NULL, NULL));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003499
David Horstmannceeaeb92023-01-05 15:44:23 +00003500 for (i = 1; i < n_exponents; i++) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003501 add_c_prev = add_count;
3502 dbl_c_prev = dbl_count;
3503 mul_c_prev = mul_count;
3504 add_count = 0;
3505 dbl_count = 0;
3506 mul_count = 0;
3507
David Horstmannceeaeb92023-01-05 15:44:23 +00003508 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i]));
3509 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3510 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, NULL, NULL));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003511
David Horstmannceeaeb92023-01-05 15:44:23 +00003512 if (add_count != add_c_prev ||
Gilles Peskinec95696f2018-09-17 15:59:01 +02003513 dbl_count != dbl_c_prev ||
David Horstmannceeaeb92023-01-05 15:44:23 +00003514 mul_count != mul_c_prev) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003515 ret = 1;
3516 break;
3517 }
3518 }
3519
3520cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +00003521 if (verbose != 0) {
3522 if (ret != 0) {
3523 mbedtls_printf("failed (%u)\n", (unsigned int) i);
3524 } else {
3525 mbedtls_printf("passed\n");
3526 }
Gilles Peskinec95696f2018-09-17 15:59:01 +02003527 }
David Horstmannceeaeb92023-01-05 15:44:23 +00003528 return ret;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003529}
3530
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003531/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003532 * Checkup routine
3533 */
David Horstmannceeaeb92023-01-05 15:44:23 +00003534int mbedtls_ecp_self_test(int verbose)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003535{
Janos Follath24eed8d2019-11-22 13:21:35 +00003536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 mbedtls_ecp_group grp;
3538 mbedtls_ecp_point R, P;
3539 mbedtls_mpi m;
Gilles Peskine24666792018-09-17 18:29:49 +02003540
3541#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskined9767a52018-09-14 19:29:47 +02003542 /* Exponents especially adapted for secp192k1, which has the lowest
3543 * order n of all supported curves (secp192r1 is in a slightly larger
3544 * field but the order of its base point is slightly smaller). */
Gilles Peskine24666792018-09-17 18:29:49 +02003545 const char *sw_exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003546 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003547 "000000000000000000000000000000000000000000000001", /* one */
Gilles Peskined9767a52018-09-14 19:29:47 +02003548 "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003549 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003550 "400000000000000000000000000000000000000000000000", /* one and zeros */
3551 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3552 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003553 };
Gilles Peskine24666792018-09-17 18:29:49 +02003554#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3555#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3556 const char *m_exponents[] =
3557 {
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003558 /* Valid private values for Curve25519. In a build with Curve448
3559 * but not Curve25519, they will be adjusted in
3560 * self_test_adjust_exponent(). */
Gilles Peskine24666792018-09-17 18:29:49 +02003561 "4000000000000000000000000000000000000000000000000000000000000000",
3562 "5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
3563 "5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
3564 "41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
3565 "5555555555555555555555555555555555555555555555555555555555555550",
3566 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
3567 };
3568#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003569
David Horstmannceeaeb92023-01-05 15:44:23 +00003570 mbedtls_ecp_group_init(&grp);
3571 mbedtls_ecp_point_init(&R);
3572 mbedtls_ecp_point_init(&P);
3573 mbedtls_mpi_init(&m);
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003574
Gilles Peskine24666792018-09-17 18:29:49 +02003575#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003576 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003578 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP192R1));
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003579#else
David Horstmannceeaeb92023-01-05 15:44:23 +00003580 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id));
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003581#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003582
David Horstmannceeaeb92023-01-05 15:44:23 +00003583 if (verbose != 0) {
3584 mbedtls_printf(" ECP SW test #1 (constant op_count, base point G): ");
3585 }
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003586 /* Do a dummy multiplication first to trigger precomputation */
David Horstmannceeaeb92023-01-05 15:44:23 +00003587 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2));
3588 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, NULL, NULL));
3589 ret = self_test_point(verbose,
3590 &grp, &R, &m, &grp.G,
3591 sw_exponents,
3592 sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3593 if (ret != 0) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003594 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +00003595 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003596
David Horstmannceeaeb92023-01-05 15:44:23 +00003597 if (verbose != 0) {
3598 mbedtls_printf(" ECP SW test #2 (constant op_count, other point): ");
3599 }
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003600 /* We computed P = 2G last time, use it */
David Horstmannceeaeb92023-01-05 15:44:23 +00003601 ret = self_test_point(verbose,
3602 &grp, &R, &m, &P,
3603 sw_exponents,
3604 sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3605 if (ret != 0) {
Gilles Peskine24666792018-09-17 18:29:49 +02003606 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +00003607 }
Gilles Peskine24666792018-09-17 18:29:49 +02003608
David Horstmannceeaeb92023-01-05 15:44:23 +00003609 mbedtls_ecp_group_free(&grp);
3610 mbedtls_ecp_point_free(&R);
Gilles Peskine24666792018-09-17 18:29:49 +02003611#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3612
3613#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003614 if (verbose != 0) {
3615 mbedtls_printf(" ECP Montgomery test (constant op_count): ");
3616 }
Gilles Peskine24666792018-09-17 18:29:49 +02003617#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003618 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE25519));
Gilles Peskine24666792018-09-17 18:29:49 +02003619#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
David Horstmannceeaeb92023-01-05 15:44:23 +00003620 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE448));
Gilles Peskine24666792018-09-17 18:29:49 +02003621#else
3622#error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
3623#endif
David Horstmannceeaeb92023-01-05 15:44:23 +00003624 ret = self_test_point(verbose,
3625 &grp, &R, &m, &grp.G,
3626 m_exponents,
3627 sizeof(m_exponents) / sizeof(m_exponents[0]));
3628 if (ret != 0) {
Gilles Peskine24666792018-09-17 18:29:49 +02003629 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +00003630 }
Gilles Peskine24666792018-09-17 18:29:49 +02003631#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003632
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003633cleanup:
3634
David Horstmannceeaeb92023-01-05 15:44:23 +00003635 if (ret < 0 && verbose != 0) {
3636 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
3637 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003638
David Horstmannceeaeb92023-01-05 15:44:23 +00003639 mbedtls_ecp_group_free(&grp);
3640 mbedtls_ecp_point_free(&R);
3641 mbedtls_ecp_point_free(&P);
3642 mbedtls_mpi_free(&m);
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003643
David Horstmannceeaeb92023-01-05 15:44:23 +00003644 if (verbose != 0) {
3645 mbedtls_printf("\n");
3646 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003647
David Horstmannceeaeb92023-01-05 15:44:23 +00003648 return ret;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003649}
3650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003652
Janos Follathb0697532016-08-18 12:38:46 +01003653#endif /* !MBEDTLS_ECP_ALT */
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655#endif /* MBEDTLS_ECP_C */