blob: 1e6b69b9827099334038e490925820c7ba24ce93 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01006 */
7
8/*
9 * References:
10 *
Xiaokang Qian669c7c32023-04-10 07:36:04 +000011 * SEC1 https://www.secg.org/sec1-v2.pdf
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010012 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010013 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010014 * RFC 4492 for the related TLS structures and constants
Xiaokang Qian50fe3632023-04-12 06:08:45 +000015 * - https://www.rfc-editor.org/rfc/rfc4492
Nicholas Wilson08f3ef12015-11-10 13:10:01 +000016 * RFC 7748 for the Curve448 and Curve25519 curve definitions
Xiaokang Qian50fe3632023-04-12 06:08:45 +000017 * - https://www.rfc-editor.org/rfc/rfc7748
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020018 *
Xiaokang Qian50fe3632023-04-12 06:08:45 +000019 * [Curve25519] https://cr.yp.to/ecdh/curve25519-20060209.pdf
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010020 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020021 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020022 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
23 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
24 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010025 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020026 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010027 * render ECC resistant against Side Channel Attacks. IACR Cryptology
28 * ePrint Archive, 2004, vol. 2004, p. 342.
29 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010033
Valerio Settifd122f42023-04-05 18:15:32 +020034#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Janos Follath430d3372016-11-03 14:25:37 +000037#include "mbedtls/threading.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050038#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000039#include "mbedtls/error.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020040
Janos Follath8c70e812021-06-24 14:48:38 +010041#include "bn_mul.h"
Gabor Mezei2a7bcaf2023-07-06 10:37:51 +020042#include "ecp_invasive.h"
Gilles Peskine80ba8502021-04-03 20:36:37 +020043
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <string.h>
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010049/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010050 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020051 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010052 */
Valerio Settifd122f42023-04-05 18:15:32 +020053#if defined(MBEDTLS_ECP_C)
54static unsigned long add_count, dbl_count;
55#endif /* MBEDTLS_ECP_C */
56static unsigned long mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010057#endif
58
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +020059#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +010060/*
61 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +020062 *
63 * Default value 0 means that ECC operations will not yield.
64 * Note that regardless of the value of ecp_max_ops, always at
65 * least one step is performed before yielding.
66 *
67 * Setting ecp_max_ops=1 can be suitable for testing purposes
68 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +010069 */
70static unsigned ecp_max_ops = 0;
71
72/*
73 * Set ecp_max_ops
74 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075void mbedtls_ecp_set_max_ops(unsigned max_ops)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +010076{
77 ecp_max_ops = max_ops;
78}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +010079
80/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020081 * Check if restart is enabled
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083int mbedtls_ecp_restart_is_enabled(void)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020084{
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return ecp_max_ops != 0;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020086}
87
88/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +020089 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +010090 */
Gilles Peskine449bd832023-01-11 14:50:10 +010091struct mbedtls_ecp_restart_mul {
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +010092 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +010093 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +010094 mbedtls_ecp_point *T; /* table for precomputed points */
95 unsigned char T_size; /* number of points in table T */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +020096 enum { /* what were we doing last time we returned? */
97 ecp_rsm_init = 0, /* nothing so far, dummy initial state */
98 ecp_rsm_pre_dbl, /* precompute 2^n multiples */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +010099 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
100 ecp_rsm_pre_add, /* precompute remaining points by adding */
101 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +0200102 ecp_rsm_comb_core, /* ecp_mul_comb_core() */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100103 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100104 } state;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100105};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100106
107/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200108 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110static void ecp_restart_rsm_init(mbedtls_ecp_restart_mul_ctx *ctx)
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100111{
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_ecp_point_init(&ctx->R);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200113 ctx->i = 0;
114 ctx->T = NULL;
115 ctx->T_size = 0;
116 ctx->state = ecp_rsm_init;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100117}
118
119/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200120 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx)
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100123{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100124 unsigned char i;
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (ctx == NULL) {
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100127 return;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100128 }
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_ecp_point_free(&ctx->R);
131
132 if (ctx->T != NULL) {
133 for (i = 0; i < ctx->T_size; i++) {
134 mbedtls_ecp_point_free(ctx->T + i);
135 }
136 mbedtls_free(ctx->T);
137 }
138
139 ecp_restart_rsm_init(ctx);
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100140}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100141
142/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200143 * Restart context for ecp_muladd()
144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145struct mbedtls_ecp_restart_muladd {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200146 mbedtls_ecp_point mP; /* mP value */
147 mbedtls_ecp_point R; /* R intermediate result */
148 enum { /* what should we do next? */
149 ecp_rsma_mul1 = 0, /* first multiplication */
150 ecp_rsma_mul2, /* second multiplication */
151 ecp_rsma_add, /* addition */
152 ecp_rsma_norm, /* normalization */
153 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200154};
155
156/*
157 * Init restart_muladd sub-context
158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159static void ecp_restart_ma_init(mbedtls_ecp_restart_muladd_ctx *ctx)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200160{
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ecp_point_init(&ctx->mP);
162 mbedtls_ecp_point_init(&ctx->R);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200163 ctx->state = ecp_rsma_mul1;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200164}
165
166/*
167 * Free the components of a restart_muladd sub-context
168 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200170{
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ctx == NULL) {
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200172 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 }
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 mbedtls_ecp_point_free(&ctx->mP);
176 mbedtls_ecp_point_free(&ctx->R);
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 ecp_restart_ma_init(ctx);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200179}
180
181/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200182 * Initialize a restart context
183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200185{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200186 ctx->ops_done = 0;
187 ctx->depth = 0;
188 ctx->rsm = NULL;
189 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200190}
191
192/*
193 * Free the components of a restart context
194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200196{
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (ctx == NULL) {
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200198 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 }
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 ecp_restart_rsm_free(ctx->rsm);
202 mbedtls_free(ctx->rsm);
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 ecp_restart_ma_free(ctx->ma);
205 mbedtls_free(ctx->ma);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_ecp_restart_init(ctx);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200208}
209
210/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100211 * Check if we can do the next step
212 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
214 mbedtls_ecp_restart_ctx *rs_ctx,
215 unsigned ops)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100216{
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (rs_ctx != NULL && ecp_max_ops != 0) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100218 /* scale depending on curve size: the chosen reference is 256-bit,
219 * and multiplication is quadratic. Round to the closest integer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (grp->pbits >= 512) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100221 ops *= 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 } else if (grp->pbits >= 384) {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100223 ops *= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 }
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100225
Hanno Beckerb10c6602018-10-26 13:50:13 +0100226 /* Avoid infinite loops: always allow first step.
227 * Because of that, however, it's not generally true
228 * that ops_done <= ecp_max_ops, so the check
229 * ops_done > ecp_max_ops below is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if ((rs_ctx->ops_done != 0) &&
231 (rs_ctx->ops_done > ecp_max_ops ||
232 ops > ecp_max_ops - rs_ctx->ops_done)) {
233 return MBEDTLS_ERR_ECP_IN_PROGRESS;
Hanno Beckerb10c6602018-10-26 13:50:13 +0100234 }
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100235
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100236 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200237 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100238 }
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return 0;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100241}
242
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200243/* Call this when entering a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244#define ECP_RS_ENTER(SUB) do { \
245 /* reset ops count for this call if top-level */ \
246 if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200247 rs_ctx->ops_done = 0; \
248 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 /* set up our own sub-context if needed */ \
250 if (mbedtls_ecp_restart_is_enabled() && \
251 rs_ctx != NULL && rs_ctx->SUB == NULL) \
252 { \
253 rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
254 if (rs_ctx->SUB == NULL) \
255 return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
256 \
257 ecp_restart_## SUB ##_init(rs_ctx->SUB); \
258 } \
259} while (0)
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200260
261/* Call this when leaving a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262#define ECP_RS_LEAVE(SUB) do { \
263 /* clear our sub-context when not in progress (done or error) */ \
264 if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
265 ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
266 { \
267 ecp_restart_## SUB ##_free(rs_ctx->SUB); \
268 mbedtls_free(rs_ctx->SUB); \
269 rs_ctx->SUB = NULL; \
270 } \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200271 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (rs_ctx != NULL) \
Manuel Pégourié-Gonnarda58e0112018-10-16 10:42:47 +0200273 rs_ctx->depth--; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100274} while (0)
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200275
276#else /* MBEDTLS_ECP_RESTARTABLE */
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278#define ECP_RS_ENTER(sub) (void) rs_ctx;
279#define ECP_RS_LEAVE(sub) (void) rs_ctx;
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +0200280
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200281#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100282
Valerio Settifd122f42023-04-05 18:15:32 +0200283#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284static void mpi_init_many(mbedtls_mpi *arr, size_t size)
Hanno Becker466df6e2022-01-10 11:16:51 +0000285{
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 while (size--) {
287 mbedtls_mpi_init(arr++);
288 }
Hanno Becker466df6e2022-01-10 11:16:51 +0000289}
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291static void mpi_free_many(mbedtls_mpi *arr, size_t size)
Hanno Becker466df6e2022-01-10 11:16:51 +0000292{
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 while (size--) {
294 mbedtls_mpi_free(arr++);
295 }
Hanno Becker466df6e2022-01-10 11:16:51 +0000296}
Valerio Settifd122f42023-04-05 18:15:32 +0200297#endif /* MBEDTLS_ECP_C */
Hanno Becker466df6e2022-01-10 11:16:51 +0000298
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100299/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200300 * List of supported curves:
301 * - internal ID
Christoph M. Wintersteigercb310732019-02-15 15:50:38 +0000302 * - 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 +0200303 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200304 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100305 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100306 * Curves are listed in order: largest curves first, and for a given size,
Gilles Peskineae270bf2021-06-02 00:05:29 +0200307 * fastest curves first.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200308 *
Gilles Peskineae270bf2021-06-02 00:05:29 +0200309 * 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 +0200310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
314 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
317 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
320 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
323 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
326 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
329 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
332 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
335 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
338 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100339#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
341 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
344 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100345#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200346#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Christoph M. Wintersteiger86e36c42018-12-06 17:27:31 +0000347 { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100348#endif
Gilles Peskine360e2c42020-07-24 02:03:20 +0200349#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
350 { MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
351#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200353};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355#define ECP_NB_CURVES sizeof(ecp_supported_curves) / \
356 sizeof(ecp_supported_curves[0])
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200359
360/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200361 * List of supported curves and associated info
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200364{
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return ecp_supported_curves;
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200366}
367
368/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100369 * List of supported curves, group ID only
370 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100372{
373 static int init_done = 0;
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (!init_done) {
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100376 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 curve_info++) {
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100382 ecp_supported_grp_id[i++] = curve_info->grp_id;
383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100385
386 init_done = 1;
387 }
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return ecp_supported_grp_id;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100390}
391
392/*
393 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395const 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 +0200396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 curve_info++) {
402 if (curve_info->grp_id == grp_id) {
403 return curve_info;
404 }
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return NULL;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200408}
409
410/*
411 * Get the curve info from the TLS identifier
412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 for (curve_info = mbedtls_ecp_curve_list();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 curve_info++) {
420 if (curve_info->tls_id == tls_id) {
421 return curve_info;
422 }
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200423 }
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return NULL;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200426}
427
428/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100429 * Get the curve info from the name
430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (name == NULL) {
436 return NULL;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100437 }
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 for (curve_info = mbedtls_ecp_curve_list();
440 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
441 curve_info++) {
442 if (strcmp(curve_info->name, name) == 0) {
443 return curve_info;
444 }
445 }
446
447 return NULL;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100448}
449
450/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100451 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100452 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100453mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100454{
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (grp->G.X.p == NULL) {
456 return MBEDTLS_ECP_TYPE_NONE;
457 }
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (grp->G.Y.p == NULL) {
460 return MBEDTLS_ECP_TYPE_MONTGOMERY;
461 } else {
462 return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS;
463 }
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100464}
465
466/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100467 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100470{
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 mbedtls_mpi_init(&pt->X);
472 mbedtls_mpi_init(&pt->Y);
473 mbedtls_mpi_init(&pt->Z);
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100474}
475
476/*
477 * Initialize (the components of) a group
478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100480{
Manuel Pégourié-Gonnard95e2eca2018-06-20 10:29:47 +0200481 grp->id = MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_mpi_init(&grp->P);
483 mbedtls_mpi_init(&grp->A);
484 mbedtls_mpi_init(&grp->B);
485 mbedtls_ecp_point_init(&grp->G);
486 mbedtls_mpi_init(&grp->N);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200487 grp->pbits = 0;
488 grp->nbits = 0;
489 grp->h = 0;
490 grp->modp = NULL;
491 grp->t_pre = NULL;
492 grp->t_post = NULL;
493 grp->t_data = NULL;
494 grp->T = NULL;
495 grp->T_size = 0;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100496}
497
498/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200499 * Initialize (the components of) a key pair
500 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200502{
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 mbedtls_ecp_group_init(&key->grp);
504 mbedtls_mpi_init(&key->d);
505 mbedtls_ecp_point_init(&key->Q);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200506}
507
508/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100509 * Unallocate (the components of) a point
510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100512{
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (pt == NULL) {
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100514 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 }
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_mpi_free(&(pt->X));
518 mbedtls_mpi_free(&(pt->Y));
519 mbedtls_mpi_free(&(pt->Z));
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100520}
521
522/*
kXuanba9cb762021-04-08 14:32:06 +0800523 * Check that the comb table (grp->T) is static initialized.
524 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525static int ecp_group_is_static_comb_table(const mbedtls_ecp_group *grp)
526{
kXuanba9cb762021-04-08 14:32:06 +0800527#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
528 return grp->T != NULL && grp->T_size == 0;
529#else
530 (void) grp;
531 return 0;
532#endif
533}
534
535/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100536 * Unallocate (the components of) a group
537 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100539{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200540 size_t i;
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (grp == NULL) {
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100543 return;
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100544 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (grp->h != 1) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 mbedtls_mpi_free(&grp->A);
548 mbedtls_mpi_free(&grp->B);
549 mbedtls_ecp_point_free(&grp->G);
Janos Follathd8cb3d72023-07-31 12:54:59 +0100550
551#if !defined(MBEDTLS_ECP_WITH_MPI_UINT)
Xiaokang Qianb903f4e2023-07-20 05:51:53 +0000552 mbedtls_mpi_free(&grp->N);
Janos Follathd8cb3d72023-07-31 12:54:59 +0100553 mbedtls_mpi_free(&grp->P);
554#endif
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200555 }
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) {
558 for (i = 0; i < grp->T_size; i++) {
559 mbedtls_ecp_point_free(&grp->T[i]);
560 }
561 mbedtls_free(grp->T);
562 }
563
564 mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group));
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100565}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100566
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100567/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200568 * Unallocate (the components of) a key pair
569 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200571{
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (key == NULL) {
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200573 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 }
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 mbedtls_ecp_group_free(&key->grp);
577 mbedtls_mpi_free(&key->d);
578 mbedtls_ecp_point_free(&key->Q);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200579}
580
581/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200582 * Copy the contents of a point
583 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200585{
Janos Follath24eed8d2019-11-22 13:21:35 +0000586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
588 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y));
589 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z));
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200590
591cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return ret;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200593}
594
595/*
596 * Copy the contents of a group object
597 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200599{
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return mbedtls_ecp_group_load(dst, src->id);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200601}
602
603/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100604 * Set point to zero
605 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100607{
Janos Follath24eed8d2019-11-22 13:21:35 +0000608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
610 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1));
611 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0));
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100612
613cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return ret;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100615}
616
617/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100618 * Tell if a point is zero
619 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100621{
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100623}
624
625/*
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500626 * Compare two points lazily
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200627 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
629 const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200630{
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
632 mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
633 mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
634 return 0;
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200635 }
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200638}
639
640/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100641 * Import a non-zero point from ASCII strings
642 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100643int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
644 const char *x, const char *y)
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100645{
Janos Follath24eed8d2019-11-22 13:21:35 +0000646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
648 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y));
649 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100650
651cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 return ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100653}
654
655/*
Janos Follath7caf8e42019-02-20 12:00:22 +0000656 * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100657 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
659 const mbedtls_ecp_point *P,
660 int format, size_t *olen,
661 unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100662{
Janos Follath28eb06d2019-02-26 10:53:34 +0000663 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100664 size_t plen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
666 format != MBEDTLS_ECP_PF_COMPRESSED) {
667 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
668 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 plen = mbedtls_mpi_size(&grp->P);
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100671
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200672#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine59970052019-02-28 13:12:06 +0100673 (void) format; /* Montgomery curves always use the same point format */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000675 *olen = plen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (buflen < *olen) {
677 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
678 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen));
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100681 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000682#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200683#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000685 /*
686 * Common case: P == 0
687 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
689 if (buflen < 1) {
690 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
691 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100692
Janos Follath7caf8e42019-02-20 12:00:22 +0000693 buf[0] = 0x00;
694 *olen = 1;
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return 0;
Janos Follath7caf8e42019-02-20 12:00:22 +0000697 }
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000700 *olen = 2 * plen + 1;
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (buflen < *olen) {
703 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
704 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000705
706 buf[0] = 0x04;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
708 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen));
709 } else if (format == MBEDTLS_ECP_PF_COMPRESSED) {
Janos Follath7caf8e42019-02-20 12:00:22 +0000710 *olen = plen + 1;
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (buflen < *olen) {
713 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
714 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0);
717 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
Janos Follath7caf8e42019-02-20 12:00:22 +0000718 }
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100719 }
Janos Follath7caf8e42019-02-20 12:00:22 +0000720#endif
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100721
722cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 return ret;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100724}
725
Glenn Strauss2ff77112022-09-14 23:27:50 -0400726#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100727static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
728 const mbedtls_mpi *X,
729 mbedtls_mpi *Y,
730 int parity_bit);
Glenn Strauss2ff77112022-09-14 23:27:50 -0400731#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
732
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100733/*
Janos Follath59b813c2019-02-13 10:44:06 +0000734 * Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100735 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
737 mbedtls_ecp_point *pt,
738 const unsigned char *buf, size_t ilen)
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100739{
Janos Follath28eb06d2019-02-26 10:53:34 +0000740 int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100741 size_t plen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (ilen < 1) {
743 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
744 }
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 plen = mbedtls_mpi_size(&grp->P);
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100747
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200748#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
750 if (plen != ilen) {
751 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
752 }
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen));
755 mbedtls_mpi_free(&pt->Y);
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (grp->id == MBEDTLS_ECP_DP_CURVE25519) {
Janos Follathffbd7e82019-02-25 11:35:20 +0000758 /* Set most significant bit to 0 as prescribed in RFC7748 §5 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0));
760 }
Janos Follath28eb06d2019-02-26 10:53:34 +0000761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
Janos Follath59b813c2019-02-13 10:44:06 +0000763 }
764#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200765#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
767 if (buf[0] == 0x00) {
768 if (ilen == 1) {
769 return mbedtls_ecp_set_zero(pt);
770 } else {
771 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
772 }
Janos Follath59b813c2019-02-13 10:44:06 +0000773 }
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (ilen < 1 + plen) {
776 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
777 }
Janos Follath59b813c2019-02-13 10:44:06 +0000778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen));
780 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
Glenn Strauss2ff77112022-09-14 23:27:50 -0400781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (buf[0] == 0x04) {
Glenn Strauss2ff77112022-09-14 23:27:50 -0400783 /* format == MBEDTLS_ECP_PF_UNCOMPRESSED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (ilen != 1 + plen * 2) {
785 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
786 }
787 return mbedtls_mpi_read_binary(&pt->Y, buf + 1 + plen, plen);
788 } else if (buf[0] == 0x02 || buf[0] == 0x03) {
Glenn Strauss2ff77112022-09-14 23:27:50 -0400789 /* format == MBEDTLS_ECP_PF_COMPRESSED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (ilen != 1 + plen) {
791 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
792 }
793 return mbedtls_ecp_sw_derive_y(grp, &pt->X, &pt->Y,
794 (buf[0] & 1));
795 } else {
796 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Glenn Strauss2ff77112022-09-14 23:27:50 -0400797 }
Janos Follath59b813c2019-02-13 10:44:06 +0000798 }
799#endif
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100800
801cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return ret;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100803}
804
805/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100806 * Import a point from a TLS ECPoint record (RFC 4492)
807 * struct {
808 * opaque point <1..2^8-1>;
809 * } ECPoint;
810 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
812 mbedtls_ecp_point *pt,
813 const unsigned char **buf, size_t buf_len)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100814{
815 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100816 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100817 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200818 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (buf_len < 2) {
821 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
822 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100823
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100824 data_len = *(*buf)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (data_len < 1 || data_len > buf_len - 1) {
826 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
827 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100828
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100829 /*
830 * Save buffer start for read_binary and update buf
831 */
832 buf_start = *buf;
833 *buf += data_len;
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len);
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100836}
837
838/*
839 * Export a point as a TLS ECPoint record (RFC 4492)
840 * struct {
841 * opaque point <1..2^8-1>;
842 * } ECPoint;
843 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
845 int format, size_t *olen,
846 unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100847{
Janos Follath24eed8d2019-11-22 13:21:35 +0000848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
850 format != MBEDTLS_ECP_PF_COMPRESSED) {
851 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
852 }
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100853
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100854 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100855 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100856 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (blen < 1) {
858 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
859 }
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format,
862 olen, buf + 1, blen - 1)) != 0) {
863 return ret;
864 }
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100865
866 /*
867 * write length to the first byte and update total length
868 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200869 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100870 ++*olen;
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100873}
874
875/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100876 * Set a group from an ECParameters record (RFC 4492)
877 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
879 const unsigned char **buf, size_t len)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500880{
Janos Follath24eed8d2019-11-22 13:21:35 +0000881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500882 mbedtls_ecp_group_id grp_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) {
884 return ret;
885 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 return mbedtls_ecp_group_load(grp, grp_id);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500888}
889
890/*
891 * Read a group id from an ECParameters record (RFC 4492) and convert it to
892 * mbedtls_ecp_group_id.
893 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
895 const unsigned char **buf, size_t len)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100896{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200897 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100899 /*
900 * We expect at least three bytes (see below)
901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (len < 3) {
903 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
904 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100905
906 /*
907 * First byte is curve_type; only named_curve is handled
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
910 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
911 }
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100912
913 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100914 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100915 */
Dave Rodgman6a9fb932023-08-16 17:50:36 +0100916 tls_id = MBEDTLS_GET_UINT16_BE(*buf, 0);
917 *buf += 2;
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) {
920 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
921 }
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200922
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500923 *grp = curve_info->grp_id;
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return 0;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100926}
927
928/*
929 * Write the ECParameters record corresponding to a group (RFC 4492)
930 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
932 unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100933{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 const mbedtls_ecp_curve_info *curve_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) {
936 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
937 }
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200938
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100939 /*
940 * We are going to write 3 bytes (see below)
941 */
942 *olen = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (blen < *olen) {
944 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
945 }
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100946
947 /*
948 * First byte is curve_type, always named_curve
949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100951
952 /*
953 * Next two bytes are the namedcurve value
954 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0);
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100958}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100959
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200960/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
962 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200963 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 * 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 +0200965 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200967{
Janos Follath24eed8d2019-11-22 13:21:35 +0000968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (grp->modp == NULL) {
971 return mbedtls_mpi_mod_mpi(N, N, &grp->P);
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200972 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 /* N->s < 0 is a much faster test, which fails only if N is 0 */
975 if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
976 mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
977 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
978 }
979
980 MBEDTLS_MPI_CHK(grp->modp(N));
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200981
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200982 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) {
984 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P));
985 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200988 /* we known P, N and the result are positive */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P));
990 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200991
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200992cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return ret;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200994}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200995
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100996/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100997 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100998 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100999 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 * 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 +01001001 * bring the result back to this range.
1002 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001003 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +01001004 */
1005
1006/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 * 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 +01001008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001010#define INC_MUL_COUNT mul_count++;
1011#else
1012#define INC_MUL_COUNT
1013#endif
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015#define MOD_MUL(N) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001016 do \
1017 { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 MBEDTLS_MPI_CHK(ecp_modp(&(N), grp)); \
Hanno Becker1eeca412018-10-15 12:01:35 +01001019 INC_MUL_COUNT \
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 } while (0)
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022static inline int mbedtls_mpi_mul_mod(const mbedtls_ecp_group *grp,
1023 mbedtls_mpi *X,
1024 const mbedtls_mpi *A,
1025 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001026{
Janos Follath24eed8d2019-11-22 13:21:35 +00001027 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(X, A, B));
1029 MOD_MUL(*X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001030cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001032}
1033
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001034/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +02001036 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038#define MOD_SUB(N) \
Hanno Beckerce29ae82022-01-04 04:55:11 +00001039 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 while ((N)->s < 0 && mbedtls_mpi_cmp_int((N), 0) != 0) \
1041 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi((N), (N), &grp->P)); \
1042 } while (0)
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001043
Dave Rodgman815b2402023-12-14 20:51:21 +00001044MBEDTLS_MAYBE_UNUSED
Gilles Peskine449bd832023-01-11 14:50:10 +01001045static inline int mbedtls_mpi_sub_mod(const mbedtls_ecp_group *grp,
1046 mbedtls_mpi *X,
1047 const mbedtls_mpi *A,
1048 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001049{
Janos Follath24eed8d2019-11-22 13:21:35 +00001050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, A, B));
1052 MOD_SUB(X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001053cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001055}
1056
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001057/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 * 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 +02001059 * We known P, N and the result are positive, so sub_abs is correct, and
1060 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062#define MOD_ADD(N) \
1063 while (mbedtls_mpi_cmp_mpi((N), &grp->P) >= 0) \
1064 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs((N), (N), &grp->P))
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp,
1067 mbedtls_mpi *X,
1068 const mbedtls_mpi *A,
1069 const mbedtls_mpi *B)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001070{
Janos Follath24eed8d2019-11-22 13:21:35 +00001071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B));
1073 MOD_ADD(X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001074cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001076}
1077
Dave Rodgman815b2402023-12-14 20:51:21 +00001078MBEDTLS_MAYBE_UNUSED
Gilles Peskine449bd832023-01-11 14:50:10 +01001079static inline int mbedtls_mpi_mul_int_mod(const mbedtls_ecp_group *grp,
1080 mbedtls_mpi *X,
1081 const mbedtls_mpi *A,
1082 mbedtls_mpi_uint c)
Hanno Becker02b35bd2022-01-01 06:54:25 +00001083{
1084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(X, A, c));
1087 MOD_ADD(X);
Hanno Becker02b35bd2022-01-01 06:54:25 +00001088cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return ret;
Hanno Becker02b35bd2022-01-01 06:54:25 +00001090}
1091
Dave Rodgman815b2402023-12-14 20:51:21 +00001092MBEDTLS_MAYBE_UNUSED
Gilles Peskine449bd832023-01-11 14:50:10 +01001093static inline int mbedtls_mpi_sub_int_mod(const mbedtls_ecp_group *grp,
1094 mbedtls_mpi *X,
1095 const mbedtls_mpi *A,
1096 mbedtls_mpi_uint c)
Hanno Beckerce29ae82022-01-04 04:55:11 +00001097{
1098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(X, A, c));
1101 MOD_SUB(X);
Hanno Beckerce29ae82022-01-04 04:55:11 +00001102cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 return ret;
Hanno Beckerce29ae82022-01-04 04:55:11 +00001104}
1105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106#define MPI_ECP_SUB_INT(X, A, c) \
1107 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int_mod(grp, X, A, c))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001108
Dave Rodgman815b2402023-12-14 20:51:21 +00001109MBEDTLS_MAYBE_UNUSED
Gilles Peskine449bd832023-01-11 14:50:10 +01001110static inline int mbedtls_mpi_shift_l_mod(const mbedtls_ecp_group *grp,
1111 mbedtls_mpi *X,
1112 size_t count)
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001113{
Janos Follath24eed8d2019-11-22 13:21:35 +00001114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, count));
1116 MOD_ADD(X);
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001117cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return ret;
Gilles Peskine3b3b34f2019-07-18 21:08:27 +02001119}
1120
Hanno Becker595616e2022-01-05 08:28:24 +00001121/*
1122 * Macro wrappers around ECP modular arithmetic
1123 *
1124 * Currently, these wrappers are defined via the bignum module.
1125 */
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127#define MPI_ECP_ADD(X, A, B) \
1128 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, X, A, B))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130#define MPI_ECP_SUB(X, A, B) \
1131 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, X, A, B))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133#define MPI_ECP_MUL(X, A, B) \
1134 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, B))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136#define MPI_ECP_SQR(X, A) \
1137 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, A))
Hanno Becker885ed402022-01-04 06:43:50 +00001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139#define MPI_ECP_MUL_INT(X, A, c) \
1140 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int_mod(grp, X, A, c))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142#define MPI_ECP_INV(dst, src) \
1143 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod((dst), (src), &grp->P))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145#define MPI_ECP_MOV(X, A) \
1146 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148#define MPI_ECP_SHIFT_L(X, count) \
1149 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, X, count))
Hanno Beckerce29ae82022-01-04 04:55:11 +00001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151#define MPI_ECP_LSET(X, c) \
1152 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, c))
Hanno Becker595616e2022-01-05 08:28:24 +00001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154#define MPI_ECP_CMP_INT(X, c) \
1155 mbedtls_mpi_cmp_int(X, c)
Hanno Becker595616e2022-01-05 08:28:24 +00001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157#define MPI_ECP_CMP(X, Y) \
1158 mbedtls_mpi_cmp_mpi(X, Y)
Hanno Becker595616e2022-01-05 08:28:24 +00001159
1160/* Needs f_rng, p_rng to be defined. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161#define MPI_ECP_RAND(X) \
1162 MBEDTLS_MPI_CHK(mbedtls_mpi_random((X), 2, &grp->P, f_rng, p_rng))
Hanno Becker595616e2022-01-05 08:28:24 +00001163
1164/* Conditional negation
1165 * Needs grp and a temporary MPI tmp to be defined. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166#define MPI_ECP_COND_NEG(X, cond) \
Hanno Becker595616e2022-01-05 08:28:24 +00001167 do \
1168 { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 unsigned char nonzero = mbedtls_mpi_cmp_int((X), 0) != 0; \
1170 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&tmp, &grp->P, (X))); \
1171 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), &tmp, \
1172 nonzero & cond)); \
1173 } while (0)
Hanno Becker595616e2022-01-05 08:28:24 +00001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175#define MPI_ECP_NEG(X) MPI_ECP_COND_NEG((X), 1)
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177#define MPI_ECP_VALID(X) \
1178 ((X)->p != NULL)
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180#define MPI_ECP_COND_ASSIGN(X, Y, cond) \
1181 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), (Y), (cond)))
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183#define MPI_ECP_COND_SWAP(X, Y, cond) \
1184 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap((X), (Y), (cond)))
Hanno Beckerc27a0e02022-01-06 05:56:34 +00001185
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02001186#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Glenn Straussefde9d52022-12-20 04:20:12 -05001187
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001188/*
1189 * Computes the right-hand side of the Short Weierstrass equation
1190 * RHS = X^3 + A X + B
1191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192static int ecp_sw_rhs(const mbedtls_ecp_group *grp,
1193 mbedtls_mpi *rhs,
1194 const mbedtls_mpi *X)
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001195{
1196 int ret;
1197
1198 /* Compute X^3 + A X + B as X (X^2 + A) + B */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MPI_ECP_SQR(rhs, X);
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001200
1201 /* Special case for A = -3 */
Chien Wong153ae462023-01-31 23:27:03 +08001202 if (mbedtls_ecp_group_a_is_minus_3(grp)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 MPI_ECP_SUB_INT(rhs, rhs, 3);
1204 } else {
1205 MPI_ECP_ADD(rhs, rhs, &grp->A);
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001206 }
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 MPI_ECP_MUL(rhs, rhs, X);
1209 MPI_ECP_ADD(rhs, rhs, &grp->B);
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001210
1211cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 return ret;
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001213}
1214
1215/*
1216 * Derive Y from X and a parity bit
1217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
1219 const mbedtls_mpi *X,
1220 mbedtls_mpi *Y,
1221 int parity_bit)
Glenn Strauss45241612022-12-19 19:37:07 -05001222{
1223 /* w = y^2 = x^3 + ax + b
1224 * y = sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4)
1225 *
1226 * Note: this method for extracting square root does not validate that w
1227 * was indeed a square so this function will return garbage in Y if X
1228 * does not correspond to a point on the curve.
1229 */
1230
1231 /* Check prerequisite p = 3 mod 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (mbedtls_mpi_get_bit(&grp->P, 0) != 1 ||
1233 mbedtls_mpi_get_bit(&grp->P, 1) != 1) {
1234 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
1235 }
Glenn Strauss45241612022-12-19 19:37:07 -05001236
1237 int ret;
1238 mbedtls_mpi exp;
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_mpi_init(&exp);
Glenn Strauss45241612022-12-19 19:37:07 -05001240
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01001241 /* use Y to store intermediate result, actually w above */
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, Y, X));
Glenn Strauss45241612022-12-19 19:37:07 -05001243
1244 /* w = y^2 */ /* Y contains y^2 intermediate result */
1245 /* exp = ((p+1)/4) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&exp, &grp->P, 1));
1247 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&exp, 2));
Glenn Strauss45241612022-12-19 19:37:07 -05001248 /* sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(Y, Y /*y^2*/, &exp, &grp->P, NULL));
Glenn Strauss45241612022-12-19 19:37:07 -05001250
1251 /* check parity bit match or else invert Y */
1252 /* This quick inversion implementation is valid because Y != 0 for all
1253 * Short Weierstrass curves supported by mbedtls, as each supported curve
1254 * has an order that is a large prime, so each supported curve does not
1255 * have any point of order 2, and a point with Y == 0 would be of order 2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (mbedtls_mpi_get_bit(Y, 0) != parity_bit) {
1257 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(Y, &grp->P, Y));
1258 }
Glenn Strauss45241612022-12-19 19:37:07 -05001259
1260cleanup:
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 mbedtls_mpi_free(&exp);
1263 return ret;
Glenn Strauss45241612022-12-19 19:37:07 -05001264}
Valerio Settifd122f42023-04-05 18:15:32 +02001265#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Glenn Strauss45241612022-12-19 19:37:07 -05001266
Valerio Settifd122f42023-04-05 18:15:32 +02001267#if defined(MBEDTLS_ECP_C)
1268#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001269/*
1270 * For curves in short Weierstrass form, we do all the internal operations in
1271 * Jacobian coordinates.
1272 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001273 * For multiplication, we'll use a comb method with countermeasures against
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001274 * SPA, hence timing attacks.
1275 */
1276
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001277/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001278 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001279 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001281static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001282{
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (MPI_ECP_CMP_INT(&pt->Z, 0) == 0) {
1284 return 0;
1285 }
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001286
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker76f897d2022-01-02 12:47:34 +00001288 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 mbedtls_mpi_init(&T);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 MPI_ECP_INV(&T, &pt->Z); /* T <- 1 / Z */
1292 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y' <- Y*T = Y / Z */
1293 MPI_ECP_SQR(&T, &T); /* T <- T^2 = 1 / Z^2 */
1294 MPI_ECP_MUL(&pt->X, &pt->X, &T); /* X <- X * T = X / Z^2 */
1295 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y'' <- Y' * T = Y / Z^3 */
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 MPI_ECP_LSET(&pt->Z, 1);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001298
1299cleanup:
1300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 mbedtls_mpi_free(&T);
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 return ret;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +01001304}
1305
1306/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001307 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +01001308 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001309 * (See for example Cohen's "A Course in Computational Algebraic Number
1310 * Theory", Algorithm 10.3.4.)
1311 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001312 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001313 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001314 *
1315 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001317static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
1318 mbedtls_ecp_point *T[], size_t T_size)
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001319{
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (T_size < 2) {
1321 return ecp_normalize_jac(grp, *T);
1322 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001323
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1325 size_t i;
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001326 mbedtls_mpi *c, t;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) {
1329 return MBEDTLS_ERR_ECP_ALLOC_FAILED;
1330 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 mbedtls_mpi_init(&t);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 mpi_init_many(c, T_size);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001335 /*
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001336 * c[i] = Z_0 * ... * Z_i, i = 0,..,n := T_size-1
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 MPI_ECP_MOV(&c[0], &T[0]->Z);
1339 for (i = 1; i < T_size; i++) {
1340 MPI_ECP_MUL(&c[i], &c[i-1], &T[i]->Z);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001341 }
1342
1343 /*
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001344 * c[n] = 1 / (Z_0 * ... * Z_n) mod P
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 MPI_ECP_INV(&c[T_size-1], &c[T_size-1]);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 for (i = T_size - 1;; i--) {
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001349 /* At the start of iteration i (note that i decrements), we have
1350 * - c[j] = Z_0 * .... * Z_j for j < i,
1351 * - c[j] = 1 / (Z_0 * .... * Z_j) for j == i,
1352 *
1353 * This is maintained via
1354 * - c[i-1] <- c[i] * Z_i
1355 *
1356 * We also derive 1/Z_i = c[i] * c[i-1] for i>0 and use that
1357 * to do the actual normalization. For i==0, we already have
1358 * c[0] = 1 / Z_0.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001359 */
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (i > 0) {
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001362 /* Compute 1/Z_i and establish invariant for the next iteration. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 MPI_ECP_MUL(&t, &c[i], &c[i-1]);
1364 MPI_ECP_MUL(&c[i-1], &c[i], &T[i]->Z);
1365 } else {
1366 MPI_ECP_MOV(&t, &c[0]);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001367 }
1368
Hanno Beckerb8442cd2022-01-04 06:32:11 +00001369 /* Now t holds 1 / Z_i; normalize as in ecp_normalize_jac() */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
1371 MPI_ECP_SQR(&t, &t);
1372 MPI_ECP_MUL(&T[i]->X, &T[i]->X, &t);
1373 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001374
1375 /*
1376 * Post-precessing: reclaim some memory by shrinking coordinates
1377 * - not storing Z (always 1)
1378 * - shrinking other coordinates, but still keeping the same number of
1379 * limbs as P, as otherwise it will too likely be regrown too fast.
1380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n));
1382 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n));
Hanno Beckeree95f6c2022-01-09 05:46:18 +00001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 MPI_ECP_LSET(&T[i]->Z, 1);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (i == 0) {
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001387 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 }
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001389 }
1390
1391cleanup:
1392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 mbedtls_mpi_free(&t);
1394 mpi_free_many(c, T_size);
1395 mbedtls_free(c);
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 return ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001398}
1399
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001400/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001401 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1402 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
1405 mbedtls_ecp_point *Q,
1406 unsigned char inv)
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001407{
Janos Follath24eed8d2019-11-22 13:21:35 +00001408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker595616e2022-01-05 08:28:24 +00001409 mbedtls_mpi tmp;
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 mbedtls_mpi_init(&tmp);
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 MPI_ECP_COND_NEG(&Q->Y, inv);
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001413
1414cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 mbedtls_mpi_free(&tmp);
1416 return ret;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001417}
1418
1419/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001420 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001421 *
Peter Dettmance661b22015-02-07 14:43:51 +07001422 * 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 +01001423 *
Peter Dettmance661b22015-02-07 14:43:51 +07001424 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1425 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1426 *
1427 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1428 *
1429 * Cost: 1D := 3M + 4S (A == 0)
1430 * 4M + 4S (A == -3)
1431 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1434 const mbedtls_ecp_point *P,
1435 mbedtls_mpi tmp[4])
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001436{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001438 dbl_count++;
1439#endif
1440
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001442
1443 /* Special case for A = -3 */
Chien Wong153ae462023-01-31 23:27:03 +08001444 if (mbedtls_ecp_group_a_is_minus_3(grp)) {
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001445 /* tmp[0] <- M = 3(X + Z^2)(X - Z^2) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 MPI_ECP_SQR(&tmp[1], &P->Z);
1447 MPI_ECP_ADD(&tmp[2], &P->X, &tmp[1]);
1448 MPI_ECP_SUB(&tmp[3], &P->X, &tmp[1]);
1449 MPI_ECP_MUL(&tmp[1], &tmp[2], &tmp[3]);
1450 MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
1451 } else {
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001452 /* tmp[0] <- M = 3.X^2 + A.Z^4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 MPI_ECP_SQR(&tmp[1], &P->X);
1454 MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
Peter Dettmance661b22015-02-07 14:43:51 +07001455
1456 /* Optimize away for "koblitz" curves with A = 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (MPI_ECP_CMP_INT(&grp->A, 0) != 0) {
Peter Dettmance661b22015-02-07 14:43:51 +07001458 /* M += A.Z^4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 MPI_ECP_SQR(&tmp[1], &P->Z);
1460 MPI_ECP_SQR(&tmp[2], &tmp[1]);
1461 MPI_ECP_MUL(&tmp[1], &tmp[2], &grp->A);
1462 MPI_ECP_ADD(&tmp[0], &tmp[0], &tmp[1]);
Peter Dettmance661b22015-02-07 14:43:51 +07001463 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001464 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001465
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001466 /* tmp[1] <- S = 4.X.Y^2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 MPI_ECP_SQR(&tmp[2], &P->Y);
1468 MPI_ECP_SHIFT_L(&tmp[2], 1);
1469 MPI_ECP_MUL(&tmp[1], &P->X, &tmp[2]);
1470 MPI_ECP_SHIFT_L(&tmp[1], 1);
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001471
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001472 /* tmp[3] <- U = 8.Y^4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 MPI_ECP_SQR(&tmp[3], &tmp[2]);
1474 MPI_ECP_SHIFT_L(&tmp[3], 1);
Peter Dettmance661b22015-02-07 14:43:51 +07001475
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001476 /* tmp[2] <- T = M^2 - 2.S */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 MPI_ECP_SQR(&tmp[2], &tmp[0]);
1478 MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
1479 MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
Peter Dettmance661b22015-02-07 14:43:51 +07001480
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001481 /* tmp[1] <- S = M(S - T) - U */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[2]);
1483 MPI_ECP_MUL(&tmp[1], &tmp[1], &tmp[0]);
1484 MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[3]);
Peter Dettmance661b22015-02-07 14:43:51 +07001485
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001486 /* tmp[3] <- U = 2.Y.Z */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MPI_ECP_MUL(&tmp[3], &P->Y, &P->Z);
1488 MPI_ECP_SHIFT_L(&tmp[3], 1);
Peter Dettmance661b22015-02-07 14:43:51 +07001489
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001490 /* Store results */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 MPI_ECP_MOV(&R->X, &tmp[2]);
1492 MPI_ECP_MOV(&R->Y, &tmp[1]);
1493 MPI_ECP_MOV(&R->Z, &tmp[3]);
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001494
1495cleanup:
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return ret;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001498}
1499
1500/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001501 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001502 *
1503 * The coordinates of Q must be normalized (= affine),
1504 * but those of P don't need to. R is not normalized.
1505 *
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001506 * P,Q,R may alias, but only at the level of EC points: they must be either
1507 * equal as pointers, or disjoint (including the coordinate data buffers).
1508 * Fine-grained aliasing at the level of coordinates is not supported.
1509 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001510 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001511 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001512 * - at each step, P, Q and R are multiples of the base point, the factor
1513 * being less than its order, so none of them is zero;
1514 * - Q is an odd multiple of the base point, P an even multiple,
1515 * due to the choice of precomputed points in the modified comb method.
1516 * So branches for these cases do not leak secret information.
1517 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001518 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1521 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
1522 mbedtls_mpi tmp[4])
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001523{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001525 add_count++;
1526#endif
1527
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker838b7152022-01-04 05:01:53 +00001529
1530 /* NOTE: Aliasing between input and output is allowed, so one has to make
1531 * sure that at the point X,Y,Z are written, {P,Q}->{X,Y,Z} are no
1532 * longer read from. */
Hanno Becker5c8ea302022-01-01 06:01:45 +00001533 mbedtls_mpi * const X = &R->X;
1534 mbedtls_mpi * const Y = &R->Y;
1535 mbedtls_mpi * const Z = &R->Z;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (!MPI_ECP_VALID(&Q->Z)) {
1538 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1539 }
Hanno Beckeree95f6c2022-01-09 05:46:18 +00001540
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001541 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001542 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001543 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (MPI_ECP_CMP_INT(&P->Z, 0) == 0) {
1545 return mbedtls_ecp_copy(R, Q);
1546 }
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (MPI_ECP_CMP_INT(&Q->Z, 0) == 0) {
1549 return mbedtls_ecp_copy(R, P);
1550 }
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001551
1552 /*
1553 * Make sure Q coordinates are normalized
1554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (MPI_ECP_CMP_INT(&Q->Z, 1) != 0) {
1556 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1557 }
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MPI_ECP_SQR(&tmp[0], &P->Z);
1560 MPI_ECP_MUL(&tmp[1], &tmp[0], &P->Z);
1561 MPI_ECP_MUL(&tmp[0], &tmp[0], &Q->X);
1562 MPI_ECP_MUL(&tmp[1], &tmp[1], &Q->Y);
1563 MPI_ECP_SUB(&tmp[0], &tmp[0], &P->X);
1564 MPI_ECP_SUB(&tmp[1], &tmp[1], &P->Y);
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001565
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001566 /* Special cases (2) and (3) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if (MPI_ECP_CMP_INT(&tmp[0], 0) == 0) {
1568 if (MPI_ECP_CMP_INT(&tmp[1], 0) == 0) {
1569 ret = ecp_double_jac(grp, R, P, tmp);
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001570 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 } else {
1572 ret = mbedtls_ecp_set_zero(R);
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001573 goto cleanup;
1574 }
1575 }
1576
Hanno Becker838b7152022-01-04 05:01:53 +00001577 /* {P,Q}->Z no longer used, so OK to write to Z even if there's aliasing. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 MPI_ECP_MUL(Z, &P->Z, &tmp[0]);
1579 MPI_ECP_SQR(&tmp[2], &tmp[0]);
1580 MPI_ECP_MUL(&tmp[3], &tmp[2], &tmp[0]);
1581 MPI_ECP_MUL(&tmp[2], &tmp[2], &P->X);
Hanno Beckerce29ae82022-01-04 04:55:11 +00001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 MPI_ECP_MOV(&tmp[0], &tmp[2]);
1584 MPI_ECP_SHIFT_L(&tmp[0], 1);
Hanno Beckerce29ae82022-01-04 04:55:11 +00001585
Hanno Becker838b7152022-01-04 05:01:53 +00001586 /* {P,Q}->X no longer used, so OK to write to X even if there's aliasing. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 MPI_ECP_SQR(X, &tmp[1]);
1588 MPI_ECP_SUB(X, X, &tmp[0]);
1589 MPI_ECP_SUB(X, X, &tmp[3]);
1590 MPI_ECP_SUB(&tmp[2], &tmp[2], X);
1591 MPI_ECP_MUL(&tmp[2], &tmp[2], &tmp[1]);
1592 MPI_ECP_MUL(&tmp[3], &tmp[3], &P->Y);
Hanno Becker838b7152022-01-04 05:01:53 +00001593 /* {P,Q}->Y no longer used, so OK to write to Y even if there's aliasing. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 MPI_ECP_SUB(Y, &tmp[2], &tmp[3]);
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001595
1596cleanup:
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 return ret;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001599}
1600
1601/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001602 * Randomize jacobian coordinates:
1603 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001604 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001605 *
1606 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
1609 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001610{
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d629792022-01-04 06:45:49 +00001612 mbedtls_mpi l;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 mbedtls_mpi_init(&l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001615
1616 /* Generate l such that 1 < l < p */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MPI_ECP_RAND(&l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001618
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001619 /* Z' = l * Z */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 MPI_ECP_MUL(&pt->Z, &pt->Z, &l);
Hanno Becker0d629792022-01-04 06:45:49 +00001621
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001622 /* Y' = l * Y */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001624
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001625 /* X' = l^2 * X */
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 MPI_ECP_SQR(&l, &l);
1627 MPI_ECP_MUL(&pt->X, &pt->X, &l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001628
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001629 /* Y'' = l^2 * Y' = l^3 * Y */
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001631
1632cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 mbedtls_mpi_free(&l);
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Gilles Peskine59215172021-03-29 22:28:50 +02001636 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 }
1638 return ret;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001639}
1640
1641/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001642 * Check and define parameters used by the comb method (see below for details)
1643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1645#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001646#endif
1647
1648/* d = ceil( n / w ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649#define COMB_MAX_D (MBEDTLS_ECP_MAX_BITS + 1) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001650
1651/* number of precomputed points */
Gilles Peskine449bd832023-01-11 14:50:10 +01001652#define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001653
1654/*
1655 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001656 *
1657 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001658 * modified version that provides resistance to SPA by avoiding zero
1659 * digits in the representation as in [3]. We modify the method further by
1660 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001661 * representation uses one more K_i, due to carries, but saves on the size of
1662 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001663 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001664 * Summary of the comb method and its modifications:
1665 *
1666 * - The goal is to compute m*P for some w*d-bit integer m.
1667 *
1668 * - The basic comb method splits m into the w-bit integers
1669 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1670 * index has residue i modulo d, and computes m * P as
1671 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1672 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1673 *
1674 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1675 * .. + 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]] ..,
1676 * thereby successively converting it into a form where all summands
1677 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1678 *
1679 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1680 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1681 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1682 * Performing and iterating this procedure for those x[i] that are even
1683 * (keeping track of carry), we can transform the original sum into one of the form
1684 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1685 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1686 * which is why we are only computing half of it in the first place in
1687 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1688 *
1689 * - For the sake of compactness, only the seven low-order bits of x[i]
1690 * are used to represent its absolute value (K_i in the paper), and the msb
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001691 * 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 +02001692 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001693 *
1694 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001695 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001696 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001698 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1699 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001701static void ecp_comb_recode_core(unsigned char x[], size_t d,
1702 unsigned char w, const mbedtls_mpi *m)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001703{
1704 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001705 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 memset(x, 0, d+1);
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001708
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001709 /* First get the classical comb values (except for x_d = 0) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 for (i = 0; i < d; i++) {
1711 for (j = 0; j < w; j++) {
1712 x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j;
1713 }
1714 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001715
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001716 /* Now make sure x_1 .. x_d are odd */
1717 c = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 for (i = 1; i <= d; i++) {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001719 /* Add carry and update it */
1720 cc = x[i] & c;
1721 x[i] = x[i] ^ c;
1722 c = cc;
1723
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001724 /* Adjust if needed, avoiding branches */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 adjust = 1 - (x[i] & 0x01);
1726 c |= x[i] & (x[i-1] * adjust);
1727 x[i] = x[i] ^ (x[i-1] * adjust);
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001728 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001729 }
1730}
1731
1732/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001733 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001734 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001735 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001736 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001737 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1738 * 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 +01001739 *
1740 * 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 +02001741 *
1742 * Note: Even comb values (those where P would be omitted from the
1743 * sum defining T[i] above) are not needed in our adaption
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001744 * the comb method. See ecp_comb_recode_core().
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001745 *
1746 * This function currently works in four steps:
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001747 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001748 * (2) [norm_dbl] Normalization of coordinates of these T[i]
1749 * (3) [add] Computation of all T[i]
1750 * (4) [norm_add] Normalization of all T[i]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001751 *
1752 * Step 1 can be interrupted but not the others; together with the final
1753 * coordinate normalization they are the largest steps done at once, depending
1754 * on the window size. Here are operation counts for P-256:
1755 *
1756 * step (2) (3) (4)
1757 * w = 5 142 165 208
1758 * w = 4 136 77 160
1759 * w = 3 130 33 136
1760 * w = 2 124 11 124
1761 *
1762 * So if ECC operations are blocking for too long even with a low max_ops
1763 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1764 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001765 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001766static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
1767 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
1768 unsigned char w, size_t d,
1769 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001770{
Janos Follath24eed8d2019-11-22 13:21:35 +00001771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001772 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001773 size_t j = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 const unsigned char T_size = 1U << (w - 1);
1775 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1] = { NULL };
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001776
Hanno Beckera7f8edd2022-01-04 07:29:46 +00001777 mbedtls_mpi tmp[4];
1778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Beckera7f8edd2022-01-04 07:29:46 +00001780
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001781#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1783 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001784 goto dbl;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 }
1786 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) {
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001787 goto norm_dbl;
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 }
1789 if (rs_ctx->rsm->state == ecp_rsm_pre_add) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001790 goto add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 }
1792 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001793 goto norm_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 }
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001795 }
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001796#else
1797 (void) rs_ctx;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001798#endif
1799
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001800#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001802 rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1803
1804 /* initial state for the loop */
1805 rs_ctx->rsm->i = 0;
1806 }
1807
1808dbl:
1809#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001810 /*
1811 * Set T[0] = P and
1812 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1813 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&T[0], P));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001815
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001816#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001818 j = rs_ctx->rsm->i;
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 } else
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001820#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 j = 0;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 for (; j < d * (w - 1); j++) {
1824 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL);
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 i = 1U << (j / d);
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001827 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if (j % d == 0) {
1830 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(cur, T + (i >> 1)));
1831 }
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 MBEDTLS_MPI_CHK(ecp_double_jac(grp, cur, cur, tmp));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001834 }
1835
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001836#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001838 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001840
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001841norm_dbl:
1842#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001843 /*
Hanno Beckerac4d4bc2022-01-09 05:58:49 +00001844 * Normalize current elements in T to allow them to be used in
1845 * ecp_add_mixed() below, which requires one normalized input.
1846 *
1847 * As T has holes, use an auxiliary array of pointers to elements in T.
1848 *
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001849 */
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001850 j = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 for (i = 1; i < T_size; i <<= 1) {
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001852 TT[j++] = T + i;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 }
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001858
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001859#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001861 rs_ctx->rsm->state = ecp_rsm_pre_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001863
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001864add:
1865#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001866 /*
1867 * Compute the remaining ones using the minimal number of additions
1868 * Be careful to update T[2^l] only after using it!
1869 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 for (i = 1; i < T_size; i <<= 1) {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001873 j = i;
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 while (j--) {
1875 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, &T[i + j], &T[j], &T[i], tmp));
1876 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001877 }
1878
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001879#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001881 rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001883
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001884norm_add:
1885#endif
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001886 /*
Manuel Pégourié-Gonnarda966fde2018-10-23 10:41:11 +02001887 * Normalize final elements in T. Even though there are no holes now, we
1888 * still need the auxiliary array for homogeneity with the previous
1889 * call. Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02001890 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 for (j = 0; j + 1 < T_size; j++) {
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001892 TT[j] = T + j + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 }
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001898
Hanno Beckeree95f6c2022-01-09 05:46:18 +00001899 /* Free Z coordinate (=1 after normalization) to save RAM.
1900 * This makes T[i] invalid as mbedtls_ecp_points, but this is OK
1901 * since from this point onwards, they are only accessed indirectly
1902 * via the getter function ecp_select_comb() which does set the
1903 * target's Z coordinate to 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 for (i = 0; i < T_size; i++) {
1905 mbedtls_mpi_free(&T[i].Z);
1906 }
Hanno Beckeree95f6c2022-01-09 05:46:18 +00001907
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001908cleanup:
Hanno Beckera7f8edd2022-01-04 07:29:46 +00001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Beckera7f8edd2022-01-04 07:29:46 +00001911
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001912#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
1914 ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
1915 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001916 rs_ctx->rsm->i = j;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 }
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001918 }
1919#endif
Janos Follathb0697532016-08-18 12:38:46 +01001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001922}
1923
1924/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001925 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001926 *
1927 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001928 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1930 const mbedtls_ecp_point T[], unsigned char T_size,
1931 unsigned char i)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001932{
Janos Follath24eed8d2019-11-22 13:21:35 +00001933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001934 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001935
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001936 /* Ignore the "sign" bit and scale down */
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 ii = (i & 0x7Fu) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001938
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001939 /* Read the whole table to thwart cache-based timing attacks */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 for (j = 0; j < T_size; j++) {
1941 MPI_ECP_COND_ASSIGN(&R->X, &T[j].X, j == ii);
1942 MPI_ECP_COND_ASSIGN(&R->Y, &T[j].Y, j == ii);
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001943 }
1944
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001945 /* Safely invert result if i is "negative" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, R, i >> 7));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 MPI_ECP_LSET(&R->Z, 1);
Hanno Becker6a288702022-01-05 05:19:48 +00001949
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001950cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001952}
1953
1954/*
1955 * Core multiplication algorithm for the (modified) comb method.
1956 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001957 *
1958 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001959 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1961 const mbedtls_ecp_point T[], unsigned char T_size,
1962 const unsigned char x[], size_t d,
1963 int (*f_rng)(void *, unsigned char *, size_t),
1964 void *p_rng,
1965 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001966{
Janos Follath24eed8d2019-11-22 13:21:35 +00001967 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 mbedtls_ecp_point Txi;
Hanno Beckera7f8edd2022-01-04 07:29:46 +00001969 mbedtls_mpi tmp[4];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001970 size_t i;
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 mbedtls_ecp_point_init(&Txi);
1973 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001974
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001975#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001976 (void) rs_ctx;
1977#endif
1978
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001979#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
1981 rs_ctx->rsm->state != ecp_rsm_comb_core) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02001982 rs_ctx->rsm->i = 0;
1983 rs_ctx->rsm->state = ecp_rsm_comb_core;
1984 }
1985
1986 /* new 'if' instead of nested for the sake of the 'else' branch */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001988 /* restore current index (R already pointing to rs_ctx->rsm->R) */
1989 i = rs_ctx->rsm->i;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 } else
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001991#endif
1992 {
1993 /* Start with a non-zero point and randomize its coordinates */
1994 i = d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 MBEDTLS_MPI_CHK(ecp_select_comb(grp, R, T, T_size, x[i]));
1996 if (f_rng != 0) {
1997 MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, R, f_rng, p_rng));
1998 }
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001999 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 while (i != 0) {
2002 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD);
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002003 --i;
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 MBEDTLS_MPI_CHK(ecp_double_jac(grp, R, R, tmp));
2006 MBEDTLS_MPI_CHK(ecp_select_comb(grp, &Txi, T, T_size, x[i]));
2007 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, R, R, &Txi, tmp));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002008 }
2009
2010cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 mbedtls_ecp_point_free(&Txi);
2013 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Beckera7f8edd2022-01-04 07:29:46 +00002014
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002015#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
2017 ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard90f31b72018-10-16 10:45:24 +02002018 rs_ctx->rsm->i = i;
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002019 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01002020 }
2021#endif
2022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002024}
2025
2026/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002027 * Recode the scalar to get constant-time comb multiplication
2028 *
2029 * As the actual scalar recoding needs an odd scalar as a starting point,
2030 * this wrapper ensures that by replacing m by N - m if necessary, and
2031 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002032 *
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002033 * This works because we only support large prime order for Short Weierstrass
2034 * curves, so N is always odd hence either m or N - m is.
2035 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002036 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038static int ecp_comb_recode_scalar(const mbedtls_ecp_group *grp,
2039 const mbedtls_mpi *m,
2040 unsigned char k[COMB_MAX_D + 1],
2041 size_t d,
2042 unsigned char w,
2043 unsigned char *parity_trick)
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002044{
Janos Follath24eed8d2019-11-22 13:21:35 +00002045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002046 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 mbedtls_mpi_init(&M);
2049 mbedtls_mpi_init(&mm);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002050
Manuel Pégourié-Gonnardfd87e352017-08-24 14:21:05 +02002051 /* N is always odd (see above), just make extra sure */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) {
2053 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2054 }
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002055
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002056 /* do we need the parity trick? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 *parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0);
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002058
2059 /* execute parity fix in constant time */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m));
2061 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m));
2062 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick));
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002063
2064 /* actual scalar recoding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 ecp_comb_recode_core(k, d, w, &M);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002066
2067cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 mbedtls_mpi_free(&mm);
2069 mbedtls_mpi_free(&M);
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return ret;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01002072}
2073
2074/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002075 * Perform comb multiplication (for short Weierstrass curves)
2076 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002077 *
2078 * Scalar recoding may use a parity trick that makes us compute -m * P,
2079 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081static int ecp_mul_comb_after_precomp(const mbedtls_ecp_group *grp,
2082 mbedtls_ecp_point *R,
2083 const mbedtls_mpi *m,
2084 const mbedtls_ecp_point *T,
2085 unsigned char T_size,
2086 unsigned char w,
2087 size_t d,
2088 int (*f_rng)(void *, unsigned char *, size_t),
2089 void *p_rng,
2090 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002091{
Janos Follath24eed8d2019-11-22 13:21:35 +00002092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01002093 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002094 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002095 mbedtls_ecp_point *RR = R;
2096
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002097#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002099 RR = &rs_ctx->rsm->R;
2100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (rs_ctx->rsm->state == ecp_rsm_final_norm) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002102 goto final_norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 }
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002104 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002105#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w,
2108 &parity_trick));
2109 MBEDTLS_MPI_CHK(ecp_mul_comb_core(grp, RR, T, T_size, k, d,
2110 f_rng, p_rng, rs_ctx));
2111 MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, RR, parity_trick));
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002112
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002113#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002115 rs_ctx->rsm->state = ecp_rsm_final_norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 }
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002117
Manuel Pégourié-Gonnard4ed1dab2017-08-24 11:02:04 +02002118final_norm:
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01002120#endif
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002121 /*
2122 * Knowledge of the jacobian coordinates may leak the last few bits of the
2123 * scalar [1], and since our MPI implementation isn't constant-flow,
2124 * inversion (used for coordinate normalization) may leak the full value
2125 * of its input via side-channels [2].
2126 *
2127 * [1] https://eprint.iacr.org/2003/191
2128 * [2] https://eprint.iacr.org/2020/055
2129 *
2130 * Avoid the leak by randomizing coordinates before we normalize them.
2131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (f_rng != 0) {
2133 MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, RR, f_rng, p_rng));
2134 }
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, RR));
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002137
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002138#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
2140 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, RR));
2141 }
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01002142#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002143
2144cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 return ret;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002146}
2147
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002148/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002149 * Pick window size based on curve size and whether we optimize for base point
2150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151static unsigned char ecp_pick_window_size(const mbedtls_ecp_group *grp,
2152 unsigned char p_eq_g)
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002153{
2154 unsigned char w;
2155
2156 /*
2157 * Minimize the number of multiplications, that is minimize
2158 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2159 * (see costs of the various parts, with 1S = 1M)
2160 */
2161 w = grp->nbits >= 384 ? 5 : 4;
2162
2163 /*
2164 * If P == G, pre-compute a bit more, since this may be re-used later.
2165 * Just adding one avoids upping the cost of the first mul too much,
2166 * and the memory cost too.
2167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (p_eq_g) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002169 w++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 }
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002171
2172 /*
kXuanba9cb762021-04-08 14:32:06 +08002173 * If static comb table may not be used (!p_eq_g) or static comb table does
2174 * not exists, make sure w is within bounds.
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002175 * (The last test is useful only for very small curves in the test suite.)
kXuanba9cb762021-04-08 14:32:06 +08002176 *
2177 * The user reduces MBEDTLS_ECP_WINDOW_SIZE does not changes the size of
2178 * static comb table, because the size of static comb table is fixed when
2179 * it is generated.
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181#if (MBEDTLS_ECP_WINDOW_SIZE < 6)
2182 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 +01002183 w = MBEDTLS_ECP_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 }
k-stachowiak653a4a22019-07-03 14:31:09 +02002185#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (w >= grp->nbits) {
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002187 w = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 }
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 return w;
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01002191}
2192
2193/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002194 * Multiplication using the comb method - for curves in short Weierstrass form
2195 *
2196 * This function is mainly responsible for administrative work:
2197 * - managing the restart context if enabled
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002198 * - managing the table of precomputed points (passed between the below two
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002199 * functions): allocation, computation, ownership transfer, freeing.
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002200 *
2201 * It delegates the actual arithmetic work to:
2202 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2203 *
2204 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2207 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2208 int (*f_rng)(void *, unsigned char *, size_t),
2209 void *p_rng,
2210 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002211{
Janos Follath24eed8d2019-11-22 13:21:35 +00002212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002213 unsigned char w, p_eq_g, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01002214 size_t d;
Manuel Pégourié-Gonnardf2a9fcf2020-06-03 12:11:56 +02002215 unsigned char T_size = 0, T_ok = 0;
2216 mbedtls_ecp_point *T = NULL;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 ECP_RS_ENTER(rsm);
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01002219
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002220 /* Is P the base point ? */
2221#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 p_eq_g = (MPI_ECP_CMP(&P->Y, &grp->G.Y) == 0 &&
2223 MPI_ECP_CMP(&P->X, &grp->G.X) == 0);
Manuel Pégourié-Gonnard196d1332017-08-28 13:14:27 +02002224#else
2225 p_eq_g = 0;
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01002226#endif
2227
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002228 /* Pick window size and deduce related sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 w = ecp_pick_window_size(grp, p_eq_g);
2230 T_size = 1U << (w - 1);
2231 d = (grp->nbits + w - 1) / w;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002232
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002233 /* Pre-computed table: do we have it already for the base point? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (p_eq_g && grp->T != NULL) {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002235 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002236 T = grp->T;
2237 T_ok = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 } else
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002239#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002240 /* Pre-computed table: do we have one in progress? complete? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01002242 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002243 T = rs_ctx->rsm->T;
2244 rs_ctx->rsm->T = NULL;
2245 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002246
Manuel Pégourié-Gonnardb25cb602018-10-16 11:48:09 +02002247 /* This effectively jumps to the call to mul_comb_after_precomp() */
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002248 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 } else
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002250#endif
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002251 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002252 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 T = mbedtls_calloc(T_size, sizeof(mbedtls_ecp_point));
2254 if (T == NULL) {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002255 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002256 goto cleanup;
2257 }
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +02002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 for (i = 0; i < T_size; i++) {
2260 mbedtls_ecp_point_init(&T[i]);
2261 }
Manuel Pégourié-Gonnard11556e22017-08-24 13:41:19 +02002262
2263 T_ok = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002264 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002265
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01002266 /* Compute table (or finish computing it) if not done already */
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (!T_ok) {
2268 MBEDTLS_MPI_CHK(ecp_precompute_comb(grp, T, P, w, d, rs_ctx));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (p_eq_g) {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02002271 /* almost transfer ownership of T to the group, but keep a copy of
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +02002272 * the pointer to use for calling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002273 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002274 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002275 }
2276 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002277
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01002278 /* Actual comb multiplication using precomputed points */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m,
2280 T, T_size, w, d,
2281 f_rng, p_rng, rs_ctx));
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002282
2283cleanup:
2284
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002285 /* does T belong to the group? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (T == grp->T) {
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002287 T = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 }
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002289
2290 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002291#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 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 +01002293 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02002294 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02002295 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01002296 T = NULL;
2297 }
2298#endif
2299
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002300 /* did T belong to us? then let's destroy it! */
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (T != NULL) {
2302 for (i = 0; i < T_size; i++) {
2303 mbedtls_ecp_point_free(&T[i]);
2304 }
2305 mbedtls_free(T);
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002306 }
2307
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01002308 /* prevent caller from using invalid value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 int should_free_R = (ret != 0);
David Horstmannfc735df2022-10-06 19:11:04 +01002310#if defined(MBEDTLS_ECP_RESTARTABLE)
2311 /* don't free R while in progress in case R == P */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
David Horstmann6e116872022-10-25 10:32:08 +01002313 should_free_R = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
David Horstmannfc735df2022-10-06 19:11:04 +01002315#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (should_free_R) {
2317 mbedtls_ecp_point_free(R);
2318 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 ECP_RS_LEAVE(rsm);
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return ret;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002323}
2324
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002325#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002326
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002327#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002328/*
2329 * For Montgomery curves, we do all the internal arithmetic in projective
2330 * coordinates. Import/export of points uses only the x coordinates, which is
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002331 * internally represented as X / Z.
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002332 *
2333 * For scalar multiplication, we'll use a Montgomery ladder.
2334 */
2335
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01002336/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002337 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2338 * Cost: 1M + 1I
2339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002341{
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MPI_ECP_INV(&P->Z, &P->Z);
2344 MPI_ECP_MUL(&P->X, &P->X, &P->Z);
2345 MPI_ECP_LSET(&P->Z, 1);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002346
2347cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 return ret;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002349}
2350
2351/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002352 * Randomize projective x/z coordinates:
2353 * (X, Z) -> (l X, l Z) for random l
2354 * This is sort of the reverse operation of ecp_normalize_mxz().
2355 *
2356 * This countermeasure was first suggested in [2].
2357 * Cost: 2M
2358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002359static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
2360 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002361{
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2363 mbedtls_mpi l;
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 mbedtls_mpi_init(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002365
2366 /* Generate l such that 1 < l < p */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MPI_ECP_RAND(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MPI_ECP_MUL(&P->X, &P->X, &l);
2370 MPI_ECP_MUL(&P->Z, &P->Z, &l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002371
2372cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 mbedtls_mpi_free(&l);
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Gilles Peskine59215172021-03-29 22:28:50 +02002376 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 }
2378 return ret;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002379}
2380
2381/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002382 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2383 * for Montgomery curves in x/z coordinates.
2384 *
2385 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2386 * with
2387 * d = X1
2388 * P = (X2, Z2)
2389 * Q = (X3, Z3)
2390 * R = (X4, Z4)
2391 * S = (X5, Z5)
2392 * and eliminating temporary variables tO, ..., t4.
2393 *
2394 * Cost: 5M + 4S
2395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
2397 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2398 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2399 const mbedtls_mpi *d,
2400 mbedtls_mpi T[4])
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002401{
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7eb2aa02021-01-22 09:43:59 +01002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 MPI_ECP_ADD(&T[0], &P->X, &P->Z); /* Pp := PX + PZ */
2405 MPI_ECP_SUB(&T[1], &P->X, &P->Z); /* Pm := PX - PZ */
2406 MPI_ECP_ADD(&T[2], &Q->X, &Q->Z); /* Qp := QX + XZ */
2407 MPI_ECP_SUB(&T[3], &Q->X, &Q->Z); /* Qm := QX - QZ */
2408 MPI_ECP_MUL(&T[3], &T[3], &T[0]); /* Qm * Pp */
2409 MPI_ECP_MUL(&T[2], &T[2], &T[1]); /* Qp * Pm */
2410 MPI_ECP_SQR(&T[0], &T[0]); /* Pp^2 */
2411 MPI_ECP_SQR(&T[1], &T[1]); /* Pm^2 */
2412 MPI_ECP_MUL(&R->X, &T[0], &T[1]); /* Pp^2 * Pm^2 */
2413 MPI_ECP_SUB(&T[0], &T[0], &T[1]); /* Pp^2 - Pm^2 */
2414 MPI_ECP_MUL(&R->Z, &grp->A, &T[0]); /* A * (Pp^2 - Pm^2) */
2415 MPI_ECP_ADD(&R->Z, &T[1], &R->Z); /* [ A * (Pp^2-Pm^2) ] + Pm^2 */
2416 MPI_ECP_ADD(&S->X, &T[3], &T[2]); /* Qm*Pp + Qp*Pm */
2417 MPI_ECP_SQR(&S->X, &S->X); /* (Qm*Pp + Qp*Pm)^2 */
2418 MPI_ECP_SUB(&S->Z, &T[3], &T[2]); /* Qm*Pp - Qp*Pm */
2419 MPI_ECP_SQR(&S->Z, &S->Z); /* (Qm*Pp - Qp*Pm)^2 */
2420 MPI_ECP_MUL(&S->Z, d, &S->Z); /* d * ( Qm*Pp - Qp*Pm )^2 */
2421 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 +01002422
2423cleanup:
Hanno Becker28ccb1c2022-01-04 07:15:04 +00002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return ret;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002426}
2427
2428/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002429 * Multiplication with Montgomery ladder in x/z coordinates,
2430 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2433 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2434 int (*f_rng)(void *, unsigned char *, size_t),
2435 void *p_rng)
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002436{
Janos Follath24eed8d2019-11-22 13:21:35 +00002437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002438 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002439 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 mbedtls_ecp_point RP;
2441 mbedtls_mpi PX;
Hanno Becker30838862022-01-04 13:25:59 +00002442 mbedtls_mpi tmp[4];
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 mbedtls_ecp_point_init(&RP); mbedtls_mpi_init(&PX);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Becker30838862022-01-04 13:25:59 +00002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (f_rng == NULL) {
2448 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2449 }
Manuel Pégourié-Gonnard02b57052021-06-15 11:29:26 +02002450
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002451 /* Save PX and read from P before writing to R, in case P == R */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 MPI_ECP_MOV(&PX, &P->X);
2453 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&RP, P));
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002454
2455 /* Set R to zero in modified x/z coordinates */
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 MPI_ECP_LSET(&R->X, 1);
2457 MPI_ECP_LSET(&R->Z, 0);
2458 mbedtls_mpi_free(&R->Y);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002459
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002460 /* RP.X might be slightly larger than P, so reduce it */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MOD_ADD(&RP.X);
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002462
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002463 /* Randomize coordinates of the starting point */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, &RP, f_rng, p_rng));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002465
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002466 /* Loop invariant: R = result so far, RP = R + P */
Aurelien Jarnoc79ce882022-05-15 13:24:05 +02002467 i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 while (i-- > 0) {
2469 b = mbedtls_mpi_get_bit(m, i);
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002470 /*
2471 * if (b) R = 2R + P else R = 2R,
2472 * which is:
2473 * if (b) double_add( RP, R, RP, R )
2474 * else double_add( R, RP, R, RP )
2475 * but using safe conditional swaps to avoid leaks
2476 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
2478 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
2479 MBEDTLS_MPI_CHK(ecp_double_add_mxz(grp, R, &RP, R, &RP, &PX, tmp));
2480 MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
2481 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002482 }
2483
Manuel Pégourié-Gonnarda4aa89b2020-03-25 12:41:29 +01002484 /*
2485 * Knowledge of the projective coordinates may leak the last few bits of the
2486 * scalar [1], and since our MPI implementation isn't constant-flow,
2487 * inversion (used for coordinate normalization) may leak the full value
2488 * of its input via side-channels [2].
2489 *
2490 * [1] https://eprint.iacr.org/2003/191
2491 * [2] https://eprint.iacr.org/2020/055
2492 *
2493 * Avoid the leak by randomizing coordinates before we normalize them.
2494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, R, f_rng, p_rng));
2496 MBEDTLS_MPI_CHK(ecp_normalize_mxz(grp, R));
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002497
2498cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 mbedtls_ecp_point_free(&RP); mbedtls_mpi_free(&PX);
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
2502 return ret;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002503}
2504
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002505#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002506
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002507/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002508 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002509 *
2510 * This internal function can be called without an RNG in case where we know
2511 * the inputs are not sensitive.
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002513static int ecp_mul_restartable_internal(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2514 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2515 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2516 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002517{
Janos Follathb0697532016-08-18 12:38:46 +01002518 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02002519
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002520#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002521 /* reset ops count for this call if top-level */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (rs_ctx != NULL && rs_ctx->depth++ == 0) {
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002523 rs_ctx->ops_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 }
Gilles Peskine59970052019-02-28 13:12:06 +01002525#else
2526 (void) rs_ctx;
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002527#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002528
David Horstmannfc735df2022-10-06 19:11:04 +01002529 int restarting = 0;
Manuel Pégourié-Gonnard95aedfe2017-08-24 13:47:04 +02002530#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002532#endif
David Horstmannfc735df2022-10-06 19:11:04 +01002533 /* skip argument check when restarting */
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (!restarting) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002535 /* check_privkey is free */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_CHK);
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002537
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002538 /* Common sanity checks */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m));
2540 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002541 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002542
2543 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002544#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2546 MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng));
2547 }
Janos Follath430d3372016-11-03 14:25:37 +00002548#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002549#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2551 MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx));
2552 }
Janos Follath430d3372016-11-03 14:25:37 +00002553#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002554
Janos Follath6c8ccd52016-11-29 15:37:09 +00002555cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002556
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002557#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (rs_ctx != NULL) {
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002559 rs_ctx->depth--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002561#endif
2562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 return ret;
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002564}
2565
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002566/*
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002567 * Restartable multiplication R = m * P
2568 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2570 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2571 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2572 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002573{
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (f_rng == NULL) {
2575 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2576 }
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 return ecp_mul_restartable_internal(grp, R, m, P, f_rng, p_rng, rs_ctx);
Manuel Pégourié-Gonnard75525ae2021-06-15 11:29:26 +02002579}
2580
2581/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002582 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002583 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2585 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2586 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002587{
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002589}
Valerio Settifd122f42023-04-05 18:15:32 +02002590#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002591
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002592#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002593/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002594 * Check that an affine point is valid as a public key,
2595 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002597static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002598{
Janos Follath24eed8d2019-11-22 13:21:35 +00002599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002601
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002602 /* pt coordinates must be normalized for our checks */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
2604 mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
2605 mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
2606 mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) {
2607 return MBEDTLS_ERR_ECP_INVALID_KEY;
2608 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 mbedtls_mpi_init(&YY); mbedtls_mpi_init(&RHS);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002611
2612 /*
2613 * YY = Y^2
Manuel Pégourié-Gonnard8b6d14b2022-12-20 10:02:52 +01002614 * RHS = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 MPI_ECP_SQR(&YY, &pt->Y);
2617 MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, &RHS, &pt->X));
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if (MPI_ECP_CMP(&YY, &RHS) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 }
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002622
2623cleanup:
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 mbedtls_mpi_free(&YY); mbedtls_mpi_free(&RHS);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 return ret;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002628}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002629#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002630
Valerio Settifd122f42023-04-05 18:15:32 +02002631#if defined(MBEDTLS_ECP_C)
Gilles Peskine9b99a892018-09-14 18:32:19 +02002632#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002633/*
TRodziewicz9edff742021-03-04 17:59:39 +01002634 * R = m * P with shortcuts for m == 0, m == 1 and m == -1
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002635 * NOT constant-time - ONLY for short Weierstrass!
2636 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
2638 mbedtls_ecp_point *R,
2639 const mbedtls_mpi *m,
2640 const mbedtls_ecp_point *P,
2641 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002642{
Janos Follath24eed8d2019-11-22 13:21:35 +00002643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc27a0e02022-01-06 05:56:34 +00002644 mbedtls_mpi tmp;
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 mbedtls_mpi_init(&tmp);
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (mbedtls_mpi_cmp_int(m, 0) == 0) {
2648 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2649 MBEDTLS_MPI_CHK(mbedtls_ecp_set_zero(R));
2650 } else if (mbedtls_mpi_cmp_int(m, 1) == 0) {
2651 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2652 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2653 } else if (mbedtls_mpi_cmp_int(m, -1) == 0) {
2654 MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2655 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2656 MPI_ECP_NEG(&R->Y);
2657 } else {
2658 MBEDTLS_MPI_CHK(ecp_mul_restartable_internal(grp, R, m, P,
2659 NULL, NULL, rs_ctx));
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002660 }
2661
2662cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 mbedtls_mpi_free(&tmp);
Hanno Beckerc27a0e02022-01-06 05:56:34 +00002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 return ret;
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002666}
2667
2668/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002669 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002670 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002671 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002672int mbedtls_ecp_muladd_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2674 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2675 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2676 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002677{
Janos Follath24eed8d2019-11-22 13:21:35 +00002678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002679 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002680 mbedtls_ecp_point *pmP = &mP;
2681 mbedtls_ecp_point *pR = R;
Hanno Becker3b29f212022-01-04 07:34:14 +00002682 mbedtls_mpi tmp[4];
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2684 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
2685 }
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 mbedtls_ecp_point_init(&mP);
2688 mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Becker3b29f212022-01-04 07:34:14 +00002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 ECP_RS_ENTER(ma);
Manuel Pégourié-Gonnarddb4a8eb2017-08-23 18:18:22 +02002691
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002692#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002694 /* redirect intermediate results to restart context */
2695 pmP = &rs_ctx->ma->mP;
2696 pR = &rs_ctx->ma->R;
2697
2698 /* jump to next operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (rs_ctx->ma->state == ecp_rsma_mul2) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002700 goto mul2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 }
2702 if (rs_ctx->ma->state == ecp_rsma_add) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002703 goto add;
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 }
2705 if (rs_ctx->ma->state == ecp_rsma_norm) {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002706 goto norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 }
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002708 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002709#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx));
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002712#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002714 rs_ctx->ma->state = ecp_rsma_mul2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 }
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002716
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002717mul2:
2718#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pR, n, Q, rs_ctx));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002720
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002721#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002723 rs_ctx->ma->state = ecp_rsma_add;
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 }
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002725
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002726add:
2727#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_ADD);
2729 MBEDTLS_MPI_CHK(ecp_add_mixed(grp, pR, pmP, pR, tmp));
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002730#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
Manuel Pégourié-Gonnardc9efa002017-08-24 10:25:06 +02002732 rs_ctx->ma->state = ecp_rsma_norm;
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 }
Janos Follath430d3372016-11-03 14:25:37 +00002734
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002735norm:
2736#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
2738 MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, pR));
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002739
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002740#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2742 MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, pR));
2743 }
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002744#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002745
2746cleanup:
Hanno Becker3b29f212022-01-04 07:34:14 +00002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
Hanno Becker3b29f212022-01-04 07:34:14 +00002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 mbedtls_ecp_point_free(&mP);
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 ECP_RS_LEAVE(ma);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 return ret;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002755}
2756
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002757/*
2758 * Linear combination
2759 * NOT constant-time
2760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002761int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2762 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2763 const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002764{
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL);
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002766}
Gilles Peskine9b99a892018-09-14 18:32:19 +02002767#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Valerio Settifd122f42023-04-05 18:15:32 +02002768#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002769
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002770#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002771#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Dave Rodgmanb8f18852023-07-18 12:45:17 +01002772#define ECP_MPI_INIT(_p, _n) { .p = (mbedtls_mpi_uint *) (_p), .s = 1, .n = (_n) }
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002773#define ECP_MPI_INIT_ARRAY(x) \
Dave Rodgmanb8f18852023-07-18 12:45:17 +01002774 ECP_MPI_INIT(x, sizeof(x) / sizeof(mbedtls_mpi_uint))
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002775/*
2776 * Constants for the two points other than 0, 1, -1 (mod p) in
2777 * https://cr.yp.to/ecdh.html#validate
2778 * See ecp_check_pubkey_x25519().
2779 */
2780static const mbedtls_mpi_uint x25519_bad_point_1[] = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 MBEDTLS_BYTES_TO_T_UINT_8(0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae),
2782 MBEDTLS_BYTES_TO_T_UINT_8(0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a),
2783 MBEDTLS_BYTES_TO_T_UINT_8(0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd),
2784 MBEDTLS_BYTES_TO_T_UINT_8(0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00),
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002785};
2786static const mbedtls_mpi_uint x25519_bad_point_2[] = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 MBEDTLS_BYTES_TO_T_UINT_8(0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24),
2788 MBEDTLS_BYTES_TO_T_UINT_8(0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b),
2789 MBEDTLS_BYTES_TO_T_UINT_8(0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86),
2790 MBEDTLS_BYTES_TO_T_UINT_8(0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57),
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002791};
2792static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 x25519_bad_point_1);
Manuel Pégourié-Gonnard06215ea2021-06-23 12:53:18 +02002794static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 x25519_bad_point_2);
Janos Follath865a75e2021-06-24 15:34:59 +01002796#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002797
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002798/*
2799 * Check that the input point is not one of the low-order points.
2800 * This is recommended by the "May the Fourth" paper:
2801 * https://eprint.iacr.org/2017/806.pdf
2802 * Those points are never sent by an honest peer.
2803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002804static int ecp_check_bad_points_mx(const mbedtls_mpi *X, const mbedtls_mpi *P,
2805 const mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002806{
2807 int ret;
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002808 mbedtls_mpi XmP;
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 mbedtls_mpi_init(&XmP);
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002811
2812 /* Reduce X mod P so that we only need to check values less than P.
2813 * We know X < 2^256 so we can proceed by subtraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&XmP, X));
2815 while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) {
2816 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&XmP, &XmP, P));
2817 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002818
Janos Follath865a75e2021-06-24 15:34:59 +01002819 /* Check against the known bad values that are less than P. For Curve448
2820 * these are 0, 1 and -1. For Curve25519 we check the values less than P
2821 * from the following list: https://cr.yp.to/ecdh.html#validate */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) { /* takes care of 0 and 1 */
Janos Follath8081ced2021-06-24 14:24:13 +01002823 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2824 goto cleanup;
2825 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002826
Janos Follath865a75e2021-06-24 15:34:59 +01002827#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
2829 if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) {
Janos Follath865a75e2021-06-24 15:34:59 +01002830 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2831 goto cleanup;
2832 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) {
Janos Follath865a75e2021-06-24 15:34:59 +01002835 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2836 goto cleanup;
2837 }
Janos Follath8081ced2021-06-24 14:24:13 +01002838 }
Janos Follath83e384d2021-06-25 15:29:56 +01002839#else
2840 (void) grp_id;
Janos Follath865a75e2021-06-24 15:34:59 +01002841#endif
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002842
Manuel Pégourié-Gonnard2389a602021-06-23 12:25:48 +02002843 /* Final check: check if XmP + 1 is P (final because it changes XmP!) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&XmP, &XmP, 1));
2845 if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) {
Janos Follath8081ced2021-06-24 14:24:13 +01002846 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2847 goto cleanup;
2848 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002849
2850 ret = 0;
2851
2852cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 mbedtls_mpi_free(&XmP);
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 return ret;
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002856}
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002857
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002858/*
2859 * Check validity of a public key for Montgomery curves with x-only schemes
2860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002862{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002863 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002864 /* Allow any public value, if it's too big then we'll just reduce it mod p
2865 * (RFC 7748 sec. 5 para. 3). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) {
2867 return MBEDTLS_ERR_ECP_INVALID_KEY;
2868 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002869
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002870 /* Implicit in all standards (as they don't consider negative numbers):
2871 * X must be non-negative. This is normally ensured by the way it's
2872 * encoded for transmission, but let's be extra sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) {
2874 return MBEDTLS_ERR_ECP_INVALID_KEY;
2875 }
Manuel Pégourié-Gonnardf29857c2021-06-23 10:14:58 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id);
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002878}
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002879#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002880
2881/*
2882 * Check that a point is valid as a public key
2883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
2885 const mbedtls_ecp_point *pt)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002886{
2887 /* Must use affine coordinates */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) {
2889 return MBEDTLS_ERR_ECP_INVALID_KEY;
2890 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002891
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002892#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2894 return ecp_check_pubkey_mx(grp, pt);
2895 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002896#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002897#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2899 return ecp_check_pubkey_sw(grp, pt);
2900 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002901#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002903}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002904
2905/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
2909 const mbedtls_mpi *d)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002910{
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002911#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002913 /* see RFC 7748 sec. 5 para. 5 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (mbedtls_mpi_get_bit(d, 0) != 0 ||
2915 mbedtls_mpi_get_bit(d, 1) != 0 ||
2916 mbedtls_mpi_bitlen(d) - 1 != grp->nbits) { /* mbedtls_mpi_bitlen is one-based! */
2917 return MBEDTLS_ERR_ECP_INVALID_KEY;
2918 }
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002919
2920 /* see [Curve25519] page 5 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) {
2922 return MBEDTLS_ERR_ECP_INVALID_KEY;
2923 }
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 return 0;
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002926 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002927#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2928#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002930 /* see SEC1 3.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
2932 mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
2933 return MBEDTLS_ERR_ECP_INVALID_KEY;
2934 } else {
2935 return 0;
2936 }
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002937 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002938#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002939
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002941}
2942
Gilles Peskine72fcc982021-03-23 22:31:31 +01002943#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2944MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +01002945int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
2946 mbedtls_mpi *d,
2947 int (*f_rng)(void *, unsigned char *, size_t),
2948 void *p_rng)
Gilles Peskine72fcc982021-03-23 22:31:31 +01002949{
2950 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Gilles Peskine61f1f5f2021-03-24 12:46:46 +01002951 size_t n_random_bytes = high_bit / 8 + 1;
Gilles Peskine72fcc982021-03-23 22:31:31 +01002952
2953 /* [Curve25519] page 5 */
Gilles Peskine61f1f5f2021-03-24 12:46:46 +01002954 /* Generate a (high_bit+1)-bit random number by generating just enough
2955 * random bytes, then shifting out extra bits from the top (necessary
2956 * when (high_bit+1) is not a multiple of 8). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(d, n_random_bytes,
2958 f_rng, p_rng));
2959 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1));
Gilles Peskine72fcc982021-03-23 22:31:31 +01002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, high_bit, 1));
Gilles Peskine72fcc982021-03-23 22:31:31 +01002962
2963 /* Make sure the last two bits are unset for Curve448, three bits for
2964 Curve25519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 0, 0));
2966 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 1, 0));
2967 if (high_bit == 254) {
2968 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 2, 0));
Gilles Peskine72fcc982021-03-23 22:31:31 +01002969 }
2970
2971cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 return ret;
Gilles Peskine72fcc982021-03-23 22:31:31 +01002973}
2974#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2975
Gilles Peskine60d8b982021-03-29 22:28:21 +02002976#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2977static int mbedtls_ecp_gen_privkey_sw(
2978 const mbedtls_mpi *N, mbedtls_mpi *d,
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine60d8b982021-03-29 22:28:21 +02002980{
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 int ret = mbedtls_mpi_random(d, 1, N, f_rng, p_rng);
2982 switch (ret) {
Gilles Peskine60d8b982021-03-29 22:28:21 +02002983 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Gilles Peskine60d8b982021-03-29 22:28:21 +02002985 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 return ret;
Gilles Peskine60d8b982021-03-29 22:28:21 +02002987 }
2988}
2989#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2990
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002991/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002992 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002993 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002994int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
2995 mbedtls_mpi *d,
2996 int (*f_rng)(void *, unsigned char *, size_t),
2997 void *p_rng)
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002998{
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02002999#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3001 return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng);
3002 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003003#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003004
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003005#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3007 return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng);
3008 }
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003009#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003012}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01003013
Valerio Settifd122f42023-04-05 18:15:32 +02003014#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003015/*
3016 * Generate a keypair with configurable base point
3017 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003018int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
3019 const mbedtls_ecp_point *G,
3020 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3021 int (*f_rng)(void *, unsigned char *, size_t),
3022 void *p_rng)
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003023{
Janos Follath24eed8d2019-11-22 13:21:35 +00003024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
3026 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, Q, d, G, f_rng, p_rng));
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02003027
3028cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 return ret;
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003030}
3031
3032/*
3033 * Generate key pair, wrapper for conventional base point
3034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
3036 mbedtls_mpi *d, mbedtls_ecp_point *Q,
3037 int (*f_rng)(void *, unsigned char *, size_t),
3038 void *p_rng)
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02003039{
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01003041}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01003042
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003043/*
3044 * Generate a keypair, prettier wrapper
3045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003046int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3047 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003048{
Janos Follath24eed8d2019-11-22 13:21:35 +00003049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3051 return ret;
3052 }
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng);
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003055}
Valerio Settifd122f42023-04-05 18:15:32 +02003056#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01003057
Gilles Peskine28240322023-06-21 19:52:11 +02003058int mbedtls_ecp_set_public_key(mbedtls_ecp_group_id grp_id,
3059 mbedtls_ecp_keypair *key,
3060 const mbedtls_ecp_point *Q)
3061{
3062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3063
3064 if (key->grp.id == MBEDTLS_ECP_DP_NONE) {
3065 /* Group not set yet */
3066 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3067 return ret;
3068 }
3069 } else if (key->grp.id != grp_id) {
3070 /* Group mismatch */
3071 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
3072 }
3073 return mbedtls_ecp_copy(&key->Q, Q);
3074}
3075
3076
Janos Follath77800962019-02-25 16:32:08 +00003077#define ECP_CURVE25519_KEY_SIZE 32
Archana1d2e2bb2021-06-07 06:13:16 +05303078#define ECP_CURVE448_KEY_SIZE 56
Janos Follath171a7ef2019-02-15 16:17:45 +00003079/*
3080 * Read a private key.
3081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003082int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3083 const unsigned char *buf, size_t buflen)
Janos Follath171a7ef2019-02-15 16:17:45 +00003084{
3085 int ret = 0;
3086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3088 return ret;
3089 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003090
Janos Follath28eb06d2019-02-26 10:53:34 +00003091 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3092
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003093#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Janos Follath28eb06d2019-02-26 10:53:34 +00003095 /*
Archana1d2e2bb2021-06-07 06:13:16 +05303096 * Mask the key as mandated by RFC7748 for Curve25519 and Curve448.
Janos Follath28eb06d2019-02-26 10:53:34 +00003097 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
3099 if (buflen != ECP_CURVE25519_KEY_SIZE) {
3100 return MBEDTLS_ERR_ECP_INVALID_KEY;
3101 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
Janos Follath171a7ef2019-02-15 16:17:45 +00003104
Janos Follath28eb06d2019-02-26 10:53:34 +00003105 /* Set the three least significant bits to 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
3107 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
3108 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0));
Janos Follath171a7ef2019-02-15 16:17:45 +00003109
Janos Follath28eb06d2019-02-26 10:53:34 +00003110 /* Set the most significant bit to 0 */
3111 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 mbedtls_mpi_set_bit(&key->d,
3113 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0)
3114 );
Janos Follath171a7ef2019-02-15 16:17:45 +00003115
Janos Follath28eb06d2019-02-26 10:53:34 +00003116 /* Set the second most significant bit to 1 */
3117 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 mbedtls_mpi_set_bit(&key->d,
3119 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1)
3120 );
3121 } else if (grp_id == MBEDTLS_ECP_DP_CURVE448) {
3122 if (buflen != ECP_CURVE448_KEY_SIZE) {
3123 return MBEDTLS_ERR_ECP_INVALID_KEY;
3124 }
Archana1d2e2bb2021-06-07 06:13:16 +05303125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
Archana1d2e2bb2021-06-07 06:13:16 +05303127
3128 /* Set the two least significant bits to 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
3130 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
Archana1d2e2bb2021-06-07 06:13:16 +05303131
3132 /* Set the most significant bit to 1 */
3133 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 mbedtls_mpi_set_bit(&key->d,
3135 ECP_CURVE448_KEY_SIZE * 8 - 1, 1)
3136 );
Archana1d2e2bb2021-06-07 06:13:16 +05303137 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003138 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003139#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003140#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3142 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen));
Janos Follath171a7ef2019-02-15 16:17:45 +00003143 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003144#endif
Manuel Pégourié-Gonnard745ec5d2023-10-17 10:13:45 +02003145
3146 if (ret == 0) {
3147 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d));
3148 }
Valerio Setti41b08182023-07-04 12:14:21 +02003149
Janos Follath171a7ef2019-02-15 16:17:45 +00003150cleanup:
3151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 if (ret != 0) {
3153 mbedtls_mpi_free(&key->d);
3154 }
Janos Follath171a7ef2019-02-15 16:17:45 +00003155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 return ret;
Janos Follath171a7ef2019-02-15 16:17:45 +00003157}
3158
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003159/*
Steven Cooremande8593f2020-06-09 19:55:26 +02003160 * Write a private key.
3161 */
Gilles Peskinec0f7a862024-02-19 16:50:39 +01003162#if !defined MBEDTLS_DEPRECATED_REMOVED
Gilles Peskine449bd832023-01-11 14:50:10 +01003163int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
3164 unsigned char *buf, size_t buflen)
Steven Cooremande8593f2020-06-09 19:55:26 +02003165{
Gilles Peskine5bb04e02024-02-19 13:24:41 +01003166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremande8593f2020-06-09 19:55:26 +02003167
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003168#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3170 if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) {
3171 if (buflen < ECP_CURVE25519_KEY_SIZE) {
3172 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3173 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 } else if (key->grp.id == MBEDTLS_ECP_DP_CURVE448) {
3176 if (buflen < ECP_CURVE448_KEY_SIZE) {
3177 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3178 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003179 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen));
Steven Cooremande8593f2020-06-09 19:55:26 +02003181 }
Steven Cooremande8593f2020-06-09 19:55:26 +02003182#endif
Gilles Peskinee8c04fe2018-09-14 17:44:21 +02003183#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3185 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen));
Steven Cooremande8593f2020-06-09 19:55:26 +02003186 }
3187
3188#endif
3189cleanup:
3190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 return ret;
Steven Cooremande8593f2020-06-09 19:55:26 +02003192}
Gilles Peskinec0f7a862024-02-19 16:50:39 +01003193#endif /* MBEDTLS_DEPRECATED_REMOVED */
Steven Cooremande8593f2020-06-09 19:55:26 +02003194
Gilles Peskineb395e742024-02-28 14:18:28 +01003195int mbedtls_ecp_write_key_ext(const mbedtls_ecp_keypair *key,
Gilles Peskinee3fb4cc2024-02-19 16:27:35 +01003196 size_t *olen, unsigned char *buf, size_t buflen)
3197{
3198 size_t len = (key->grp.nbits + 7) / 8;
3199 if (len > buflen) {
3200 /* For robustness, ensure *olen <= buflen even on error. */
3201 *olen = 0;
3202 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3203 }
3204 *olen = len;
3205
3206 /* Private key not set */
3207 if (key->d.n == 0) {
3208 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
3209 }
3210
3211#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3212 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3213 return mbedtls_mpi_write_binary_le(&key->d, buf, len);
3214 }
3215#endif
3216
3217#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3218 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3219 return mbedtls_mpi_write_binary(&key->d, buf, len);
3220 }
3221#endif
3222
3223 /* Private key set but no recognized curve type? This shouldn't happen. */
3224 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3225}
3226
Gilles Peskine62e33bc2023-06-22 22:27:32 +02003227/*
3228 * Write a public key.
3229 */
Gilles Peskine39b7bba2024-01-02 17:56:54 +01003230int mbedtls_ecp_write_public_key(const mbedtls_ecp_keypair *key,
Gilles Peskine62e33bc2023-06-22 22:27:32 +02003231 int format, size_t *olen,
3232 unsigned char *buf, size_t buflen)
3233{
3234 return mbedtls_ecp_point_write_binary(&key->grp, &key->Q,
3235 format, olen, buf, buflen);
3236}
3237
3238
Valerio Settifd122f42023-04-05 18:15:32 +02003239#if defined(MBEDTLS_ECP_C)
Steven Cooremande8593f2020-06-09 19:55:26 +02003240/*
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003241 * Check a public-private key pair
3242 */
Manuel Pégourié-Gonnardf8c24bf2021-06-15 11:29:26 +02003243int mbedtls_ecp_check_pub_priv(
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
3245 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003246{
Janos Follath24eed8d2019-11-22 13:21:35 +00003247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 mbedtls_ecp_point Q;
3249 mbedtls_ecp_group grp;
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003251 pub->grp.id != prv->grp.id ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
3253 mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
3254 mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
3255 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003256 }
3257
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 mbedtls_ecp_point_init(&Q);
3259 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 /* mbedtls_ecp_mul() needs a non-const group... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 mbedtls_ecp_group_copy(&grp, &prv->grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003263
3264 /* Also checks d is valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 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 +01003266
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
3268 mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
3269 mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003271 goto cleanup;
3272 }
3273
3274cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 mbedtls_ecp_point_free(&Q);
3276 mbedtls_ecp_group_free(&grp);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 return ret;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003279}
Gilles Peskine7ea72022023-06-21 20:39:08 +02003280
3281int mbedtls_ecp_keypair_calc_public(mbedtls_ecp_keypair *key,
3282 int (*f_rng)(void *, unsigned char *, size_t),
3283 void *p_rng)
3284{
3285 return mbedtls_ecp_mul(&key->grp, &key->Q, &key->d, &key->grp.G,
3286 f_rng, p_rng);
3287}
Valerio Settifd122f42023-04-05 18:15:32 +02003288#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01003289
Gilles Peskinee6886102023-06-21 18:22:06 +02003290mbedtls_ecp_group_id mbedtls_ecp_keypair_get_group_id(
3291 const mbedtls_ecp_keypair *key)
3292{
3293 return key->grp.id;
3294}
3295
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003296/*
3297 * Export generic key-pair parameters.
3298 */
3299int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
3300 mbedtls_mpi *d, mbedtls_ecp_point *Q)
3301{
3302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003303
Gilles Peskineba5b5d62023-06-21 18:24:04 +02003304 if (grp != NULL && (ret = mbedtls_ecp_group_copy(grp, &key->grp)) != 0) {
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003305 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 }
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003307
Gilles Peskineba5b5d62023-06-21 18:24:04 +02003308 if (d != NULL && (ret = mbedtls_mpi_copy(d, &key->d)) != 0) {
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003309 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 }
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003311
Gilles Peskineba5b5d62023-06-21 18:24:04 +02003312 if (Q != NULL && (ret = mbedtls_ecp_copy(Q, &key->Q)) != 0) {
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003313 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 }
Przemek Stekiel711d0f52022-03-18 13:52:26 +01003315
3316 return 0;
3317}
3318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003320
Valerio Settifd122f42023-04-05 18:15:32 +02003321#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003322/*
3323 * PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!!
3324 *
3325 * This is the linear congruential generator from numerical recipes,
3326 * except we only use the low byte as the output. See
3327 * https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
3328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003329static int self_test_rng(void *ctx, unsigned char *out, size_t len)
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003330{
3331 static uint32_t state = 42;
3332
3333 (void) ctx;
3334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 for (size_t i = 0; i < len; i++) {
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003336 state = state * 1664525u + 1013904223u;
3337 out[i] = (unsigned char) state;
3338 }
3339
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 return 0;
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +02003341}
3342
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003343/* Adjust the exponent to be a valid private point for the specified curve.
3344 * This is sometimes necessary because we use a single set of exponents
3345 * for all curves but the validity of values depends on the curve. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346static int self_test_adjust_exponent(const mbedtls_ecp_group *grp,
3347 mbedtls_mpi *m)
Gilles Peskinea088c812018-09-17 18:31:15 +02003348{
3349 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 switch (grp->id) {
3351 /* If Curve25519 is available, then that's what we use for the
3352 * Montgomery test, so we don't need the adjustment code. */
3353#if !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Gilles Peskinea088c812018-09-17 18:31:15 +02003354#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3355 case MBEDTLS_ECP_DP_CURVE448:
3356 /* Move highest bit from 254 to N-1. Setting bit N-1 is
3357 * necessary to enforce the highest-bit-set constraint. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0));
3359 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1));
Gilles Peskinea088c812018-09-17 18:31:15 +02003360 /* Copy second-highest bit from 253 to N-2. This is not
3361 * necessary but improves the test variety a bit. */
3362 MBEDTLS_MPI_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 mbedtls_mpi_set_bit(m, grp->nbits - 1,
3364 mbedtls_mpi_get_bit(m, 253)));
Gilles Peskinea088c812018-09-17 18:31:15 +02003365 break;
3366#endif
3367#endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */
3368 default:
3369 /* Non-Montgomery curves and Curve25519 need no adjustment. */
3370 (void) grp;
3371 (void) m;
3372 goto cleanup;
3373 }
3374cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 return ret;
Gilles Peskinea088c812018-09-17 18:31:15 +02003376}
3377
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003378/* Calculate R = m.P for each m in exponents. Check that the number of
3379 * basic operations doesn't depend on the value of m. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003380static int self_test_point(int verbose,
3381 mbedtls_ecp_group *grp,
3382 mbedtls_ecp_point *R,
3383 mbedtls_mpi *m,
3384 const mbedtls_ecp_point *P,
3385 const char *const *exponents,
3386 size_t n_exponents)
Gilles Peskinec95696f2018-09-17 15:59:01 +02003387{
3388 int ret = 0;
Gilles Peskine24666792018-09-17 18:29:49 +02003389 size_t i = 0;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003390 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
3391 add_count = 0;
3392 dbl_count = 0;
3393 mul_count = 0;
Gilles Peskinea088c812018-09-17 18:31:15 +02003394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0]));
3396 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3397 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 for (i = 1; i < n_exponents; i++) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003400 add_c_prev = add_count;
3401 dbl_c_prev = dbl_count;
3402 mul_c_prev = mul_count;
3403 add_count = 0;
3404 dbl_count = 0;
3405 mul_count = 0;
3406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i]));
3408 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3409 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
Gilles Peskinec95696f2018-09-17 15:59:01 +02003410
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 if (add_count != add_c_prev ||
Gilles Peskinec95696f2018-09-17 15:59:01 +02003412 dbl_count != dbl_c_prev ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 mul_count != mul_c_prev) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003414 ret = 1;
3415 break;
3416 }
3417 }
3418
3419cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 if (verbose != 0) {
3421 if (ret != 0) {
3422 mbedtls_printf("failed (%u)\n", (unsigned int) i);
3423 } else {
3424 mbedtls_printf("passed\n");
3425 }
Gilles Peskinec95696f2018-09-17 15:59:01 +02003426 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 return ret;
Gilles Peskinec95696f2018-09-17 15:59:01 +02003428}
Valerio Settifd122f42023-04-05 18:15:32 +02003429#endif /* MBEDTLS_ECP_C */
Gilles Peskinec95696f2018-09-17 15:59:01 +02003430
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01003431/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003432 * Checkup routine
3433 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003434int mbedtls_ecp_self_test(int verbose)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003435{
Valerio Settifd122f42023-04-05 18:15:32 +02003436#if defined(MBEDTLS_ECP_C)
Janos Follath24eed8d2019-11-22 13:21:35 +00003437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 mbedtls_ecp_group grp;
3439 mbedtls_ecp_point R, P;
3440 mbedtls_mpi m;
Gilles Peskine24666792018-09-17 18:29:49 +02003441
3442#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskined9767a52018-09-14 19:29:47 +02003443 /* Exponents especially adapted for secp192k1, which has the lowest
3444 * order n of all supported curves (secp192r1 is in a slightly larger
3445 * field but the order of its base point is slightly smaller). */
Gilles Peskine24666792018-09-17 18:29:49 +02003446 const char *sw_exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003447 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003448 "000000000000000000000000000000000000000000000001", /* one */
Gilles Peskined9767a52018-09-14 19:29:47 +02003449 "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01003450 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01003451 "400000000000000000000000000000000000000000000000", /* one and zeros */
3452 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3453 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003454 };
Gilles Peskine24666792018-09-17 18:29:49 +02003455#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3456#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3457 const char *m_exponents[] =
3458 {
Gilles Peskine6d9c8d72020-07-22 01:26:25 +02003459 /* Valid private values for Curve25519. In a build with Curve448
3460 * but not Curve25519, they will be adjusted in
3461 * self_test_adjust_exponent(). */
Gilles Peskine24666792018-09-17 18:29:49 +02003462 "4000000000000000000000000000000000000000000000000000000000000000",
3463 "5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
3464 "5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
3465 "41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
3466 "5555555555555555555555555555555555555555555555555555555555555550",
3467 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
3468 };
3469#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003470
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 mbedtls_ecp_group_init(&grp);
3472 mbedtls_ecp_point_init(&R);
3473 mbedtls_ecp_point_init(&P);
3474 mbedtls_mpi_init(&m);
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003475
Gilles Peskine24666792018-09-17 18:29:49 +02003476#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003477 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP192R1));
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02003480#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id));
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02003482#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003483
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 if (verbose != 0) {
3485 mbedtls_printf(" ECP SW test #1 (constant op_count, base point G): ");
3486 }
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003487 /* Do a dummy multiplication first to trigger precomputation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003488 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2));
3489 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, self_test_rng, NULL));
3490 ret = self_test_point(verbose,
3491 &grp, &R, &m, &grp.G,
3492 sw_exponents,
3493 sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3494 if (ret != 0) {
Gilles Peskinec95696f2018-09-17 15:59:01 +02003495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 if (verbose != 0) {
3499 mbedtls_printf(" ECP SW test #2 (constant op_count, other point): ");
3500 }
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003501 /* We computed P = 2G last time, use it */
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 ret = self_test_point(verbose,
3503 &grp, &R, &m, &P,
3504 sw_exponents,
3505 sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3506 if (ret != 0) {
Gilles Peskine24666792018-09-17 18:29:49 +02003507 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 }
Gilles Peskine24666792018-09-17 18:29:49 +02003509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 mbedtls_ecp_group_free(&grp);
3511 mbedtls_ecp_point_free(&R);
Gilles Peskine24666792018-09-17 18:29:49 +02003512#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3513
3514#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 if (verbose != 0) {
3516 mbedtls_printf(" ECP Montgomery test (constant op_count): ");
3517 }
Gilles Peskine24666792018-09-17 18:29:49 +02003518#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE25519));
Gilles Peskine24666792018-09-17 18:29:49 +02003520#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE448));
Gilles Peskine24666792018-09-17 18:29:49 +02003522#else
3523#error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
3524#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 ret = self_test_point(verbose,
3526 &grp, &R, &m, &grp.G,
3527 m_exponents,
3528 sizeof(m_exponents) / sizeof(m_exponents[0]));
3529 if (ret != 0) {
Gilles Peskine24666792018-09-17 18:29:49 +02003530 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 }
Gilles Peskine24666792018-09-17 18:29:49 +02003532#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02003533
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003534cleanup:
3535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 if (ret < 0 && verbose != 0) {
3537 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
3538 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 mbedtls_ecp_group_free(&grp);
3541 mbedtls_ecp_point_free(&R);
3542 mbedtls_ecp_point_free(&P);
3543 mbedtls_mpi_free(&m);
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003544
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 if (verbose != 0) {
3546 mbedtls_printf("\n");
3547 }
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01003548
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 return ret;
Valerio Settifd122f42023-04-05 18:15:32 +02003550#else /* MBEDTLS_ECP_C */
3551 (void) verbose;
3552 return 0;
3553#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003554}
3555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003557
Valerio Settifd122f42023-04-05 18:15:32 +02003558#endif /* MBEDTLS_ECP_LIGHT */