blob: 24cd21b24cbfa232398f3e0814ac138aa8d96367 [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 *
Xiaokang Qian669c7c32023-04-10 07:36:04 +000023 * SEC1 https://www.secg.org/sec1-v2.pdf
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
Xiaokang Qian50fe3632023-04-12 06:08:45 +000027 * - https://www.rfc-editor.org/rfc/rfc4492
Nicholas Wilson08f3ef12015-11-10 13:10:01 +000028 * RFC 7748 for the Curve448 and Curve25519 curve definitions
Xiaokang Qian50fe3632023-04-12 06:08:45 +000029 * - https://www.rfc-editor.org/rfc/rfc7748
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020030 *
Xiaokang Qian50fe3632023-04-12 06:08:45 +000031 * [Curve25519] https://cr.yp.to/ecdh/curve25519-20060209.pdf
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010032 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020033 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020034 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
35 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
36 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010037 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020038 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010039 * render ECC resistant against Side Channel Attacks. IACR Cryptology
40 * ePrint Archive, 2004, vol. 2004, p. 342.
41 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010042 */
43
Gilles Peskinedb09ef62020-06-03 01:43:33 +020044#include "common.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050046/**
47 * \brief Function level alternative implementation.
48 *
49 * The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
50 * replace certain functions in this module. The alternative implementations are
51 * typically hardware accelerators and need to activate the hardware before the
52 * computation starts and deactivate it after it finishes. The
53 * mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
54 * this purpose.
55 *
56 * To preserve the correct functionality the following conditions must hold:
57 *
58 * - The alternative implementation must be activated by
59 * mbedtls_internal_ecp_init() before any of the replaceable functions is
60 * called.
61 * - mbedtls_internal_ecp_free() must \b only be called when the alternative
62 * implementation is activated.
63 * - mbedtls_internal_ecp_init() must \b not be called when the alternative
64 * implementation is activated.
65 * - Public functions must not return while the alternative implementation is
66 * activated.
67 * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
68 * before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
69 * \endcode ensures that the alternative implementation supports the current
70 * group.
71 */
72#if defined(MBEDTLS_ECP_INTERNAL_ALT)
73#endif
74
Valerio Settifd122f42023-04-05 18:15:32 +020075#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010076
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/ecp.h"
Janos Follath430d3372016-11-03 14:25:37 +000078#include "mbedtls/threading.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020081
Janos Follath8c70e812021-06-24 14:48:38 +010082#include "bn_mul.h"
Gilles Peskine80ba8502021-04-03 20:36:37 +020083#include "ecp_invasive.h"
84
Rich Evans00ab4702015-02-06 13:43:58 +000085#include <string.h>
86
Janos Follathb0697532016-08-18 12:38:46 +010087#if !defined(MBEDTLS_ECP_ALT)
88
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000089#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020090
Gilles Peskine6a2fb612021-05-24 22:25:04 +020091#include "ecp_internal_alt.h"
Janos Follathb0697532016-08-18 12:38:46 +010092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010094/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010095 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020096 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010097 */
Valerio Settifd122f42023-04-05 18:15:32 +020098#if defined(MBEDTLS_ECP_C)
99static unsigned long add_count, dbl_count;
100#endif /* MBEDTLS_ECP_C */
101static unsigned long mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100102#endif
103
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200104#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100105/*
106 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200107 *
108 * Default value 0 means that ECC operations will not yield.
109 * Note that regardless of the value of ecp_max_ops, always at
110 * least one step is performed before yielding.
111 *
112 * Setting ecp_max_ops=1 can be suitable for testing purposes
113 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100114 */
115static unsigned ecp_max_ops = 0;
116
117/*
118 * Set ecp_max_ops
119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120void mbedtls_ecp_set_max_ops(unsigned max_ops)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100121{
122 ecp_max_ops = max_ops;
123}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100124
125/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200126 * Check if restart is enabled
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128int mbedtls_ecp_restart_is_enabled(void)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200129{
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return ecp_max_ops != 0;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200131}
132
133/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200134 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136struct mbedtls_ecp_restart_mul {
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100137 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100138 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100139 mbedtls_ecp_point *T; /* table for precomputed points */
140 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200141 enum { /* what were we doing last time we returned? */
142 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
143 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100144 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
145 ecp_rsm_pre_add, /* precompute remaining points by adding */
146 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200147 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100148 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100149 } state;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100150};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100151
152/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200153 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100154 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155static void ecp_restart_rsm_init(mbedtls_ecp_restart_mul_ctx *ctx)
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100156{
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_ecp_point_init(&ctx->R);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200158 ctx->i = 0;
159 ctx->T = NULL;
160 ctx->T_size = 0;
161 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100162}
163
164/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200165 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx)
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100168{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100169 unsigned char i;
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ctx == NULL) {
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100172 return;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 mbedtls_ecp_point_free(&ctx->R);
176
177 if (ctx->T != NULL) {
178 for (i = 0; i < ctx->T_size; i++) {
179 mbedtls_ecp_point_free(ctx->T + i);
180 }
181 mbedtls_free(ctx->T);
182 }
183
184 ecp_restart_rsm_init(ctx);
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100185}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100186
187/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200188 * Restart context for ecp_muladd()
189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190struct mbedtls_ecp_restart_muladd {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200191 mbedtls_ecp_point mP; /* mP value */
192 mbedtls_ecp_point R; /* R intermediate result */
193 enum { /* what should we do next? */
194 ecp_rsma_mul1 = 0, /* first multiplication */
195 ecp_rsma_mul2, /* second multiplication */
196 ecp_rsma_add, /* addition */
197 ecp_rsma_norm, /* normalization */
198 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200199};
200
201/*
202 * Init restart_muladd sub-context
203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204static void ecp_restart_ma_init(mbedtls_ecp_restart_muladd_ctx *ctx)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_ecp_point_init(&ctx->mP);
207 mbedtls_ecp_point_init(&ctx->R);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200208 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200209}
210
211/*
212 * Free the components of a restart_muladd sub-context
213 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200215{
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (ctx == NULL) {
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200217 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 }
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_ecp_point_free(&ctx->mP);
221 mbedtls_ecp_point_free(&ctx->R);
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 ecp_restart_ma_init(ctx);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200224}
225
226/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200227 * Initialize a restart context
228 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200230{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200231 ctx->ops_done = 0;
232 ctx->depth = 0;
233 ctx->rsm = NULL;
234 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200235}
236
237/*
238 * Free the components of a restart context
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200241{
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (ctx == NULL) {
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200243 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 }
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 ecp_restart_rsm_free(ctx->rsm);
247 mbedtls_free(ctx->rsm);
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 ecp_restart_ma_free(ctx->ma);
250 mbedtls_free(ctx->ma);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 mbedtls_ecp_restart_init(ctx);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200253}
254
255/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100256 * Check if we can do the next step
257 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
259 mbedtls_ecp_restart_ctx *rs_ctx,
260 unsigned ops)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100261{
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (rs_ctx != NULL && ecp_max_ops != 0) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100263 /* scale depending on curve size: the chosen reference is 256-bit,
264 * and multiplication is quadratic. Round to the closest integer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (grp->pbits >= 512) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100266 ops *= 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 } else if (grp->pbits >= 384) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100268 ops *= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 }
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100270
Hanno Beckerb10c6602018-10-26 13:50:13 +0100271 /* Avoid infinite loops: always allow first step.
272 * Because of that, however, it's not generally true
273 * that ops_done <= ecp_max_ops, so the check
274 * ops_done > ecp_max_ops below is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((rs_ctx->ops_done != 0) &&
276 (rs_ctx->ops_done > ecp_max_ops ||
277 ops > ecp_max_ops - rs_ctx->ops_done)) {
278 return MBEDTLS_ERR_ECP_IN_PROGRESS;
Hanno Beckerb10c6602018-10-26 13:50:13 +0100279 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100280
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100281 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200282 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100283 }
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return 0;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100286}
287
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200288/* Call this when entering a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289#define ECP_RS_ENTER(SUB) do { \
290 /* reset ops count for this call if top-level */ \
291 if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200292 rs_ctx->ops_done = 0; \
293 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 /* set up our own sub-context if needed */ \
295 if (mbedtls_ecp_restart_is_enabled() && \
296 rs_ctx != NULL && rs_ctx->SUB == NULL) \
297 { \
298 rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
299 if (rs_ctx->SUB == NULL) \
300 return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
301 \
302 ecp_restart_## SUB ##_init(rs_ctx->SUB); \
303 } \
304} while (0)
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200305
306/* Call this when leaving a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307#define ECP_RS_LEAVE(SUB) do { \
308 /* clear our sub-context when not in progress (done or error) */ \
309 if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
310 ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
311 { \
312 ecp_restart_## SUB ##_free(rs_ctx->SUB); \
313 mbedtls_free(rs_ctx->SUB); \
314 rs_ctx->SUB = NULL; \
315 } \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200316 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (rs_ctx != NULL) \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200318 rs_ctx->depth--; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100319} while (0)
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200320
321#else /* MBEDTLS_ECP_RESTARTABLE */
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323#define ECP_RS_ENTER(sub) (void) rs_ctx;
324#define ECP_RS_LEAVE(sub) (void) rs_ctx;
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200325
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200326#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100327
Valerio Settifd122f42023-04-05 18:15:32 +0200328#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void mpi_init_many(mbedtls_mpi *arr, size_t size)
Hanno Becker466df6e2022-01-10 11:16:51 +0000330{
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 while (size--) {
332 mbedtls_mpi_init(arr++);
333 }
Hanno Becker466df6e2022-01-10 11:16:51 +0000334}
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336static void mpi_free_many(mbedtls_mpi *arr, size_t size)
Hanno Becker466df6e2022-01-10 11:16:51 +0000337{
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 while (size--) {
339 mbedtls_mpi_free(arr++);
340 }
Hanno Becker466df6e2022-01-10 11:16:51 +0000341}
Valerio Settifd122f42023-04-05 18:15:32 +0200342#endif /* MBEDTLS_ECP_C */
Hanno Becker466df6e2022-01-10 11:16:51 +0000343
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100344/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200345 * List of supported curves:
346 * - internal ID
Christoph M. Wintersteigercb310732019-02-15 15:50:38 +0000347 * - 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 +0200348 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200349 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100350 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100351 * Curves are listed in order: largest curves first, and for a given size,
Gilles Peskineae270bf2021-06-02 00:05:29 +0200352 * fastest curves first.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200353 *
Gilles Peskineae270bf2021-06-02 00:05:29 +0200354 * Reminder: update profiles in x509_crt.c and ssl_tls.c when adding a new curve!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
359 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
362 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
365 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
368 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
371 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
374 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
377 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
380 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
383 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
386 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
389 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100390#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200391#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Christoph M. Wintersteiger86e36c42018-12-06 17:27:31 +0000392 { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100393#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200394#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
395 { MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200398};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400#define ECP_NB_CURVES sizeof(ecp_supported_curves) / \
401 sizeof(ecp_supported_curves[0])
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200404
405/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200406 * List of supported curves and associated info
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200409{
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return ecp_supported_curves;
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200411}
412
413/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100414 * List of supported curves, group ID only
415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100417{
418 static int init_done = 0;
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (!init_done) {
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100421 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 curve_info++) {
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100427 ecp_supported_grp_id[i++] = curve_info->grp_id;
428 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100430
431 init_done = 1;
432 }
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return ecp_supported_grp_id;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100435}
436
437/*
438 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200439 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440const 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 +0200441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 curve_info++) {
447 if (curve_info->grp_id == grp_id) {
448 return curve_info;
449 }
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200450 }
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return NULL;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200453}
454
455/*
456 * Get the curve info from the TLS identifier
457 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 curve_info++) {
465 if (curve_info->tls_id == tls_id) {
466 return curve_info;
467 }
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200468 }
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return NULL;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200471}
472
473/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100474 * Get the curve info from the name
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (name == NULL) {
481 return NULL;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100482 }
483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 for (curve_info = mbedtls_ecp_curve_list();
485 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
486 curve_info++) {
487 if (strcmp(curve_info->name, name) == 0) {
488 return curve_info;
489 }
490 }
491
492 return NULL;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100493}
494
495/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100496 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100499{
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (grp->G.X.p == NULL) {
501 return MBEDTLS_ECP_TYPE_NONE;
502 }
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (grp->G.Y.p == NULL) {
505 return MBEDTLS_ECP_TYPE_MONTGOMERY;
506 } else {
507 return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS;
508 }
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100509}
510
511/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100512 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100513 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100515{
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_mpi_init(&pt->X);
517 mbedtls_mpi_init(&pt->Y);
518 mbedtls_mpi_init(&pt->Z);
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100519}
520
521/*
522 * Initialize (the components of) a group
523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100525{
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200526 grp->id = MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_mpi_init(&grp->P);
528 mbedtls_mpi_init(&grp->A);
529 mbedtls_mpi_init(&grp->B);
530 mbedtls_ecp_point_init(&grp->G);
531 mbedtls_mpi_init(&grp->N);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200532 grp->pbits = 0;
533 grp->nbits = 0;
534 grp->h = 0;
535 grp->modp = NULL;
536 grp->t_pre = NULL;
537 grp->t_post = NULL;
538 grp->t_data = NULL;
539 grp->T = NULL;
540 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100541}
542
543/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200544 * Initialize (the components of) a key pair
545 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200547{
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 mbedtls_ecp_group_init(&key->grp);
549 mbedtls_mpi_init(&key->d);
550 mbedtls_ecp_point_init(&key->Q);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200551}
552
553/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100554 * Unallocate (the components of) a point
555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100557{
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (pt == NULL) {
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100559 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 }
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_mpi_free(&(pt->X));
563 mbedtls_mpi_free(&(pt->Y));
564 mbedtls_mpi_free(&(pt->Z));
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100565}
566
567/*
kXuanba9cb762021-04-08 14:32:06 +0800568 * Check that the comb table (grp->T) is static initialized.
569 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570static int ecp_group_is_static_comb_table(const mbedtls_ecp_group *grp)
571{
kXuanba9cb762021-04-08 14:32:06 +0800572#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
573 return grp->T != NULL && grp->T_size == 0;
574#else
575 (void) grp;
576 return 0;
577#endif
578}
579
580/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100581 * Unallocate (the components of) a group
582 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100583void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100584{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200585 size_t i;
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (grp == NULL) {
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100588 return;
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100589 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (grp->h != 1) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 mbedtls_mpi_free(&grp->A);
593 mbedtls_mpi_free(&grp->B);
594 mbedtls_ecp_point_free(&grp->G);
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200595 }
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) {
598 for (i = 0; i < grp->T_size; i++) {
599 mbedtls_ecp_point_free(&grp->T[i]);
600 }
601 mbedtls_free(grp->T);
602 }
603
604 mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group));
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100605}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100606
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100607/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200608 * Unallocate (the components of) a key pair
609 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200611{
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (key == NULL) {
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200613 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 }
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 mbedtls_ecp_group_free(&key->grp);
617 mbedtls_mpi_free(&key->d);
618 mbedtls_ecp_point_free(&key->Q);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200619}
620
621/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200622 * Copy the contents of a point
623 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200625{
Janos Follath24eed8d2019-11-22 13:21:35 +0000626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
628 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y));
629 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z));
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200630
631cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200633}
634
635/*
636 * Copy the contents of a group object
637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200639{
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 return mbedtls_ecp_group_load(dst, src->id);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200641}
642
643/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100644 * Set point to zero
645 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100647{
Janos Follath24eed8d2019-11-22 13:21:35 +0000648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
650 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1));
651 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0));
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100652
653cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 return ret;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100655}
656
657/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100658 * Tell if a point is zero
659 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100661{
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100663}
664
665/*
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500666 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
669 const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
672 mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
673 mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
674 return 0;
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200675 }
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200678}
679
680/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100681 * Import a non-zero point from ASCII strings
682 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
684 const char *x, const char *y)
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100685{
Janos Follath24eed8d2019-11-22 13:21:35 +0000686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
688 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y));
689 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100690
691cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100693}
694
695/*
Janos Follath7caf8e42019-02-20 12:00:22 +0000696 * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
699 const mbedtls_ecp_point *P,
700 int format, size_t *olen,
701 unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100702{
Janos Follath28eb06d2019-02-26 10:53:34 +0000703 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100704 size_t plen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
706 format != MBEDTLS_ECP_PF_COMPRESSED) {
707 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
708 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 plen = mbedtls_mpi_size(&grp->P);
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100711
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200712#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine59970052019-02-28 13:12:06 +0100713 (void) format; /* Montgomery curves always use the same point format */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000715 *olen = plen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (buflen < *olen) {
717 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
718 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen));
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100721 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000722#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200723#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000725 /*
726 * Common case: P == 0
727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
729 if (buflen < 1) {
730 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
731 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100732
Janos Follath7caf8e42019-02-20 12:00:22 +0000733 buf[0] = 0x00;
734 *olen = 1;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return 0;
Janos Follath7caf8e42019-02-20 12:00:22 +0000737 }
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000740 *olen = 2 * plen + 1;
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (buflen < *olen) {
743 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
744 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000745
746 buf[0] = 0x04;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
748 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen));
749 } else if (format == MBEDTLS_ECP_PF_COMPRESSED) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000750 *olen = plen + 1;
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (buflen < *olen) {
753 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
754 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0);
757 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
Janos Follath7caf8e42019-02-20 12:00:22 +0000758 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100759 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000760#endif
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100761
762cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 return ret;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100764}
765
Glenn Strauss2ff77112022-09-14 23:27:50 -0400766#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100767static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
768 const mbedtls_mpi *X,
769 mbedtls_mpi *Y,
770 int parity_bit);
Glenn Strauss2ff77112022-09-14 23:27:50 -0400771#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
772
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100773/*
Janos Follath59b813c2019-02-13 10:44:06 +0000774 * Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100775 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
777 mbedtls_ecp_point *pt,
778 const unsigned char *buf, size_t ilen)
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100779{
Janos Follath28eb06d2019-02-26 10:53:34 +0000780 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100781 size_t plen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ilen < 1) {
783 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
784 }
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 plen = mbedtls_mpi_size(&grp->P);
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100787
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200788#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
790 if (plen != ilen) {
791 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
792 }
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen));
795 mbedtls_mpi_free(&pt->Y);
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (grp->id == MBEDTLS_ECP_DP_CURVE25519) {
Janos Follathffbd7e82019-02-25 11:35:20 +0000798 /* Set most significant bit to 0 as prescribed in RFC7748 §5 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0));
800 }
Janos Follath28eb06d2019-02-26 10:53:34 +0000801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
Janos Follath59b813c2019-02-13 10:44:06 +0000803 }
804#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200805#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
807 if (buf[0] == 0x00) {
808 if (ilen == 1) {
809 return mbedtls_ecp_set_zero(pt);
810 } else {
811 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
812 }
Janos Follath59b813c2019-02-13 10:44:06 +0000813 }
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (ilen < 1 + plen) {
816 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
817 }
Janos Follath59b813c2019-02-13 10:44:06 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen));
820 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
Glenn Strauss2ff77112022-09-14 23:27:50 -0400821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (buf[0] == 0x04) {
Glenn Strauss2ff77112022-09-14 23:27:50 -0400823 /* format == MBEDTLS_ECP_PF_UNCOMPRESSED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (ilen != 1 + plen * 2) {
825 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
826 }
827 return mbedtls_mpi_read_binary(&pt->Y, buf + 1 + plen, plen);
828 } else if (buf[0] == 0x02 || buf[0] == 0x03) {
Glenn Strauss2ff77112022-09-14 23:27:50 -0400829 /* format == MBEDTLS_ECP_PF_COMPRESSED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (ilen != 1 + plen) {
831 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
832 }
833 return mbedtls_ecp_sw_derive_y(grp, &pt->X, &pt->Y,
834 (buf[0] & 1));
835 } else {
836 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Glenn Strauss2ff77112022-09-14 23:27:50 -0400837 }
Janos Follath59b813c2019-02-13 10:44:06 +0000838 }
839#endif
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100840
841cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 return ret;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100843}
844
845/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100846 * Import a point from a TLS ECPoint record (RFC 4492)
847 * struct {
848 * opaque point <1..2^8-1>;
849 * } ECPoint;
850 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
852 mbedtls_ecp_point *pt,
853 const unsigned char **buf, size_t buf_len)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100854{
855 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100856 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100857 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200858 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100859 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (buf_len < 2) {
861 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
862 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100863
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100864 data_len = *(*buf)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (data_len < 1 || data_len > buf_len - 1) {
866 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
867 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100868
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100869 /*
870 * Save buffer start for read_binary and update buf
871 */
872 buf_start = *buf;
873 *buf += data_len;
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len);
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100876}
877
878/*
879 * Export a point as a TLS ECPoint record (RFC 4492)
880 * struct {
881 * opaque point <1..2^8-1>;
882 * } ECPoint;
883 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
885 int format, size_t *olen,
886 unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100887{
Janos Follath24eed8d2019-11-22 13:21:35 +0000888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
890 format != MBEDTLS_ECP_PF_COMPRESSED) {
891 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
892 }
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100893
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100894 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100895 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (blen < 1) {
898 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
899 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format,
902 olen, buf + 1, blen - 1)) != 0) {
903 return ret;
904 }
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100905
906 /*
907 * write length to the first byte and update total length
908 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200909 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100910 ++*olen;
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100913}
914
915/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100916 * Set a group from an ECParameters record (RFC 4492)
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
919 const unsigned char **buf, size_t len)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500920{
Janos Follath24eed8d2019-11-22 13:21:35 +0000921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500922 mbedtls_ecp_group_id grp_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) {
924 return ret;
925 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 return mbedtls_ecp_group_load(grp, grp_id);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500928}
929
930/*
931 * Read a group id from an ECParameters record (RFC 4492) and convert it to
932 * mbedtls_ecp_group_id.
933 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
935 const unsigned char **buf, size_t len)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100936{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200937 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100939 /*
940 * We expect at least three bytes (see below)
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (len < 3) {
943 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
944 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100945
946 /*
947 * First byte is curve_type; only named_curve is handled
948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
950 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
951 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100952
953 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100954 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100955 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200956 tls_id = *(*buf)++;
957 tls_id <<= 8;
958 tls_id |= *(*buf)++;
959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) {
961 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
962 }
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200963
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500964 *grp = curve_info->grp_id;
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 return 0;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100967}
968
969/*
970 * Write the ECParameters record corresponding to a group (RFC 4492)
971 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
973 unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100974{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 const mbedtls_ecp_curve_info *curve_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) {
977 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
978 }
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200979
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100980 /*
981 * We are going to write 3 bytes (see below)
982 */
983 *olen = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (blen < *olen) {
985 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
986 }
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100987
988 /*
989 * First byte is curve_type, always named_curve
990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100992
993 /*
994 * Next two bytes are the namedcurve value
995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0);
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100999}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001000
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001001/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1003 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001004 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 * 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 +02001006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001008{
Janos Follath24eed8d2019-11-22 13:21:35 +00001009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (grp->modp == NULL) {
1012 return mbedtls_mpi_mod_mpi(N, N, &grp->P);
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001013 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 /* N->s < 0 is a much faster test, which fails only if N is 0 */
1016 if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
1017 mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
1018 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1019 }
1020
1021 MBEDTLS_MPI_CHK(grp->modp(N));
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001022
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001023 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) {
1025 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P));
1026 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001029 /* we known P, N and the result are positive */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P));
1031 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001032
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +02001033cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return ret;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +02001035}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001036
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +01001037/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001038 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001039 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001040 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 * 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 +01001042 * bring the result back to this range.
1043 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001044 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001045 */
1046
1047/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 * 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 +01001049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001051#define INC_MUL_COUNT mul_count++;
1052#else
1053#define INC_MUL_COUNT
1054#endif
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056#define MOD_MUL(N) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001057 do \
1058 { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 MBEDTLS_MPI_CHK(ecp_modp(&(N), grp)); \
Hanno Becker1eeca412018-10-15 12:01:35 +01001060 INC_MUL_COUNT \
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 } while (0)
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063static inline int mbedtls_mpi_mul_mod(const mbedtls_ecp_group *grp,
1064 mbedtls_mpi *X,
1065 const mbedtls_mpi *A,
1066 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001067{
Janos Follath24eed8d2019-11-22 13:21:35 +00001068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(X, A, B));
1070 MOD_MUL(*X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001071cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001073}
1074
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001075/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001077 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079#define MOD_SUB(N) \
Hanno Beckerce29ae82022-01-04 04:55:11 +00001080 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 while ((N)->s < 0 && mbedtls_mpi_cmp_int((N), 0) != 0) \
1082 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi((N), (N), &grp->P)); \
1083 } while (0)
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085#if (defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
1086 !(defined(MBEDTLS_ECP_NO_FALLBACK) && \
1087 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1088 defined(MBEDTLS_ECP_ADD_MIXED_ALT))) || \
1089 (defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) && \
1090 !(defined(MBEDTLS_ECP_NO_FALLBACK) && \
1091 defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)))
1092static inline int mbedtls_mpi_sub_mod(const mbedtls_ecp_group *grp,
1093 mbedtls_mpi *X,
1094 const mbedtls_mpi *A,
1095 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001096{
Janos Follath24eed8d2019-11-22 13:21:35 +00001097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, A, B));
1099 MOD_SUB(X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001100cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001102}
Steven Cooremane5388962021-03-01 14:04:53 +01001103#endif /* All functions referencing mbedtls_mpi_sub_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001104
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001105/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 * 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 +02001107 * We known P, N and the result are positive, so sub_abs is correct, and
1108 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110#define MOD_ADD(N) \
1111 while (mbedtls_mpi_cmp_mpi((N), &grp->P) >= 0) \
1112 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs((N), (N), &grp->P))
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp,
1115 mbedtls_mpi *X,
1116 const mbedtls_mpi *A,
1117 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001118{
Janos Follath24eed8d2019-11-22 13:21:35 +00001119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B));
1121 MOD_ADD(X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001122cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001124}
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126static inline int mbedtls_mpi_mul_int_mod(const mbedtls_ecp_group *grp,
1127 mbedtls_mpi *X,
1128 const mbedtls_mpi *A,
1129 mbedtls_mpi_uint c)
Hanno Becker02b35bd2022-01-01 06:54:25 +00001130{
1131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(X, A, c));
1134 MOD_ADD(X);
Hanno Becker02b35bd2022-01-01 06:54:25 +00001135cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return ret;
Hanno Becker02b35bd2022-01-01 06:54:25 +00001137}
1138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139static inline int mbedtls_mpi_sub_int_mod(const mbedtls_ecp_group *grp,
1140 mbedtls_mpi *X,
1141 const mbedtls_mpi *A,
1142 mbedtls_mpi_uint c)
Hanno Beckerce29ae82022-01-04 04:55:11 +00001143{
1144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(X, A, c));
1147 MOD_SUB(X);
Hanno Beckerce29ae82022-01-04 04:55:11 +00001148cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 return ret;
Hanno Beckerce29ae82022-01-04 04:55:11 +00001150}
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152#define MPI_ECP_SUB_INT(X, A, c) \
1153 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int_mod(grp, X, A, c))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001154
Steven Cooremane5388962021-03-01 14:04:53 +01001155#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 !(defined(MBEDTLS_ECP_NO_FALLBACK) && \
1157 defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
1158 defined(MBEDTLS_ECP_ADD_MIXED_ALT))
1159static inline int mbedtls_mpi_shift_l_mod(const mbedtls_ecp_group *grp,
1160 mbedtls_mpi *X,
1161 size_t count)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001162{
Janos Follath24eed8d2019-11-22 13:21:35 +00001163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, count));
1165 MOD_ADD(X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001166cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001168}
Gilles Peskine449bd832023-01-11 14:50:10 +01001169#endif \
1170 /* All functions referencing mbedtls_mpi_shift_l_mod() are alt-implemented without fallback */
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001171
Hanno Becker595616e2022-01-05 08:28:24 +00001172/*
1173 * Macro wrappers around ECP modular arithmetic
1174 *
1175 * Currently, these wrappers are defined via the bignum module.
1176 */
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178#define MPI_ECP_ADD(X, A, B) \
1179 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, X, A, B))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181#define MPI_ECP_SUB(X, A, B) \
1182 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, X, A, B))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184#define MPI_ECP_MUL(X, A, B) \
1185 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, B))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187#define MPI_ECP_SQR(X, A) \
1188 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, A))
Hanno Becker885ed402022-01-04 06:43:50 +00001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190#define MPI_ECP_MUL_INT(X, A, c) \
1191 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int_mod(grp, X, A, c))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193#define MPI_ECP_INV(dst, src) \
1194 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod((dst), (src), &grp->P))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196#define MPI_ECP_MOV(X, A) \
1197 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199#define MPI_ECP_SHIFT_L(X, count) \
1200 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, X, count))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202#define MPI_ECP_LSET(X, c) \
1203 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, c))
Hanno Becker595616e2022-01-05 08:28:24 +00001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205#define MPI_ECP_CMP_INT(X, c) \
1206 mbedtls_mpi_cmp_int(X, c)
Hanno Becker595616e2022-01-05 08:28:24 +00001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208#define MPI_ECP_CMP(X, Y) \
1209 mbedtls_mpi_cmp_mpi(X, Y)
Hanno Becker595616e2022-01-05 08:28:24 +00001210
1211/* Needs f_rng, p_rng to be defined. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212#define MPI_ECP_RAND(X) \
1213 MBEDTLS_MPI_CHK(mbedtls_mpi_random((X), 2, &grp->P, f_rng, p_rng))
Hanno Becker595616e2022-01-05 08:28:24 +00001214
1215/* Conditional negation
1216 * Needs grp and a temporary MPI tmp to be defined. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217#define MPI_ECP_COND_NEG(X, cond) \
Hanno Becker595616e2022-01-05 08:28:24 +00001218 do \
1219 { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 unsigned char nonzero = mbedtls_mpi_cmp_int((X), 0) != 0; \
1221 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&tmp, &grp->P, (X))); \
1222 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), &tmp, \
1223 nonzero & cond)); \
1224 } while (0)
Hanno Becker595616e2022-01-05 08:28:24 +00001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226#define MPI_ECP_NEG(X) MPI_ECP_COND_NEG((X), 1)
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228#define MPI_ECP_VALID(X) \
1229 ((X)->p != NULL)
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231#define MPI_ECP_COND_ASSIGN(X, Y, cond) \
1232 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), (Y), (cond)))
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234#define MPI_ECP_COND_SWAP(X, Y, cond) \
1235 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap((X), (Y), (cond)))
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001236
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02001237#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Glenn Straussefde9d52022-12-20 04:20:12 -05001238
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001239/*
1240 * Computes the right-hand side of the Short Weierstrass equation
1241 * RHS = X^3 + A X + B
1242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243static int ecp_sw_rhs(const mbedtls_ecp_group *grp,
1244 mbedtls_mpi *rhs,
1245 const mbedtls_mpi *X)
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001246{
1247 int ret;
1248
1249 /* Compute X^3 + A X + B as X (X^2 + A) + B */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 MPI_ECP_SQR(rhs, X);
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001251
1252 /* Special case for A = -3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (grp->A.p == NULL) {
1254 MPI_ECP_SUB_INT(rhs, rhs, 3);
1255 } else {
1256 MPI_ECP_ADD(rhs, rhs, &grp->A);
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001257 }
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 MPI_ECP_MUL(rhs, rhs, X);
1260 MPI_ECP_ADD(rhs, rhs, &grp->B);
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001261
1262cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 return ret;
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001264}
1265
1266/*
1267 * Derive Y from X and a parity bit
1268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
1270 const mbedtls_mpi *X,
1271 mbedtls_mpi *Y,
1272 int parity_bit)
Glenn Strauss45241612022-12-19 19:37:07 -05001273{
1274 /* w = y^2 = x^3 + ax + b
1275 * y = sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4)
1276 *
1277 * Note: this method for extracting square root does not validate that w
1278 * was indeed a square so this function will return garbage in Y if X
1279 * does not correspond to a point on the curve.
1280 */
1281
1282 /* Check prerequisite p = 3 mod 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (mbedtls_mpi_get_bit(&grp->P, 0) != 1 ||
1284 mbedtls_mpi_get_bit(&grp->P, 1) != 1) {
1285 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
1286 }
Glenn Strauss45241612022-12-19 19:37:07 -05001287
1288 int ret;
1289 mbedtls_mpi exp;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 mbedtls_mpi_init(&exp);
Glenn Strauss45241612022-12-19 19:37:07 -05001291
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001292 /* use Y to store intermediate result, actually w above */
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, Y, X));
Glenn Strauss45241612022-12-19 19:37:07 -05001294
1295 /* w = y^2 */ /* Y contains y^2 intermediate result */
1296 /* exp = ((p+1)/4) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&exp, &grp->P, 1));
1298 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&exp, 2));
Glenn Strauss45241612022-12-19 19:37:07 -05001299 /* sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(Y, Y /*y^2*/, &exp, &grp->P, NULL));
Glenn Strauss45241612022-12-19 19:37:07 -05001301
1302 /* check parity bit match or else invert Y */
1303 /* This quick inversion implementation is valid because Y != 0 for all
1304 * Short Weierstrass curves supported by mbedtls, as each supported curve
1305 * has an order that is a large prime, so each supported curve does not
1306 * have any point of order 2, and a point with Y == 0 would be of order 2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (mbedtls_mpi_get_bit(Y, 0) != parity_bit) {
1308 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(Y, &grp->P, Y));
1309 }
Glenn Strauss45241612022-12-19 19:37:07 -05001310
1311cleanup:
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 mbedtls_mpi_free(&exp);
1314 return ret;
Glenn Strauss45241612022-12-19 19:37:07 -05001315}
Valerio Settifd122f42023-04-05 18:15:32 +02001316#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Glenn Strauss45241612022-12-19 19:37:07 -05001317
Valerio Settifd122f42023-04-05 18:15:32 +02001318#if defined(MBEDTLS_ECP_C)
1319#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001320/*
1321 * For curves in short Weierstrass form, we do all the internal operations in
1322 * Jacobian coordinates.
1323 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001324 * For multiplication, we'll use a comb method with countermeasures against
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001325 * SPA, hence timing attacks.
1326 */
1327
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001328/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001329 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001330 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001333{
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (MPI_ECP_CMP_INT(&pt->Z, 0) == 0) {
1335 return 0;
1336 }
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001337
Janos Follathb0697532016-08-18 12:38:46 +01001338#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if (mbedtls_internal_ecp_grp_capable(grp)) {
1340 return mbedtls_internal_ecp_normalize_jac(grp, pt);
1341 }
Janos Follath372697b2016-10-28 16:53:11 +01001342#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001343
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001344#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001346#else
1347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker76f897d2022-01-02 12:47:34 +00001348 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 mbedtls_mpi_init(&T);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 MPI_ECP_INV(&T, &pt->Z); /* T <- 1 / Z */
1352 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y' <- Y*T = Y / Z */
1353 MPI_ECP_SQR(&T, &T); /* T <- T^2 = 1 / Z^2 */
1354 MPI_ECP_MUL(&pt->X, &pt->X, &T); /* X <- X * T = X / Z^2 */
1355 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y'' <- Y' * T = Y / Z^3 */
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 MPI_ECP_LSET(&pt->Z, 1);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001358
1359cleanup:
1360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 mbedtls_mpi_free(&T);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001364#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001365}
1366
1367/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001368 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001369 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001370 * (See for example Cohen's "A Course in Computational Algebraic Number
1371 * Theory", Algorithm 10.3.4.)
1372 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001373 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001374 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001375 *
1376 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
1379 mbedtls_ecp_point *T[], size_t T_size)
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001380{
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (T_size < 2) {
1382 return ecp_normalize_jac(grp, *T);
1383 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001384
Janos Follathb0697532016-08-18 12:38:46 +01001385#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (mbedtls_internal_ecp_grp_capable(grp)) {
1387 return mbedtls_internal_ecp_normalize_jac_many(grp, T, T_size);
1388 }
Janos Follathb0697532016-08-18 12:38:46 +01001389#endif
1390
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001391#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001393#else
1394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1395 size_t i;
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001396 mbedtls_mpi *c, t;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) {
1399 return MBEDTLS_ERR_ECP_ALLOC_FAILED;
1400 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 mbedtls_mpi_init(&t);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 mpi_init_many(c, T_size);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001405 /*
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001406 * c[i] = Z_0 * ... * Z_i, i = 0,..,n := T_size-1
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 MPI_ECP_MOV(&c[0], &T[0]->Z);
1409 for (i = 1; i < T_size; i++) {
1410 MPI_ECP_MUL(&c[i], &c[i-1], &T[i]->Z);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001411 }
1412
1413 /*
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001414 * c[n] = 1 / (Z_0 * ... * Z_n) mod P
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 MPI_ECP_INV(&c[T_size-1], &c[T_size-1]);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 for (i = T_size - 1;; i--) {
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001419 /* At the start of iteration i (note that i decrements), we have
1420 * - c[j] = Z_0 * .... * Z_j for j < i,
1421 * - c[j] = 1 / (Z_0 * .... * Z_j) for j == i,
1422 *
1423 * This is maintained via
1424 * - c[i-1] <- c[i] * Z_i
1425 *
1426 * We also derive 1/Z_i = c[i] * c[i-1] for i>0 and use that
1427 * to do the actual normalization. For i==0, we already have
1428 * c[0] = 1 / Z_0.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001429 */
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (i > 0) {
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001432 /* Compute 1/Z_i and establish invariant for the next iteration. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 MPI_ECP_MUL(&t, &c[i], &c[i-1]);
1434 MPI_ECP_MUL(&c[i-1], &c[i], &T[i]->Z);
1435 } else {
1436 MPI_ECP_MOV(&t, &c[0]);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001437 }
1438
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001439 /* Now t holds 1 / Z_i; normalize as in ecp_normalize_jac() */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
1441 MPI_ECP_SQR(&t, &t);
1442 MPI_ECP_MUL(&T[i]->X, &T[i]->X, &t);
1443 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001444
1445 /*
1446 * Post-precessing: reclaim some memory by shrinking coordinates
1447 * - not storing Z (always 1)
1448 * - shrinking other coordinates, but still keeping the same number of
1449 * limbs as P, as otherwise it will too likely be regrown too fast.
1450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n));
1452 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n));
Hanno Beckeree95f6c2022-01-09 05:46:18 +00001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 MPI_ECP_LSET(&T[i]->Z, 1);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (i == 0) {
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001457 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001459 }
1460
1461cleanup:
1462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_mpi_free(&t);
1464 mpi_free_many(c, T_size);
1465 mbedtls_free(c);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001468#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) */
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001469}
1470
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001471/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001472 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1473 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
1476 mbedtls_ecp_point *Q,
1477 unsigned char inv)
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001478{
Janos Follath24eed8d2019-11-22 13:21:35 +00001479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker595616e2022-01-05 08:28:24 +00001480 mbedtls_mpi tmp;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 mbedtls_mpi_init(&tmp);
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 MPI_ECP_COND_NEG(&Q->Y, inv);
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001484
1485cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 mbedtls_mpi_free(&tmp);
1487 return ret;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001488}
1489
1490/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001491 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001492 *
Peter Dettmance661b22015-02-07 14:43:51 +07001493 * 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 +01001494 *
Peter Dettmance661b22015-02-07 14:43:51 +07001495 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1496 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1497 *
1498 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1499 *
1500 * Cost: 1D := 3M + 4S (A == 0)
1501 * 4M + 4S (A == -3)
1502 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1505 const mbedtls_ecp_point *P,
1506 mbedtls_mpi tmp[4])
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001509 dbl_count++;
1510#endif
1511
Janos Follathb0697532016-08-18 12:38:46 +01001512#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (mbedtls_internal_ecp_grp_capable(grp)) {
1514 return mbedtls_internal_ecp_double_jac(grp, R, P);
1515 }
Janos Follath372697b2016-10-28 16:53:11 +01001516#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001517
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001518#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001520#else
1521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001522
1523 /* Special case for A = -3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (grp->A.p == NULL) {
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001525 /* tmp[0] <- M = 3(X + Z^2)(X - Z^2) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 MPI_ECP_SQR(&tmp[1], &P->Z);
1527 MPI_ECP_ADD(&tmp[2], &P->X, &tmp[1]);
1528 MPI_ECP_SUB(&tmp[3], &P->X, &tmp[1]);
1529 MPI_ECP_MUL(&tmp[1], &tmp[2], &tmp[3]);
1530 MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
1531 } else {
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001532 /* tmp[0] <- M = 3.X^2 + A.Z^4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 MPI_ECP_SQR(&tmp[1], &P->X);
1534 MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
Peter Dettmance661b22015-02-07 14:43:51 +07001535
1536 /* Optimize away for "koblitz" curves with A = 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (MPI_ECP_CMP_INT(&grp->A, 0) != 0) {
Peter Dettmance661b22015-02-07 14:43:51 +07001538 /* M += A.Z^4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 MPI_ECP_SQR(&tmp[1], &P->Z);
1540 MPI_ECP_SQR(&tmp[2], &tmp[1]);
1541 MPI_ECP_MUL(&tmp[1], &tmp[2], &grp->A);
1542 MPI_ECP_ADD(&tmp[0], &tmp[0], &tmp[1]);
Peter Dettmance661b22015-02-07 14:43:51 +07001543 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001544 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001545
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001546 /* tmp[1] <- S = 4.X.Y^2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 MPI_ECP_SQR(&tmp[2], &P->Y);
1548 MPI_ECP_SHIFT_L(&tmp[2], 1);
1549 MPI_ECP_MUL(&tmp[1], &P->X, &tmp[2]);
1550 MPI_ECP_SHIFT_L(&tmp[1], 1);
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001551
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001552 /* tmp[3] <- U = 8.Y^4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 MPI_ECP_SQR(&tmp[3], &tmp[2]);
1554 MPI_ECP_SHIFT_L(&tmp[3], 1);
Peter Dettmance661b22015-02-07 14:43:51 +07001555
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001556 /* tmp[2] <- T = M^2 - 2.S */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 MPI_ECP_SQR(&tmp[2], &tmp[0]);
1558 MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
1559 MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
Peter Dettmance661b22015-02-07 14:43:51 +07001560
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001561 /* tmp[1] <- S = M(S - T) - U */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[2]);
1563 MPI_ECP_MUL(&tmp[1], &tmp[1], &tmp[0]);
1564 MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[3]);
Peter Dettmance661b22015-02-07 14:43:51 +07001565
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001566 /* tmp[3] <- U = 2.Y.Z */
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 MPI_ECP_MUL(&tmp[3], &P->Y, &P->Z);
1568 MPI_ECP_SHIFT_L(&tmp[3], 1);
Peter Dettmance661b22015-02-07 14:43:51 +07001569
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001570 /* Store results */
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 MPI_ECP_MOV(&R->X, &tmp[2]);
1572 MPI_ECP_MOV(&R->Y, &tmp[1]);
1573 MPI_ECP_MOV(&R->Z, &tmp[3]);
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001574
1575cleanup:
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001578#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) */
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001579}
1580
1581/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001582 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001583 *
1584 * The coordinates of Q must be normalized (= affine),
1585 * but those of P don't need to. R is not normalized.
1586 *
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001587 * P,Q,R may alias, but only at the level of EC points: they must be either
1588 * equal as pointers, or disjoint (including the coordinate data buffers).
1589 * Fine-grained aliasing at the level of coordinates is not supported.
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é-Gonnard04a02252013-11-20 22:57:38 +01001599 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1602 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
1603 mbedtls_mpi tmp[4])
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001606 add_count++;
1607#endif
1608
Janos Follathb0697532016-08-18 12:38:46 +01001609#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (mbedtls_internal_ecp_grp_capable(grp)) {
1611 return mbedtls_internal_ecp_add_mixed(grp, R, P, Q);
1612 }
Janos Follath372697b2016-10-28 16:53:11 +01001613#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001614
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001615#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001617#else
1618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker838b7152022-01-04 05:01:53 +00001619
1620 /* NOTE: Aliasing between input and output is allowed, so one has to make
1621 * sure that at the point X,Y,Z are written, {P,Q}->{X,Y,Z} are no
1622 * longer read from. */
Hanno Becker5c8ea302022-01-01 06:01:45 +00001623 mbedtls_mpi * const X = &R->X;
1624 mbedtls_mpi * const Y = &R->Y;
1625 mbedtls_mpi * const Z = &R->Z;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (!MPI_ECP_VALID(&Q->Z)) {
1628 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1629 }
Hanno Beckeree95f6c2022-01-09 05:46:18 +00001630
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001631 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001632 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (MPI_ECP_CMP_INT(&P->Z, 0) == 0) {
1635 return mbedtls_ecp_copy(R, Q);
1636 }
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (MPI_ECP_CMP_INT(&Q->Z, 0) == 0) {
1639 return mbedtls_ecp_copy(R, P);
1640 }
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001641
1642 /*
1643 * Make sure Q coordinates are normalized
1644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if (MPI_ECP_CMP_INT(&Q->Z, 1) != 0) {
1646 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1647 }
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 MPI_ECP_SQR(&tmp[0], &P->Z);
1650 MPI_ECP_MUL(&tmp[1], &tmp[0], &P->Z);
1651 MPI_ECP_MUL(&tmp[0], &tmp[0], &Q->X);
1652 MPI_ECP_MUL(&tmp[1], &tmp[1], &Q->Y);
1653 MPI_ECP_SUB(&tmp[0], &tmp[0], &P->X);
1654 MPI_ECP_SUB(&tmp[1], &tmp[1], &P->Y);
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001655
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001656 /* Special cases (2) and (3) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (MPI_ECP_CMP_INT(&tmp[0], 0) == 0) {
1658 if (MPI_ECP_CMP_INT(&tmp[1], 0) == 0) {
1659 ret = ecp_double_jac(grp, R, P, tmp);
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001660 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 } else {
1662 ret = mbedtls_ecp_set_zero(R);
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001663 goto cleanup;
1664 }
1665 }
1666
Hanno Becker838b7152022-01-04 05:01:53 +00001667 /* {P,Q}->Z no longer used, so OK to write to Z even if there's aliasing. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 MPI_ECP_MUL(Z, &P->Z, &tmp[0]);
1669 MPI_ECP_SQR(&tmp[2], &tmp[0]);
1670 MPI_ECP_MUL(&tmp[3], &tmp[2], &tmp[0]);
1671 MPI_ECP_MUL(&tmp[2], &tmp[2], &P->X);
Hanno Beckerce29ae82022-01-04 04:55:11 +00001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MPI_ECP_MOV(&tmp[0], &tmp[2]);
1674 MPI_ECP_SHIFT_L(&tmp[0], 1);
Hanno Beckerce29ae82022-01-04 04:55:11 +00001675
Hanno Becker838b7152022-01-04 05:01:53 +00001676 /* {P,Q}->X no longer used, so OK to write to X even if there's aliasing. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 MPI_ECP_SQR(X, &tmp[1]);
1678 MPI_ECP_SUB(X, X, &tmp[0]);
1679 MPI_ECP_SUB(X, X, &tmp[3]);
1680 MPI_ECP_SUB(&tmp[2], &tmp[2], X);
1681 MPI_ECP_MUL(&tmp[2], &tmp[2], &tmp[1]);
1682 MPI_ECP_MUL(&tmp[3], &tmp[3], &P->Y);
Hanno Becker838b7152022-01-04 05:01:53 +00001683 /* {P,Q}->Y no longer used, so OK to write to Y even if there's aliasing. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 MPI_ECP_SUB(Y, &tmp[2], &tmp[3]);
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001685
1686cleanup:
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001689#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001690}
1691
1692/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001693 * Randomize jacobian coordinates:
1694 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001695 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001696 *
1697 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001699static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
1700 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001701{
Janos Follathb0697532016-08-18 12:38:46 +01001702#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 if (mbedtls_internal_ecp_grp_capable(grp)) {
1704 return mbedtls_internal_ecp_randomize_jac(grp, pt, f_rng, p_rng);
1705 }
Janos Follath372697b2016-10-28 16:53:11 +01001706#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001707
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001708#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001710#else
1711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d629792022-01-04 06:45:49 +00001712 mbedtls_mpi l;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 mbedtls_mpi_init(&l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001715
1716 /* Generate l such that 1 < l < p */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 MPI_ECP_RAND(&l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001718
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001719 /* Z' = l * Z */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 MPI_ECP_MUL(&pt->Z, &pt->Z, &l);
Hanno Becker0d629792022-01-04 06:45:49 +00001721
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001722 /* Y' = l * Y */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001724
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001725 /* X' = l^2 * X */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 MPI_ECP_SQR(&l, &l);
1727 MPI_ECP_MUL(&pt->X, &pt->X, &l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001728
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001729 /* Y'' = l^2 * Y' = l^3 * Y */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001731
1732cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 mbedtls_mpi_free(&l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Gilles Peskine59215172021-03-29 22:28:50 +02001736 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 }
1738 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001739#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) */
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001740}
1741
1742/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001743 * Check and define parameters used by the comb method (see below for details)
1744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1746#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001747#endif
1748
1749/* d = ceil( n / w ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750#define COMB_MAX_D (MBEDTLS_ECP_MAX_BITS + 1) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001751
1752/* number of precomputed points */
Gilles Peskine449bd832023-01-11 14:50:10 +01001753#define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001754
1755/*
1756 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001757 *
1758 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001759 * modified version that provides resistance to SPA by avoiding zero
1760 * digits in the representation as in [3]. We modify the method further by
1761 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001762 * representation uses one more K_i, due to carries, but saves on the size of
1763 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001764 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001765 * Summary of the comb method and its modifications:
1766 *
1767 * - The goal is to compute m*P for some w*d-bit integer m.
1768 *
1769 * - The basic comb method splits m into the w-bit integers
1770 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1771 * index has residue i modulo d, and computes m * P as
1772 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1773 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1774 *
1775 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1776 * .. + 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]] ..,
1777 * thereby successively converting it into a form where all summands
1778 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1779 *
1780 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1781 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1782 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1783 * Performing and iterating this procedure for those x[i] that are even
1784 * (keeping track of carry), we can transform the original sum into one of the form
1785 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1786 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1787 * which is why we are only computing half of it in the first place in
1788 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1789 *
1790 * - For the sake of compactness, only the seven low-order bits of x[i]
1791 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001792 * 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 +02001793 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001794 *
1795 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001796 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001797 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001799 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1800 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802static void ecp_comb_recode_core(unsigned char x[], size_t d,
1803 unsigned char w, const mbedtls_mpi *m)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001804{
1805 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001806 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 memset(x, 0, d+1);
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001809
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001810 /* First get the classical comb values (except for x_d = 0) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 for (i = 0; i < d; i++) {
1812 for (j = 0; j < w; j++) {
1813 x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j;
1814 }
1815 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001816
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001817 /* Now make sure x_1 .. x_d are odd */
1818 c = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 for (i = 1; i <= d; i++) {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001820 /* Add carry and update it */
1821 cc = x[i] & c;
1822 x[i] = x[i] ^ c;
1823 c = cc;
1824
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001825 /* Adjust if needed, avoiding branches */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 adjust = 1 - (x[i] & 0x01);
1827 c |= x[i] & (x[i-1] * adjust);
1828 x[i] = x[i] ^ (x[i-1] * adjust);
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001829 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001830 }
1831}
1832
1833/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001834 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001835 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001836 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001837 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001838 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1839 * 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 +01001840 *
1841 * 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 +02001842 *
1843 * Note: Even comb values (those where P would be omitted from the
1844 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001845 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001846 *
1847 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001848 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001849 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1850 * (3) [add] Computation of all T[i]
1851 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001852 *
1853 * Step 1 can be interrupted but not the others; together with the final
1854 * coordinate normalization they are the largest steps done at once, depending
1855 * on the window size. Here are operation counts for P-256:
1856 *
1857 * step (2) (3) (4)
1858 * w = 5 142 165 208
1859 * w = 4 136 77 160
1860 * w = 3 130 33 136
1861 * w = 2 124 11 124
1862 *
1863 * So if ECC operations are blocking for too long even with a low max_ops
1864 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1865 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001867static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
1868 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
1869 unsigned char w, size_t d,
1870 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001871{
Janos Follath24eed8d2019-11-22 13:21:35 +00001872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001873 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001874 size_t j = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 const unsigned char T_size = 1U << (w - 1);
1876 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1] = { NULL };
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001877
Hanno Beckera7f8edd2022-01-04 07:29:46 +00001878 mbedtls_mpi tmp[4];
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Beckera7f8edd2022-01-04 07:29:46 +00001881
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001882#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1884 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001885 goto dbl;
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 }
1887 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) {
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001888 goto norm_dbl;
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 }
1890 if (rs_ctx->rsm->state == ecp_rsm_pre_add) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001891 goto add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 }
1893 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001894 goto norm_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 }
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001896 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001897#else
1898 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001899#endif
1900
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001901#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001903 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1904
1905 /* initial state for the loop */
1906 rs_ctx->rsm->i = 0;
1907 }
1908
1909dbl:
1910#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001911 /*
1912 * Set T[0] = P and
1913 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1914 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&T[0], P));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001916
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001917#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001919 j = rs_ctx->rsm->i;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 } else
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001921#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 j = 0;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 for (; j < d * (w - 1); j++) {
1925 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL);
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 i = 1U << (j / d);
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001928 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (j % d == 0) {
1931 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(cur, T + (i >> 1)));
1932 }
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 MBEDTLS_MPI_CHK(ecp_double_jac(grp, cur, cur, tmp));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001935 }
1936
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001937#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001939 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001941
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001942norm_dbl:
1943#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001944 /*
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001945 * Normalize current elements in T to allow them to be used in
1946 * ecp_add_mixed() below, which requires one normalized input.
1947 *
1948 * As T has holes, use an auxiliary array of pointers to elements in T.
1949 *
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001950 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001951 j = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 for (i = 1; i < T_size; i <<= 1) {
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001953 TT[j++] = T + i;
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 }
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001959
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001960#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001962 rs_ctx->rsm->state = ecp_rsm_pre_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001964
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001965add:
1966#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001967 /*
1968 * Compute the remaining ones using the minimal number of additions
1969 * Be careful to update T[2^l] only after using it!
1970 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 for (i = 1; i < T_size; i <<= 1) {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001974 j = i;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 while (j--) {
1976 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, &T[i + j], &T[j], &T[i], tmp));
1977 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001978 }
1979
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001980#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001982 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001984
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001985norm_add:
1986#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001987 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001988 * Normalize final elements in T. Even though there are no holes now, we
1989 * still need the auxiliary array for homogeneity with the previous
1990 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001991 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 for (j = 0; j + 1 < T_size; j++) {
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001993 TT[j] = T + j + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 }
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001999
Hanno Beckeree95f6c2022-01-09 05:46:18 +00002000 /* Free Z coordinate (=1 after normalization) to save RAM.
2001 * This makes T[i] invalid as mbedtls_ecp_points, but this is OK
2002 * since from this point onwards, they are only accessed indirectly
2003 * via the getter function ecp_select_comb() which does set the
2004 * target's Z coordinate to 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 for (i = 0; i < T_size; i++) {
2006 mbedtls_mpi_free(&T[i].Z);
2007 }
Hanno Beckeree95f6c2022-01-09 05:46:18 +00002008
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002009cleanup:
Hanno Beckera7f8edd2022-01-04 07:29:46 +00002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Beckera7f8edd2022-01-04 07:29:46 +00002012
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002013#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
2015 ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2016 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002017 rs_ctx->rsm->i = j;
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 }
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01002019 }
2020#endif
Janos Follathb0697532016-08-18 12:38:46 +01002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002023}
2024
2025/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01002026 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002027 *
2028 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2031 const mbedtls_ecp_point T[], unsigned char T_size,
2032 unsigned char i)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002033{
Janos Follath24eed8d2019-11-22 13:21:35 +00002034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002035 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002036
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002037 /* Ignore the "sign" bit and scale down */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 ii = (i & 0x7Fu) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002039
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002040 /* Read the whole table to thwart cache-based timing attacks */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 for (j = 0; j < T_size; j++) {
2042 MPI_ECP_COND_ASSIGN(&R->X, &T[j].X, j == ii);
2043 MPI_ECP_COND_ASSIGN(&R->Y, &T[j].Y, j == ii);
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002044 }
2045
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01002046 /* Safely invert result if i is "negative" */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, R, i >> 7));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MPI_ECP_LSET(&R->Z, 1);
Hanno Becker6a288702022-01-05 05:19:48 +00002050
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002051cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002053}
2054
2055/*
2056 * Core multiplication algorithm for the (modified) comb method.
2057 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01002058 *
2059 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002060 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2062 const mbedtls_ecp_point T[], unsigned char T_size,
2063 const unsigned char x[], size_t d,
2064 int (*f_rng)(void *, unsigned char *, size_t),
2065 void *p_rng,
2066 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002067{
Janos Follath24eed8d2019-11-22 13:21:35 +00002068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 mbedtls_ecp_point Txi;
Hanno Beckera7f8edd2022-01-04 07:29:46 +00002070 mbedtls_mpi tmp[4];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002071 size_t i;
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 mbedtls_ecp_point_init(&Txi);
2074 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002075
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002076#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002077 (void) rs_ctx;
2078#endif
2079
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002080#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
2082 rs_ctx->rsm->state != ecp_rsm_comb_core) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002083 rs_ctx->rsm->i = 0;
2084 rs_ctx->rsm->state = ecp_rsm_comb_core;
2085 }
2086
2087 /* new 'if' instead of nested for the sake of the 'else' branch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002089 /* restore current index (R already pointing to rs_ctx->rsm->R) */
2090 i = rs_ctx->rsm->i;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 } else
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002092#endif
2093 {
2094 /* Start with a non-zero point and randomize its coordinates */
2095 i = d;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 MBEDTLS_MPI_CHK(ecp_select_comb(grp, R, T, T_size, x[i]));
2097 if (f_rng != 0) {
2098 MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, R, f_rng, p_rng));
2099 }
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002100 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 while (i != 0) {
2103 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD);
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002104 --i;
2105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_MPI_CHK(ecp_double_jac(grp, R, R, tmp));
2107 MBEDTLS_MPI_CHK(ecp_select_comb(grp, &Txi, T, T_size, x[i]));
2108 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, R, R, &Txi, tmp));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002109 }
2110
2111cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 mbedtls_ecp_point_free(&Txi);
2114 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Beckera7f8edd2022-01-04 07:29:46 +00002115
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002116#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
2118 ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002119 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002120 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002121 }
2122#endif
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002125}
2126
2127/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002128 * Recode the scalar to get constant-time comb multiplication
2129 *
2130 * As the actual scalar recoding needs an odd scalar as a starting point,
2131 * this wrapper ensures that by replacing m by N - m if necessary, and
2132 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002133 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002134 * This works because we only support large prime order for Short Weierstrass
2135 * curves, so N is always odd hence either m or N - m is.
2136 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002137 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139static int ecp_comb_recode_scalar(const mbedtls_ecp_group *grp,
2140 const mbedtls_mpi *m,
2141 unsigned char k[COMB_MAX_D + 1],
2142 size_t d,
2143 unsigned char w,
2144 unsigned char *parity_trick)
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002145{
Janos Follath24eed8d2019-11-22 13:21:35 +00002146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002147 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 mbedtls_mpi_init(&M);
2150 mbedtls_mpi_init(&mm);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002151
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002152 /* N is always odd (see above), just make extra sure */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) {
2154 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2155 }
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002156
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002157 /* do we need the parity trick? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 *parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0);
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002159
2160 /* execute parity fix in constant time */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m));
2162 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m));
2163 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick));
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002164
2165 /* actual scalar recoding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 ecp_comb_recode_core(k, d, w, &M);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002167
2168cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 mbedtls_mpi_free(&mm);
2170 mbedtls_mpi_free(&M);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 return ret;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002173}
2174
2175/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002176 * Perform comb multiplication (for short Weierstrass curves)
2177 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002178 *
2179 * Scalar recoding may use a parity trick that makes us compute -m * P,
2180 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182static int ecp_mul_comb_after_precomp(const mbedtls_ecp_group *grp,
2183 mbedtls_ecp_point *R,
2184 const mbedtls_mpi *m,
2185 const mbedtls_ecp_point *T,
2186 unsigned char T_size,
2187 unsigned char w,
2188 size_t d,
2189 int (*f_rng)(void *, unsigned char *, size_t),
2190 void *p_rng,
2191 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002192{
Janos Follath24eed8d2019-11-22 13:21:35 +00002193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002194 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002195 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002196 mbedtls_ecp_point *RR = R;
2197
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002198#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002200 RR = &rs_ctx->rsm->R;
2201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (rs_ctx->rsm->state == ecp_rsm_final_norm) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002203 goto final_norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002205 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002206#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w,
2209 &parity_trick));
2210 MBEDTLS_MPI_CHK(ecp_mul_comb_core(grp, RR, T, T_size, k, d,
2211 f_rng, p_rng, rs_ctx));
2212 MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, RR, parity_trick));
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002213
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002214#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002216 rs_ctx->rsm->state = ecp_rsm_final_norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 }
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002218
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002219final_norm:
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002221#endif
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002222 /*
2223 * Knowledge of the jacobian coordinates may leak the last few bits of the
2224 * scalar [1], and since our MPI implementation isn't constant-flow,
2225 * inversion (used for coordinate normalization) may leak the full value
2226 * of its input via side-channels [2].
2227 *
2228 * [1] https://eprint.iacr.org/2003/191
2229 * [2] https://eprint.iacr.org/2020/055
2230 *
2231 * Avoid the leak by randomizing coordinates before we normalize them.
2232 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (f_rng != 0) {
2234 MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, RR, f_rng, p_rng));
2235 }
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, RR));
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002238
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002239#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
2241 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, RR));
2242 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002243#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002244
2245cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 return ret;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002247}
2248
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002249/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002250 * Pick window size based on curve size and whether we optimize for base point
2251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002252static unsigned char ecp_pick_window_size(const mbedtls_ecp_group *grp,
2253 unsigned char p_eq_g)
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002254{
2255 unsigned char w;
2256
2257 /*
2258 * Minimize the number of multiplications, that is minimize
2259 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2260 * (see costs of the various parts, with 1S = 1M)
2261 */
2262 w = grp->nbits >= 384 ? 5 : 4;
2263
2264 /*
2265 * If P == G, pre-compute a bit more, since this may be re-used later.
2266 * Just adding one avoids upping the cost of the first mul too much,
2267 * and the memory cost too.
2268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 if (p_eq_g) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002270 w++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 }
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002272
2273 /*
kXuanba9cb762021-04-08 14:32:06 +08002274 * If static comb table may not be used (!p_eq_g) or static comb table does
2275 * not exists, make sure w is within bounds.
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002276 * (The last test is useful only for very small curves in the test suite.)
kXuanba9cb762021-04-08 14:32:06 +08002277 *
2278 * The user reduces MBEDTLS_ECP_WINDOW_SIZE does not changes the size of
2279 * static comb table, because the size of static comb table is fixed when
2280 * it is generated.
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282#if (MBEDTLS_ECP_WINDOW_SIZE < 6)
2283 if ((!p_eq_g || !ecp_group_is_static_comb_table(grp)) && w > MBEDTLS_ECP_WINDOW_SIZE) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002284 w = MBEDTLS_ECP_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 }
k-stachowiak653a4a22019-07-03 14:31:09 +02002286#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if (w >= grp->nbits) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002288 w = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 }
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 return w;
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002292}
2293
2294/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002295 * Multiplication using the comb method - for curves in short Weierstrass form
2296 *
2297 * This function is mainly responsible for administrative work:
2298 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002299 * - managing the table of precomputed points (passed between the below two
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002300 * functions): allocation, computation, ownership transfer, freeing.
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002301 *
2302 * It delegates the actual arithmetic work to:
2303 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2304 *
2305 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2308 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2309 int (*f_rng)(void *, unsigned char *, size_t),
2310 void *p_rng,
2311 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002312{
Janos Follath24eed8d2019-11-22 13:21:35 +00002313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002314 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002315 size_t d;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002316 unsigned char T_size = 0, T_ok = 0;
2317 mbedtls_ecp_point *T = NULL;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 ECP_RS_ENTER(rsm);
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002320
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002321 /* Is P the base point ? */
2322#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 p_eq_g = (MPI_ECP_CMP(&P->Y, &grp->G.Y) == 0 &&
2324 MPI_ECP_CMP(&P->X, &grp->G.X) == 0);
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002325#else
2326 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002327#endif
2328
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002329 /* Pick window size and deduce related sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 w = ecp_pick_window_size(grp, p_eq_g);
2331 T_size = 1U << (w - 1);
2332 d = (grp->nbits + w - 1) / w;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002333
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002334 /* Pre-computed table: do we have it already for the base point? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (p_eq_g && grp->T != NULL) {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002336 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002337 T = grp->T;
2338 T_ok = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 } else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002340#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002341 /* Pre-computed table: do we have one in progress? complete? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002343 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002344 T = rs_ctx->rsm->T;
2345 rs_ctx->rsm->T = NULL;
2346 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002347
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002348 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002349 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 } else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002351#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002352 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002353 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 T = mbedtls_calloc(T_size, sizeof(mbedtls_ecp_point));
2355 if (T == NULL) {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002356 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002357 goto cleanup;
2358 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 for (i = 0; i < T_size; i++) {
2361 mbedtls_ecp_point_init(&T[i]);
2362 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002363
2364 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002365 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002366
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002367 /* Compute table (or finish computing it) if not done already */
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (!T_ok) {
2369 MBEDTLS_MPI_CHK(ecp_precompute_comb(grp, T, P, w, d, rs_ctx));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (p_eq_g) {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002372 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002373 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002374 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002375 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002376 }
2377 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002378
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002379 /* Actual comb multiplication using precomputed points */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m,
2381 T, T_size, w, d,
2382 f_rng, p_rng, rs_ctx));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002383
2384cleanup:
2385
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002386 /* does T belong to the group? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (T == grp->T) {
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002388 T = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 }
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002390
2391 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002392#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 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 +01002394 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002395 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002396 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002397 T = NULL;
2398 }
2399#endif
2400
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002401 /* did T belong to us? then let's destroy it! */
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (T != NULL) {
2403 for (i = 0; i < T_size; i++) {
2404 mbedtls_ecp_point_free(&T[i]);
2405 }
2406 mbedtls_free(T);
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002407 }
2408
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002409 /* prevent caller from using invalid value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 int should_free_R = (ret != 0);
David Horstmannfc735df2022-10-06 19:11:04 +01002411#if defined(MBEDTLS_ECP_RESTARTABLE)
2412 /* don't free R while in progress in case R == P */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
David Horstmann6e116872022-10-25 10:32:08 +01002414 should_free_R = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 }
David Horstmannfc735df2022-10-06 19:11:04 +01002416#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (should_free_R) {
2418 mbedtls_ecp_point_free(R);
2419 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 ECP_RS_LEAVE(rsm);
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002424}
2425
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002426#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002427
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002428#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002429/*
2430 * For Montgomery curves, we do all the internal arithmetic in projective
2431 * coordinates. Import/export of points uses only the x coordinates, which is
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002432 * internally represented as X / Z.
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002433 *
2434 * For scalar multiplication, we'll use a Montgomery ladder.
2435 */
2436
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002437/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002438 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2439 * Cost: 1M + 1I
2440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002442{
Janos Follathb0697532016-08-18 12:38:46 +01002443#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (mbedtls_internal_ecp_grp_capable(grp)) {
2445 return mbedtls_internal_ecp_normalize_mxz(grp, P);
2446 }
Janos Follath372697b2016-10-28 16:53:11 +01002447#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002448
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002449#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002451#else
2452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 MPI_ECP_INV(&P->Z, &P->Z);
2454 MPI_ECP_MUL(&P->X, &P->X, &P->Z);
2455 MPI_ECP_LSET(&P->Z, 1);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002456
2457cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002459#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002460}
2461
2462/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002463 * Randomize projective x/z coordinates:
2464 * (X, Z) -> (l X, l Z) for random l
2465 * This is sort of the reverse operation of ecp_normalize_mxz().
2466 *
2467 * This countermeasure was first suggested in [2].
2468 * Cost: 2M
2469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
2471 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002472{
Janos Follathb0697532016-08-18 12:38:46 +01002473#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 if (mbedtls_internal_ecp_grp_capable(grp)) {
2475 return mbedtls_internal_ecp_randomize_mxz(grp, P, f_rng, p_rng);
2476 }
Janos Follath372697b2016-10-28 16:53:11 +01002477#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002478
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002479#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002481#else
2482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2483 mbedtls_mpi l;
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 mbedtls_mpi_init(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002485
2486 /* Generate l such that 1 < l < p */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 MPI_ECP_RAND(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 MPI_ECP_MUL(&P->X, &P->X, &l);
2490 MPI_ECP_MUL(&P->Z, &P->Z, &l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002491
2492cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 mbedtls_mpi_free(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Gilles Peskine59215172021-03-29 22:28:50 +02002496 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 }
2498 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002499#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002500}
2501
2502/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002503 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2504 * for Montgomery curves in x/z coordinates.
2505 *
2506 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2507 * with
2508 * d = X1
2509 * P = (X2, Z2)
2510 * Q = (X3, Z3)
2511 * R = (X4, Z4)
2512 * S = (X5, Z5)
2513 * and eliminating temporary variables tO, ..., t4.
2514 *
2515 * Cost: 5M + 4S
2516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002517static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
2518 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2519 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2520 const mbedtls_mpi *d,
2521 mbedtls_mpi T[4])
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002522{
Janos Follathb0697532016-08-18 12:38:46 +01002523#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (mbedtls_internal_ecp_grp_capable(grp)) {
2525 return mbedtls_internal_ecp_double_add_mxz(grp, R, S, P, Q, d);
2526 }
Janos Follath372697b2016-10-28 16:53:11 +01002527#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002528
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002529#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002531#else
2532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 MPI_ECP_ADD(&T[0], &P->X, &P->Z); /* Pp := PX + PZ */
2535 MPI_ECP_SUB(&T[1], &P->X, &P->Z); /* Pm := PX - PZ */
2536 MPI_ECP_ADD(&T[2], &Q->X, &Q->Z); /* Qp := QX + XZ */
2537 MPI_ECP_SUB(&T[3], &Q->X, &Q->Z); /* Qm := QX - QZ */
2538 MPI_ECP_MUL(&T[3], &T[3], &T[0]); /* Qm * Pp */
2539 MPI_ECP_MUL(&T[2], &T[2], &T[1]); /* Qp * Pm */
2540 MPI_ECP_SQR(&T[0], &T[0]); /* Pp^2 */
2541 MPI_ECP_SQR(&T[1], &T[1]); /* Pm^2 */
2542 MPI_ECP_MUL(&R->X, &T[0], &T[1]); /* Pp^2 * Pm^2 */
2543 MPI_ECP_SUB(&T[0], &T[0], &T[1]); /* Pp^2 - Pm^2 */
2544 MPI_ECP_MUL(&R->Z, &grp->A, &T[0]); /* A * (Pp^2 - Pm^2) */
2545 MPI_ECP_ADD(&R->Z, &T[1], &R->Z); /* [ A * (Pp^2-Pm^2) ] + Pm^2 */
2546 MPI_ECP_ADD(&S->X, &T[3], &T[2]); /* Qm*Pp + Qp*Pm */
2547 MPI_ECP_SQR(&S->X, &S->X); /* (Qm*Pp + Qp*Pm)^2 */
2548 MPI_ECP_SUB(&S->Z, &T[3], &T[2]); /* Qm*Pp - Qp*Pm */
2549 MPI_ECP_SQR(&S->Z, &S->Z); /* (Qm*Pp - Qp*Pm)^2 */
2550 MPI_ECP_MUL(&S->Z, d, &S->Z); /* d * ( Qm*Pp - Qp*Pm )^2 */
2551 MPI_ECP_MUL(&R->Z, &T[0], &R->Z); /* [A*(Pp^2-Pm^2)+Pm^2]*(Pp^2-Pm^2) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002552
2553cleanup:
Hanno Becker28ccb1c2022-01-04 07:15:04 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 return ret;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002556#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002557}
2558
2559/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002560 * Multiplication with Montgomery ladder in x/z coordinates,
2561 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2564 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2565 int (*f_rng)(void *, unsigned char *, size_t),
2566 void *p_rng)
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002567{
Janos Follath24eed8d2019-11-22 13:21:35 +00002568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002569 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002570 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 mbedtls_ecp_point RP;
2572 mbedtls_mpi PX;
Hanno Becker30838862022-01-04 13:25:59 +00002573 mbedtls_mpi tmp[4];
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 mbedtls_ecp_point_init(&RP); mbedtls_mpi_init(&PX);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Becker30838862022-01-04 13:25:59 +00002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 if (f_rng == NULL) {
2579 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2580 }
Manuel Pégourié-Gonnard02b57052021-06-15 11:29:26 +02002581
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002582 /* Save PX and read from P before writing to R, in case P == R */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 MPI_ECP_MOV(&PX, &P->X);
2584 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&RP, P));
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002585
2586 /* Set R to zero in modified x/z coordinates */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 MPI_ECP_LSET(&R->X, 1);
2588 MPI_ECP_LSET(&R->Z, 0);
2589 mbedtls_mpi_free(&R->Y);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002590
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002591 /* RP.X might be slightly larger than P, so reduce it */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 MOD_ADD(&RP.X);
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002593
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002594 /* Randomize coordinates of the starting point */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, &RP, f_rng, p_rng));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002596
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002597 /* Loop invariant: R = result so far, RP = R + P */
Aurelien Jarnoc79ce882022-05-15 13:24:05 +02002598 i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 while (i-- > 0) {
2600 b = mbedtls_mpi_get_bit(m, i);
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002601 /*
2602 * if (b) R = 2R + P else R = 2R,
2603 * which is:
2604 * if (b) double_add( RP, R, RP, R )
2605 * else double_add( R, RP, R, RP )
2606 * but using safe conditional swaps to avoid leaks
2607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
2609 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
2610 MBEDTLS_MPI_CHK(ecp_double_add_mxz(grp, R, &RP, R, &RP, &PX, tmp));
2611 MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
2612 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002613 }
2614
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002615 /*
2616 * Knowledge of the projective coordinates may leak the last few bits of the
2617 * scalar [1], and since our MPI implementation isn't constant-flow,
2618 * inversion (used for coordinate normalization) may leak the full value
2619 * of its input via side-channels [2].
2620 *
2621 * [1] https://eprint.iacr.org/2003/191
2622 * [2] https://eprint.iacr.org/2020/055
2623 *
2624 * Avoid the leak by randomizing coordinates before we normalize them.
2625 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, R, f_rng, p_rng));
2627 MBEDTLS_MPI_CHK(ecp_normalize_mxz(grp, R));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002628
2629cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 mbedtls_ecp_point_free(&RP); mbedtls_mpi_free(&PX);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
2633 return ret;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002634}
2635
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002636#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002637
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002638/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002639 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002640 *
2641 * This internal function can be called without an RNG in case where we know
2642 * the inputs are not sensitive.
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002644static int ecp_mul_restartable_internal(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2645 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2647 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002648{
Janos Follathb0697532016-08-18 12:38:46 +01002649 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002650#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2651 char is_grp_capable = 0;
2652#endif
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02002653
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002654#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002655 /* reset ops count for this call if top-level */
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (rs_ctx != NULL && rs_ctx->depth++ == 0) {
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002657 rs_ctx->ops_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 }
Gilles Peskine59970052019-02-28 13:12:06 +01002659#else
2660 (void) rs_ctx;
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002661#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002662
Janos Follathc44ab972016-11-18 16:38:23 +00002663#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
2665 MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
2666 }
Janos Follathc44ab972016-11-18 16:38:23 +00002667#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002668
David Horstmannfc735df2022-10-06 19:11:04 +01002669 int restarting = 0;
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002670#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002672#endif
David Horstmannfc735df2022-10-06 19:11:04 +01002673 /* skip argument check when restarting */
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (!restarting) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002675 /* check_privkey is free */
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_CHK);
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002677
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002678 /* Common sanity checks */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m));
2680 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002681 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002682
2683 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002684#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2686 MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng));
2687 }
Janos Follath430d3372016-11-03 14:25:37 +00002688#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002689#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2691 MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx));
2692 }
Janos Follath430d3372016-11-03 14:25:37 +00002693#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002694
Janos Follath6c8ccd52016-11-29 15:37:09 +00002695cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002696
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002697#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (is_grp_capable) {
2699 mbedtls_internal_ecp_free(grp);
2700 }
Janos Follathc44ab972016-11-18 16:38:23 +00002701#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002702
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002703#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 if (rs_ctx != NULL) {
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002705 rs_ctx->depth--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002707#endif
2708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 return ret;
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002710}
2711
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002712/*
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002713 * Restartable multiplication R = m * P
2714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002715int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2716 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2717 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2718 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002719{
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (f_rng == NULL) {
2721 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2722 }
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 return ecp_mul_restartable_internal(grp, R, m, P, f_rng, p_rng, rs_ctx);
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002725}
2726
2727/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002728 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002729 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002730int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2731 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2732 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002733{
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002735}
Valerio Settifd122f42023-04-05 18:15:32 +02002736#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002737
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002738#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002739/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002740 * Check that an affine point is valid as a public key,
2741 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002743static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002744{
Janos Follath24eed8d2019-11-22 13:21:35 +00002745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002747
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002748 /* pt coordinates must be normalized for our checks */
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
2750 mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
2751 mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
2752 mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) {
2753 return MBEDTLS_ERR_ECP_INVALID_KEY;
2754 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 mbedtls_mpi_init(&YY); mbedtls_mpi_init(&RHS);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002757
2758 /*
2759 * YY = Y^2
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01002760 * RHS = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002761 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 MPI_ECP_SQR(&YY, &pt->Y);
2763 MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, &RHS, &pt->X));
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 if (MPI_ECP_CMP(&YY, &RHS) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002768
2769cleanup:
2770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 mbedtls_mpi_free(&YY); mbedtls_mpi_free(&RHS);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 return ret;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002774}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002775#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002776
Valerio Settifd122f42023-04-05 18:15:32 +02002777#if defined(MBEDTLS_ECP_C)
Gilles Peskine9b99a892018-09-14 18:32:19 +02002778#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002779/*
TRodziewicz9edff742021-03-04 17:59:39 +01002780 * R = m * P with shortcuts for m == 0, m == 1 and m == -1
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002781 * NOT constant-time - ONLY for short Weierstrass!
2782 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002783static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
2784 mbedtls_ecp_point *R,
2785 const mbedtls_mpi *m,
2786 const mbedtls_ecp_point *P,
2787 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002788{
Janos Follath24eed8d2019-11-22 13:21:35 +00002789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc27a0e02022-01-06 05:56:34 +00002790 mbedtls_mpi tmp;
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 mbedtls_mpi_init(&tmp);
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (mbedtls_mpi_cmp_int(m, 0) == 0) {
2794 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2795 MBEDTLS_MPI_CHK(mbedtls_ecp_set_zero(R));
2796 } else if (mbedtls_mpi_cmp_int(m, 1) == 0) {
2797 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2798 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2799 } else if (mbedtls_mpi_cmp_int(m, -1) == 0) {
2800 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2801 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2802 MPI_ECP_NEG(&R->Y);
2803 } else {
2804 MBEDTLS_MPI_CHK(ecp_mul_restartable_internal(grp, R, m, P,
2805 NULL, NULL, rs_ctx));
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002806 }
2807
2808cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 mbedtls_mpi_free(&tmp);
Hanno Beckerc27a0e02022-01-06 05:56:34 +00002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 return ret;
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002812}
2813
2814/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002815 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002816 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002817 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002818int mbedtls_ecp_muladd_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2820 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2821 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2822 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002823{
Janos Follath24eed8d2019-11-22 13:21:35 +00002824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002825 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002826 mbedtls_ecp_point *pmP = &mP;
2827 mbedtls_ecp_point *pR = R;
Hanno Becker3b29f212022-01-04 07:34:14 +00002828 mbedtls_mpi tmp[4];
Janos Follathc44ab972016-11-18 16:38:23 +00002829#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2830 char is_grp_capable = 0;
2831#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2833 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
2834 }
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 mbedtls_ecp_point_init(&mP);
2837 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Becker3b29f212022-01-04 07:34:14 +00002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 ECP_RS_ENTER(ma);
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002840
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002841#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002843 /* redirect intermediate results to restart context */
2844 pmP = &rs_ctx->ma->mP;
2845 pR = &rs_ctx->ma->R;
2846
2847 /* jump to next operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (rs_ctx->ma->state == ecp_rsma_mul2) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002849 goto mul2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 }
2851 if (rs_ctx->ma->state == ecp_rsma_add) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002852 goto add;
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 }
2854 if (rs_ctx->ma->state == ecp_rsma_norm) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002855 goto norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 }
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002857 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002858#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx));
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002861#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002863 rs_ctx->ma->state = ecp_rsma_mul2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 }
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002865
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002866mul2:
2867#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pR, n, Q, rs_ctx));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002869
2870#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
2872 MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
2873 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002874#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2875
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002876#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002878 rs_ctx->ma->state = ecp_rsma_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 }
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002880
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002881add:
2882#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_ADD);
2884 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, pR, pmP, pR, tmp));
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002885#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002887 rs_ctx->ma->state = ecp_rsma_norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 }
Janos Follath430d3372016-11-03 14:25:37 +00002889
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002890norm:
2891#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
2893 MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, pR));
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002894
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002895#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2897 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, pR));
2898 }
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002899#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002900
2901cleanup:
Hanno Becker3b29f212022-01-04 07:34:14 +00002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Becker3b29f212022-01-04 07:34:14 +00002904
Janos Follathc44ab972016-11-18 16:38:23 +00002905#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 if (is_grp_capable) {
2907 mbedtls_internal_ecp_free(grp);
2908 }
Janos Follathc44ab972016-11-18 16:38:23 +00002909#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 mbedtls_ecp_point_free(&mP);
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 ECP_RS_LEAVE(ma);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 return ret;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002916}
2917
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002918/*
2919 * Linear combination
2920 * NOT constant-time
2921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2923 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2924 const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002925{
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002927}
Gilles Peskine9b99a892018-09-14 18:32:19 +02002928#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Valerio Settifd122f42023-04-05 18:15:32 +02002929#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002930
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002931#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002932#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Dave Rodgmanb8f18852023-07-18 12:45:17 +01002933#define ECP_MPI_INIT(_p, _n) { .p = (mbedtls_mpi_uint *) (_p), .s = 1, .n = (_n) }
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002934#define ECP_MPI_INIT_ARRAY(x) \
Dave Rodgmanb8f18852023-07-18 12:45:17 +01002935 ECP_MPI_INIT(x, sizeof(x) / sizeof(mbedtls_mpi_uint))
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002936/*
2937 * Constants for the two points other than 0, 1, -1 (mod p) in
2938 * https://cr.yp.to/ecdh.html#validate
2939 * See ecp_check_pubkey_x25519().
2940 */
2941static const mbedtls_mpi_uint x25519_bad_point_1[] = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 MBEDTLS_BYTES_TO_T_UINT_8(0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae),
2943 MBEDTLS_BYTES_TO_T_UINT_8(0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a),
2944 MBEDTLS_BYTES_TO_T_UINT_8(0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd),
2945 MBEDTLS_BYTES_TO_T_UINT_8(0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00),
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002946};
2947static const mbedtls_mpi_uint x25519_bad_point_2[] = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 MBEDTLS_BYTES_TO_T_UINT_8(0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24),
2949 MBEDTLS_BYTES_TO_T_UINT_8(0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b),
2950 MBEDTLS_BYTES_TO_T_UINT_8(0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86),
2951 MBEDTLS_BYTES_TO_T_UINT_8(0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57),
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002952};
2953static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 x25519_bad_point_1);
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002955static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 x25519_bad_point_2);
Janos Follath865a75e2021-06-24 15:34:59 +01002957#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002958
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002959/*
2960 * Check that the input point is not one of the low-order points.
2961 * This is recommended by the "May the Fourth" paper:
2962 * https://eprint.iacr.org/2017/806.pdf
2963 * Those points are never sent by an honest peer.
2964 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965static int ecp_check_bad_points_mx(const mbedtls_mpi *X, const mbedtls_mpi *P,
2966 const mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002967{
2968 int ret;
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002969 mbedtls_mpi XmP;
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 mbedtls_mpi_init(&XmP);
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002972
2973 /* Reduce X mod P so that we only need to check values less than P.
2974 * We know X < 2^256 so we can proceed by subtraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&XmP, X));
2976 while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) {
2977 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&XmP, &XmP, P));
2978 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002979
Janos Follath865a75e2021-06-24 15:34:59 +01002980 /* Check against the known bad values that are less than P. For Curve448
2981 * these are 0, 1 and -1. For Curve25519 we check the values less than P
2982 * from the following list: https://cr.yp.to/ecdh.html#validate */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) { /* takes care of 0 and 1 */
Janos Follath8081ced2021-06-24 14:24:13 +01002984 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2985 goto cleanup;
2986 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002987
Janos Follath865a75e2021-06-24 15:34:59 +01002988#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
2990 if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) {
Janos Follath865a75e2021-06-24 15:34:59 +01002991 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2992 goto cleanup;
2993 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) {
Janos Follath865a75e2021-06-24 15:34:59 +01002996 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2997 goto cleanup;
2998 }
Janos Follath8081ced2021-06-24 14:24:13 +01002999 }
Janos Follath83e384d2021-06-25 15:29:56 +01003000#else
3001 (void) grp_id;
Janos Follath865a75e2021-06-24 15:34:59 +01003002#endif
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02003003
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02003004 /* Final check: check if XmP + 1 is P (final because it changes XmP!) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&XmP, &XmP, 1));
3006 if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) {
Janos Follath8081ced2021-06-24 14:24:13 +01003007 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3008 goto cleanup;
3009 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02003010
3011 ret = 0;
3012
3013cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 mbedtls_mpi_free(&XmP);
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 return ret;
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02003017}
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02003018
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003019/*
3020 * Check validity of a public key for Montgomery curves with x-only schemes
3021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003022static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003023{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02003024 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003025 /* Allow any public value, if it's too big then we'll just reduce it mod p
3026 * (RFC 7748 sec. 5 para. 3). */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) {
3028 return MBEDTLS_ERR_ECP_INVALID_KEY;
3029 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003030
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02003031 /* Implicit in all standards (as they don't consider negative numbers):
3032 * X must be non-negative. This is normally ensured by the way it's
3033 * encoded for transmission, but let's be extra sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) {
3035 return MBEDTLS_ERR_ECP_INVALID_KEY;
3036 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id);
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003039}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003040#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003041
3042/*
3043 * Check that a point is valid as a public key
3044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003045int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
3046 const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003047{
3048 /* Must use affine coordinates */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) {
3050 return MBEDTLS_ERR_ECP_INVALID_KEY;
3051 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003052
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003053#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3055 return ecp_check_pubkey_mx(grp, pt);
3056 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003057#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003058#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3060 return ecp_check_pubkey_sw(grp, pt);
3061 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003062#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01003064}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003065
3066/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
3070 const mbedtls_mpi *d)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003071{
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003072#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003074 /* see RFC 7748 sec. 5 para. 5 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 if (mbedtls_mpi_get_bit(d, 0) != 0 ||
3076 mbedtls_mpi_get_bit(d, 1) != 0 ||
3077 mbedtls_mpi_bitlen(d) - 1 != grp->nbits) { /* mbedtls_mpi_bitlen is one-based! */
3078 return MBEDTLS_ERR_ECP_INVALID_KEY;
3079 }
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003080
3081 /* see [Curve25519] page 5 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) {
3083 return MBEDTLS_ERR_ECP_INVALID_KEY;
3084 }
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return 0;
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003087 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003088#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3089#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003091 /* see SEC1 3.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
3093 mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
3094 return MBEDTLS_ERR_ECP_INVALID_KEY;
3095 } else {
3096 return 0;
3097 }
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01003098 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003099#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003102}
3103
Gilles Peskine72fcc982021-03-23 22:31:31 +01003104#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3105MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +01003106int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
3107 mbedtls_mpi *d,
3108 int (*f_rng)(void *, unsigned char *, size_t),
3109 void *p_rng)
Gilles Peskine72fcc982021-03-23 22:31:31 +01003110{
3111 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskine61f1f5f2021-03-24 12:46:46 +01003112 size_t n_random_bytes = high_bit / 8 + 1;
Gilles Peskine72fcc982021-03-23 22:31:31 +01003113
3114 /* [Curve25519] page 5 */
Gilles Peskine61f1f5f2021-03-24 12:46:46 +01003115 /* Generate a (high_bit+1)-bit random number by generating just enough
3116 * random bytes, then shifting out extra bits from the top (necessary
3117 * when (high_bit+1) is not a multiple of 8). */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(d, n_random_bytes,
3119 f_rng, p_rng));
3120 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1));
Gilles Peskine72fcc982021-03-23 22:31:31 +01003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, high_bit, 1));
Gilles Peskine72fcc982021-03-23 22:31:31 +01003123
3124 /* Make sure the last two bits are unset for Curve448, three bits for
3125 Curve25519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 0, 0));
3127 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 1, 0));
3128 if (high_bit == 254) {
3129 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 2, 0));
Gilles Peskine72fcc982021-03-23 22:31:31 +01003130 }
3131
3132cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 return ret;
Gilles Peskine72fcc982021-03-23 22:31:31 +01003134}
3135#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3136
Gilles Peskine60d8b982021-03-29 22:28:21 +02003137#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3138static int mbedtls_ecp_gen_privkey_sw(
3139 const mbedtls_mpi *N, mbedtls_mpi *d,
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine60d8b982021-03-29 22:28:21 +02003141{
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 int ret = mbedtls_mpi_random(d, 1, N, f_rng, p_rng);
3143 switch (ret) {
Gilles Peskine60d8b982021-03-29 22:28:21 +02003144 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Gilles Peskine60d8b982021-03-29 22:28:21 +02003146 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 return ret;
Gilles Peskine60d8b982021-03-29 22:28:21 +02003148 }
3149}
3150#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3151
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02003152/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003153 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003155int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
3156 mbedtls_mpi *d,
3157 int (*f_rng)(void *, unsigned char *, size_t),
3158 void *p_rng)
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003159{
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003160#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3162 return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng);
3163 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003164#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003165
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003166#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3168 return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng);
3169 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003170#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003173}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01003174
Valerio Settifd122f42023-04-05 18:15:32 +02003175#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003176/*
3177 * Generate a keypair with configurable base point
3178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003179int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
3180 const mbedtls_ecp_point *G,
3181 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3182 int (*f_rng)(void *, unsigned char *, size_t),
3183 void *p_rng)
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003184{
Janos Follath24eed8d2019-11-22 13:21:35 +00003185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
3187 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, Q, d, G, f_rng, p_rng));
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003188
3189cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 return ret;
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003191}
3192
3193/*
3194 * Generate key pair, wrapper for conventional base point
3195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003196int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
3197 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3198 int (*f_rng)(void *, unsigned char *, size_t),
3199 void *p_rng)
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003200{
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003202}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003203
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003204/*
3205 * Generate a keypair, prettier wrapper
3206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003207int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3208 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003209{
Janos Follath24eed8d2019-11-22 13:21:35 +00003210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3212 return ret;
3213 }
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng);
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003216}
Valerio Settifd122f42023-04-05 18:15:32 +02003217#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003218
Janos Follath77800962019-02-25 16:32:08 +00003219#define ECP_CURVE25519_KEY_SIZE 32
Archana1d2e2bb2021-06-07 06:13:16 +05303220#define ECP_CURVE448_KEY_SIZE 56
Janos Follath171a7ef2019-02-15 16:17:45 +00003221/*
3222 * Read a private key.
3223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003224int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3225 const unsigned char *buf, size_t buflen)
Janos Follath171a7ef2019-02-15 16:17:45 +00003226{
3227 int ret = 0;
3228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3230 return ret;
3231 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003232
Janos Follath28eb06d2019-02-26 10:53:34 +00003233 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3234
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003235#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Janos Follath28eb06d2019-02-26 10:53:34 +00003237 /*
Archana1d2e2bb2021-06-07 06:13:16 +05303238 * Mask the key as mandated by RFC7748 for Curve25519 and Curve448.
Janos Follath28eb06d2019-02-26 10:53:34 +00003239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
3241 if (buflen != ECP_CURVE25519_KEY_SIZE) {
3242 return MBEDTLS_ERR_ECP_INVALID_KEY;
3243 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
Janos Follath171a7ef2019-02-15 16:17:45 +00003246
Janos Follath28eb06d2019-02-26 10:53:34 +00003247 /* Set the three least significant bits to 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
3249 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
3250 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0));
Janos Follath171a7ef2019-02-15 16:17:45 +00003251
Janos Follath28eb06d2019-02-26 10:53:34 +00003252 /* Set the most significant bit to 0 */
3253 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 mbedtls_mpi_set_bit(&key->d,
3255 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0)
3256 );
Janos Follath171a7ef2019-02-15 16:17:45 +00003257
Janos Follath28eb06d2019-02-26 10:53:34 +00003258 /* Set the second most significant bit to 1 */
3259 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 mbedtls_mpi_set_bit(&key->d,
3261 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1)
3262 );
3263 } else if (grp_id == MBEDTLS_ECP_DP_CURVE448) {
3264 if (buflen != ECP_CURVE448_KEY_SIZE) {
3265 return MBEDTLS_ERR_ECP_INVALID_KEY;
3266 }
Archana1d2e2bb2021-06-07 06:13:16 +05303267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
Archana1d2e2bb2021-06-07 06:13:16 +05303269
3270 /* Set the two least significant bits to 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
3272 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
Archana1d2e2bb2021-06-07 06:13:16 +05303273
3274 /* Set the most significant bit to 1 */
3275 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 mbedtls_mpi_set_bit(&key->d,
3277 ECP_CURVE448_KEY_SIZE * 8 - 1, 1)
3278 );
Archana1d2e2bb2021-06-07 06:13:16 +05303279 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003280 }
3281
3282#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003283#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3285 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen));
Janos Follath171a7ef2019-02-15 16:17:45 +00003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d));
Janos Follath171a7ef2019-02-15 16:17:45 +00003288 }
3289
3290#endif
3291cleanup:
3292
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 if (ret != 0) {
3294 mbedtls_mpi_free(&key->d);
3295 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 return ret;
Janos Follath171a7ef2019-02-15 16:17:45 +00003298}
3299
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003300/*
Steven Cooremande8593f2020-06-09 19:55:26 +02003301 * Write a private key.
3302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003303int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
3304 unsigned char *buf, size_t buflen)
Steven Cooremande8593f2020-06-09 19:55:26 +02003305{
Steven Cooreman0024df62020-07-13 10:59:40 +02003306 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Steven Cooremande8593f2020-06-09 19:55:26 +02003307
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003308#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3310 if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) {
3311 if (buflen < ECP_CURVE25519_KEY_SIZE) {
3312 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3313 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 } else if (key->grp.id == MBEDTLS_ECP_DP_CURVE448) {
3316 if (buflen < ECP_CURVE448_KEY_SIZE) {
3317 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3318 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003319 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen));
Steven Cooremande8593f2020-06-09 19:55:26 +02003321 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003322#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003323#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3325 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen));
Steven Cooremande8593f2020-06-09 19:55:26 +02003326 }
3327
3328#endif
3329cleanup:
3330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 return ret;
Steven Cooremande8593f2020-06-09 19:55:26 +02003332}
3333
Valerio Settifd122f42023-04-05 18:15:32 +02003334#if defined(MBEDTLS_ECP_C)
Steven Cooremande8593f2020-06-09 19:55:26 +02003335/*
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003336 * Check a public-private key pair
3337 */
Manuel Pégourié-Gonnardf8c24bf2021-06-15 11:29:26 +02003338int mbedtls_ecp_check_pub_priv(
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
3340 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003341{
Janos Follath24eed8d2019-11-22 13:21:35 +00003342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 mbedtls_ecp_point Q;
3344 mbedtls_ecp_group grp;
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003346 pub->grp.id != prv->grp.id ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
3348 mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
3349 mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
3350 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003351 }
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 mbedtls_ecp_point_init(&Q);
3354 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 /* mbedtls_ecp_mul() needs a non-const group... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 mbedtls_ecp_group_copy(&grp, &prv->grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003358
3359 /* Also checks d is valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, f_rng, p_rng));
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003361
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
3363 mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
3364 mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003366 goto cleanup;
3367 }
3368
3369cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 mbedtls_ecp_point_free(&Q);
3371 mbedtls_ecp_group_free(&grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 return ret;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003374}
Valerio Settifd122f42023-04-05 18:15:32 +02003375#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003376
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003377/*
3378 * Export generic key-pair parameters.
3379 */
3380int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
3381 mbedtls_mpi *d, mbedtls_ecp_point *Q)
3382{
3383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 if ((ret = mbedtls_ecp_group_copy(grp, &key->grp)) != 0) {
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003386 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 }
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if ((ret = mbedtls_mpi_copy(d, &key->d)) != 0) {
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003390 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 }
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if ((ret = mbedtls_ecp_copy(Q, &key->Q)) != 0) {
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003394 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 }
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003396
3397 return 0;
3398}
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003401
Valerio Settifd122f42023-04-05 18:15:32 +02003402#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003403/*
3404 * PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!!
3405 *
3406 * This is the linear congruential generator from numerical recipes,
3407 * except we only use the low byte as the output. See
3408 * https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
3409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410static int self_test_rng(void *ctx, unsigned char *out, size_t len)
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003411{
3412 static uint32_t state = 42;
3413
3414 (void) ctx;
3415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 for (size_t i = 0; i < len; i++) {
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003417 state = state * 1664525u + 1013904223u;
3418 out[i] = (unsigned char) state;
3419 }
3420
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 return 0;
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003422}
3423
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003424/* Adjust the exponent to be a valid private point for the specified curve.
3425 * This is sometimes necessary because we use a single set of exponents
3426 * for all curves but the validity of values depends on the curve. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003427static int self_test_adjust_exponent(const mbedtls_ecp_group *grp,
3428 mbedtls_mpi *m)
Gilles Peskinea088c812018-09-17 18:31:15 +02003429{
3430 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 switch (grp->id) {
3432 /* If Curve25519 is available, then that's what we use for the
3433 * Montgomery test, so we don't need the adjustment code. */
3434#if !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Gilles Peskinea088c812018-09-17 18:31:15 +02003435#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3436 case MBEDTLS_ECP_DP_CURVE448:
3437 /* Move highest bit from 254 to N-1. Setting bit N-1 is
3438 * necessary to enforce the highest-bit-set constraint. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0));
3440 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1));
Gilles Peskinea088c812018-09-17 18:31:15 +02003441 /* Copy second-highest bit from 253 to N-2. This is not
3442 * necessary but improves the test variety a bit. */
3443 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 mbedtls_mpi_set_bit(m, grp->nbits - 1,
3445 mbedtls_mpi_get_bit(m, 253)));
Gilles Peskinea088c812018-09-17 18:31:15 +02003446 break;
3447#endif
3448#endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */
3449 default:
3450 /* Non-Montgomery curves and Curve25519 need no adjustment. */
3451 (void) grp;
3452 (void) m;
3453 goto cleanup;
3454 }
3455cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 return ret;
Gilles Peskinea088c812018-09-17 18:31:15 +02003457}
3458
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003459/* Calculate R = m.P for each m in exponents. Check that the number of
3460 * basic operations doesn't depend on the value of m. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461static int self_test_point(int verbose,
3462 mbedtls_ecp_group *grp,
3463 mbedtls_ecp_point *R,
3464 mbedtls_mpi *m,
3465 const mbedtls_ecp_point *P,
3466 const char *const *exponents,
3467 size_t n_exponents)
Gilles Peskinec95696f2018-09-17 15:59:01 +02003468{
3469 int ret = 0;
Gilles Peskine24666792018-09-17 18:29:49 +02003470 size_t i = 0;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003471 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
3472 add_count = 0;
3473 dbl_count = 0;
3474 mul_count = 0;
Gilles Peskinea088c812018-09-17 18:31:15 +02003475
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0]));
3477 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3478 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003479
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 for (i = 1; i < n_exponents; i++) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003481 add_c_prev = add_count;
3482 dbl_c_prev = dbl_count;
3483 mul_c_prev = mul_count;
3484 add_count = 0;
3485 dbl_count = 0;
3486 mul_count = 0;
3487
Gilles Peskine449bd832023-01-11 14:50:10 +01003488 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i]));
3489 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3490 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003491
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 if (add_count != add_c_prev ||
Gilles Peskinec95696f2018-09-17 15:59:01 +02003493 dbl_count != dbl_c_prev ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 mul_count != mul_c_prev) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003495 ret = 1;
3496 break;
3497 }
3498 }
3499
3500cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 if (verbose != 0) {
3502 if (ret != 0) {
3503 mbedtls_printf("failed (%u)\n", (unsigned int) i);
3504 } else {
3505 mbedtls_printf("passed\n");
3506 }
Gilles Peskinec95696f2018-09-17 15:59:01 +02003507 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 return ret;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003509}
Valerio Settifd122f42023-04-05 18:15:32 +02003510#endif /* MBEDTLS_ECP_C */
Gilles Peskinec95696f2018-09-17 15:59:01 +02003511
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003512/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003513 * Checkup routine
3514 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003515int mbedtls_ecp_self_test(int verbose)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003516{
Valerio Settifd122f42023-04-05 18:15:32 +02003517#if defined(MBEDTLS_ECP_C)
Janos Follath24eed8d2019-11-22 13:21:35 +00003518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 mbedtls_ecp_group grp;
3520 mbedtls_ecp_point R, P;
3521 mbedtls_mpi m;
Gilles Peskine24666792018-09-17 18:29:49 +02003522
3523#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskined9767a52018-09-14 19:29:47 +02003524 /* Exponents especially adapted for secp192k1, which has the lowest
3525 * order n of all supported curves (secp192r1 is in a slightly larger
3526 * field but the order of its base point is slightly smaller). */
Gilles Peskine24666792018-09-17 18:29:49 +02003527 const char *sw_exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003528 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003529 "000000000000000000000000000000000000000000000001", /* one */
Gilles Peskined9767a52018-09-14 19:29:47 +02003530 "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003531 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003532 "400000000000000000000000000000000000000000000000", /* one and zeros */
3533 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3534 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003535 };
Gilles Peskine24666792018-09-17 18:29:49 +02003536#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3537#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3538 const char *m_exponents[] =
3539 {
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003540 /* Valid private values for Curve25519. In a build with Curve448
3541 * but not Curve25519, they will be adjusted in
3542 * self_test_adjust_exponent(). */
Gilles Peskine24666792018-09-17 18:29:49 +02003543 "4000000000000000000000000000000000000000000000000000000000000000",
3544 "5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
3545 "5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
3546 "41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
3547 "5555555555555555555555555555555555555555555555555555555555555550",
3548 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
3549 };
3550#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 mbedtls_ecp_group_init(&grp);
3553 mbedtls_ecp_point_init(&R);
3554 mbedtls_ecp_point_init(&P);
3555 mbedtls_mpi_init(&m);
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003556
Gilles Peskine24666792018-09-17 18:29:49 +02003557#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003558 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP192R1));
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003561#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id));
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003563#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 if (verbose != 0) {
3566 mbedtls_printf(" ECP SW test #1 (constant op_count, base point G): ");
3567 }
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003568 /* Do a dummy multiplication first to trigger precomputation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2));
3570 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, self_test_rng, NULL));
3571 ret = self_test_point(verbose,
3572 &grp, &R, &m, &grp.G,
3573 sw_exponents,
3574 sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3575 if (ret != 0) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003576 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003578
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 if (verbose != 0) {
3580 mbedtls_printf(" ECP SW test #2 (constant op_count, other point): ");
3581 }
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003582 /* We computed P = 2G last time, use it */
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 ret = self_test_point(verbose,
3584 &grp, &R, &m, &P,
3585 sw_exponents,
3586 sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3587 if (ret != 0) {
Gilles Peskine24666792018-09-17 18:29:49 +02003588 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 }
Gilles Peskine24666792018-09-17 18:29:49 +02003590
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 mbedtls_ecp_group_free(&grp);
3592 mbedtls_ecp_point_free(&R);
Gilles Peskine24666792018-09-17 18:29:49 +02003593#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3594
3595#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 if (verbose != 0) {
3597 mbedtls_printf(" ECP Montgomery test (constant op_count): ");
3598 }
Gilles Peskine24666792018-09-17 18:29:49 +02003599#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE25519));
Gilles Peskine24666792018-09-17 18:29:49 +02003601#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE448));
Gilles Peskine24666792018-09-17 18:29:49 +02003603#else
3604#error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
3605#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 ret = self_test_point(verbose,
3607 &grp, &R, &m, &grp.G,
3608 m_exponents,
3609 sizeof(m_exponents) / sizeof(m_exponents[0]));
3610 if (ret != 0) {
Gilles Peskine24666792018-09-17 18:29:49 +02003611 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 }
Gilles Peskine24666792018-09-17 18:29:49 +02003613#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003614
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003615cleanup:
3616
Gilles Peskine449bd832023-01-11 14:50:10 +01003617 if (ret < 0 && verbose != 0) {
3618 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
3619 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003620
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 mbedtls_ecp_group_free(&grp);
3622 mbedtls_ecp_point_free(&R);
3623 mbedtls_ecp_point_free(&P);
3624 mbedtls_mpi_free(&m);
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003625
Gilles Peskine449bd832023-01-11 14:50:10 +01003626 if (verbose != 0) {
3627 mbedtls_printf("\n");
3628 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003629
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 return ret;
Valerio Settifd122f42023-04-05 18:15:32 +02003631#else /* MBEDTLS_ECP_C */
3632 (void) verbose;
3633 return 0;
3634#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003635}
3636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003638
Janos Follathb0697532016-08-18 12:38:46 +01003639#endif /* !MBEDTLS_ECP_ALT */
3640
Valerio Settifd122f42023-04-05 18:15:32 +02003641#endif /* MBEDTLS_ECP_LIGHT */