blob: 6e55f2205fb0c19517f118cc350e6470b294606c [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/*
2 * Elliptic curve DSA
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010018 */
19
20/*
21 * References:
22 *
Xiaokang Qianb92a2f62023-04-19 02:59:15 +000023 * SEC1 https://www.secg.org/sec1-v2.pdf
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/ecdsa.h"
31#include "mbedtls/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010037#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010038
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020039#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020040
Hanno Becker319ae112018-12-14 16:43:29 +000041#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000042#include "mbedtls/error.h"
Hanno Becker319ae112018-12-14 16:43:29 +000043
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020044#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020045
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020046/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +020047 * Sub-context for ecdsa_verify()
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020048 */
Gilles Peskine449bd832023-01-11 14:50:10 +010049struct mbedtls_ecdsa_restart_ver {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020050 mbedtls_mpi u1, u2; /* intermediate values */
51 enum { /* what to do next? */
52 ecdsa_ver_init = 0, /* getting started */
53 ecdsa_ver_muladd, /* muladd step */
54 } state;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020055};
56
57/*
58 * Init verify restart sub-context
59 */
Gilles Peskine449bd832023-01-11 14:50:10 +010060static void ecdsa_restart_ver_init(mbedtls_ecdsa_restart_ver_ctx *ctx)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020061{
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_mpi_init(&ctx->u1);
63 mbedtls_mpi_init(&ctx->u2);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +020064 ctx->state = ecdsa_ver_init;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020065}
66
67/*
68 * Free the components of a verify restart sub-context
69 */
Gilles Peskine449bd832023-01-11 14:50:10 +010070static void ecdsa_restart_ver_free(mbedtls_ecdsa_restart_ver_ctx *ctx)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020071{
Gilles Peskine449bd832023-01-11 14:50:10 +010072 if (ctx == NULL) {
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020073 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_mpi_free(&ctx->u1);
77 mbedtls_mpi_free(&ctx->u2);
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 ecdsa_restart_ver_init(ctx);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020080}
81
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020082/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +020083 * Sub-context for ecdsa_sign()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020084 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085struct mbedtls_ecdsa_restart_sig {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +020086 int sign_tries;
87 int key_tries;
88 mbedtls_mpi k; /* per-signature random */
89 mbedtls_mpi r; /* r value */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020090 enum { /* what to do next? */
91 ecdsa_sig_init = 0, /* getting started */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +020092 ecdsa_sig_mul, /* doing ecp_mul() */
93 ecdsa_sig_modn, /* mod N computations */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020094 } state;
95};
96
97/*
98 * Init verify sign sub-context
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static void ecdsa_restart_sig_init(mbedtls_ecdsa_restart_sig_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200101{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200102 ctx->sign_tries = 0;
103 ctx->key_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_mpi_init(&ctx->k);
105 mbedtls_mpi_init(&ctx->r);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200106 ctx->state = ecdsa_sig_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200107}
108
109/*
110 * Free the components of a sign restart sub-context
111 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112static void ecdsa_restart_sig_free(mbedtls_ecdsa_restart_sig_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200113{
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (ctx == NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200115 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_mpi_free(&ctx->k);
119 mbedtls_mpi_free(&ctx->r);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200120}
121
122#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
123/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +0200124 * Sub-context for ecdsa_sign_det()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126struct mbedtls_ecdsa_restart_det {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200127 mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200128 enum { /* what to do next? */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200129 ecdsa_det_init = 0, /* getting started */
130 ecdsa_det_sign, /* make signature */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200131 } state;
132};
133
134/*
135 * Init verify sign_det sub-context
136 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137static void ecdsa_restart_det_init(mbedtls_ecdsa_restart_det_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200138{
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 mbedtls_hmac_drbg_init(&ctx->rng_ctx);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200140 ctx->state = ecdsa_det_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200141}
142
143/*
144 * Free the components of a sign_det restart sub-context
145 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146static void ecdsa_restart_det_free(mbedtls_ecdsa_restart_det_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200147{
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ctx == NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200149 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_hmac_drbg_free(&ctx->rng_ctx);
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 ecdsa_restart_det_init(ctx);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200155}
156#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158#define ECDSA_RS_ECP (rs_ctx == NULL ? NULL : &rs_ctx->ecp)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200159
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200160/* Utility macro for checking and updating ops budget */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161#define ECDSA_BUDGET(ops) \
162 MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, ECDSA_RS_ECP, ops));
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200163
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200164/* Call this when entering a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165#define ECDSA_RS_ENTER(SUB) do { \
166 /* reset ops count for this call if top-level */ \
167 if (rs_ctx != NULL && rs_ctx->ecp.depth++ == 0) \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200168 rs_ctx->ecp.ops_done = 0; \
169 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 /* set up our own sub-context if needed */ \
171 if (mbedtls_ecp_restart_is_enabled() && \
172 rs_ctx != NULL && rs_ctx->SUB == NULL) \
173 { \
174 rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
175 if (rs_ctx->SUB == NULL) \
176 return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
177 \
178 ecdsa_restart_## SUB ##_init(rs_ctx->SUB); \
179 } \
180} while (0)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200181
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200182/* Call this when leaving a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183#define ECDSA_RS_LEAVE(SUB) do { \
184 /* clear our sub-context when not in progress (done or error) */ \
185 if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
186 ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
187 { \
188 ecdsa_restart_## SUB ##_free(rs_ctx->SUB); \
189 mbedtls_free(rs_ctx->SUB); \
190 rs_ctx->SUB = NULL; \
191 } \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200192 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (rs_ctx != NULL) \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200194 rs_ctx->ecp.depth--; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100195} while (0)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200196
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200197#else /* MBEDTLS_ECP_RESTARTABLE */
198
199#define ECDSA_RS_ECP NULL
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201#define ECDSA_BUDGET(ops) /* no-op; for compatibility */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203#define ECDSA_RS_ENTER(SUB) (void) rs_ctx
204#define ECDSA_RS_LEAVE(SUB) (void) rs_ctx
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200205
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200206#endif /* MBEDTLS_ECP_RESTARTABLE */
207
Steven Cooremanfa6641b2021-01-11 17:11:39 +0100208#if defined(MBEDTLS_ECDSA_DETERMINISTIC) || \
Steven Cooreman107409f2021-01-26 12:01:22 +0100209 !defined(MBEDTLS_ECDSA_SIGN_ALT) || \
210 !defined(MBEDTLS_ECDSA_VERIFY_ALT)
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100211/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100212 * Derive a suitable integer for group grp from a buffer of length len
213 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215static int derive_mpi(const mbedtls_ecp_group *grp, mbedtls_mpi *x,
216 const unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100217{
Janos Follath24eed8d2019-11-22 13:21:35 +0000218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 size_t n_size = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100220 size_t use_size = blen > n_size ? n_size : blen;
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(x, buf, use_size));
223 if (use_size * 8 > grp->nbits) {
224 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(x, use_size * 8 - grp->nbits));
225 }
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100226
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100227 /* While at it, reduce modulo N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (mbedtls_mpi_cmp_mpi(x, &grp->N) >= 0) {
229 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(x, x, &grp->N));
230 }
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100231
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100232cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100234}
Steven Cooremanfa6641b2021-01-11 17:11:39 +0100235#endif /* ECDSA_DETERMINISTIC || !ECDSA_SIGN_ALT || !ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100236
JonathanWitthoeft405ec942023-04-26 10:24:12 -0500237int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid)
238{
239 switch (gid) {
240#ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
241 case MBEDTLS_ECP_DP_CURVE25519: return 0;
242#endif
243#ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
244 case MBEDTLS_ECP_DP_CURVE448: return 0;
245#endif
246 default: return 1;
247 }
248}
249
Ron Eldor936d2842018-11-01 13:05:52 +0200250#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100251/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100252 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
253 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
254 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000255int mbedtls_ecdsa_sign_restartable(mbedtls_ecp_group *grp,
256 mbedtls_mpi *r, mbedtls_mpi *s,
257 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
258 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
259 int (*f_rng_blind)(void *, unsigned char *, size_t),
260 void *p_rng_blind,
261 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100262{
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200263 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200264 int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_ecp_point R;
266 mbedtls_mpi k, e, t;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200267 mbedtls_mpi *pk = &k, *pr = r;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100268
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100269 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
271 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
272 }
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100273
Darryl Greenc64a48b2017-11-17 17:09:17 +0000274 /* Make sure d is in range 1..n-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
276 return MBEDTLS_ERR_ECP_INVALID_KEY;
277 }
Darryl Greenc64a48b2017-11-17 17:09:17 +0000278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 mbedtls_ecp_point_init(&R);
280 mbedtls_mpi_init(&k); mbedtls_mpi_init(&e); mbedtls_mpi_init(&t);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 ECDSA_RS_ENTER(sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200283
284#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200286 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200287 p_sign_tries = &rs_ctx->sig->sign_tries;
288 p_key_tries = &rs_ctx->sig->key_tries;
289 pk = &rs_ctx->sig->k;
290 pr = &rs_ctx->sig->r;
291
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200292 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (rs_ctx->sig->state == ecdsa_sig_mul) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200294 goto mul;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 }
296 if (rs_ctx->sig->state == ecdsa_sig_modn) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200297 goto modn;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200299 }
300#endif /* MBEDTLS_ECP_RESTARTABLE */
301
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200302 *p_sign_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 do {
304 if ((*p_sign_tries)++ > 10) {
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200305 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
306 goto cleanup;
307 }
308
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100309 /*
310 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100311 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100312 */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200313 *p_key_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 do {
315 if ((*p_key_tries)++ > 10) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200317 goto cleanup;
318 }
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, pk, f_rng, p_rng));
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200321
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200322#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200324 rs_ctx->sig->state = ecdsa_sig_mul;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200326
327mul:
328#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &R, pk, &grp->G,
330 f_rng_blind,
331 p_rng_blind,
332 ECDSA_RS_ECP));
333 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pr, &R.X, &grp->N));
334 } while (mbedtls_mpi_cmp_int(pr, 0) == 0);
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200335
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200336#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200338 rs_ctx->sig->state = ecdsa_sig_modn;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200340
341modn:
342#endif
343 /*
344 * Accounting for everything up to the end of the loop
345 * (step 6, but checking now avoids saving e and t)
346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 ECDSA_BUDGET(MBEDTLS_ECP_OPS_INV + 4);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100348
349 /*
350 * Step 5: derive MPI from hashed message
351 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100353
354 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200355 * Generate a random value to blind inv_mod in next step,
356 * avoiding a potential timing leak.
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, &t, f_rng_blind,
359 p_rng_blind));
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200360
361 /*
362 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, d));
365 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, s));
366 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&e, &e, &t));
367 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pk, pk, &t));
368 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pk, pk, &grp->N));
369 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(s, pk, &grp->N));
370 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e));
371 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N));
372 } while (mbedtls_mpi_cmp_int(s, 0) == 0);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100373
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200374#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Chien Wonge2caf412023-08-01 21:38:46 +0800376 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(r, pr));
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200378#endif
379
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100380cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_ecp_point_free(&R);
382 mbedtls_mpi_free(&k); mbedtls_mpi_free(&e); mbedtls_mpi_free(&t);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 ECDSA_RS_LEAVE(sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return ret;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100387}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100388
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200389/*
390 * Compute ECDSA signature of a hashed message
391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
393 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
394 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200395{
Janos Follathdca667a2019-01-04 14:32:30 +0000396 /* Use the same RNG for both blinding and ephemeral key generation */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000397 return mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
398 f_rng, p_rng, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200399}
Ron Eldor936d2842018-11-01 13:05:52 +0200400#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100403/*
404 * Deterministic signature wrapper
TRodziewicz18efb732021-04-29 23:12:19 +0200405 *
TRodziewicz7e9422d2021-04-30 10:32:58 +0200406 * note: The f_rng_blind parameter must not be NULL.
TRodziewicz18efb732021-04-29 23:12:19 +0200407 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100408 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000409int mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
410 mbedtls_mpi *r, mbedtls_mpi *s,
411 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
412 mbedtls_md_type_t md_alg,
413 int (*f_rng_blind)(void *, unsigned char *, size_t),
414 void *p_rng_blind,
415 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100416{
Janos Follath24eed8d2019-11-22 13:21:35 +0000417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200419 mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 size_t grp_len = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 const mbedtls_md_info_t *md_info;
423 mbedtls_mpi h;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) {
426 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
427 }
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_mpi_init(&h);
430 mbedtls_hmac_drbg_init(&rng_ctx);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 ECDSA_RS_ENTER(det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200433
434#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (rs_ctx != NULL && rs_ctx->det != NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200436 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200437 p_rng = &rs_ctx->det->rng_ctx;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200438
439 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (rs_ctx->det->state == ecdsa_det_sign) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200441 goto sign;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200443 }
444#endif /* MBEDTLS_ECP_RESTARTABLE */
445
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100446 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(d, data, grp_len));
448 MBEDTLS_MPI_CHK(derive_mpi(grp, &h, buf, blen));
449 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&h, data + grp_len, grp_len));
Chien Wonge2caf412023-08-01 21:38:46 +0800450 MBEDTLS_MPI_CHK(mbedtls_hmac_drbg_seed_buf(p_rng, md_info, data, 2 * grp_len));
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100451
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200452#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (rs_ctx != NULL && rs_ctx->det != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200454 rs_ctx->det->state = ecdsa_det_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200456
457sign:
458#endif
Ron Eldor8493f802018-11-01 11:32:15 +0200459#if defined(MBEDTLS_ECDSA_SIGN_ALT)
Steven Cooreman6dce4bb2021-02-10 17:07:20 +0100460 (void) f_rng_blind;
461 (void) p_rng_blind;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 ret = mbedtls_ecdsa_sign(grp, r, s, d, buf, blen,
463 mbedtls_hmac_drbg_random, p_rng);
Ron Eldor8493f802018-11-01 11:32:15 +0200464#else
Paul Elliott2ba002c2022-12-09 18:59:26 +0000465 ret = mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
466 mbedtls_hmac_drbg_random, p_rng,
467 f_rng_blind, p_rng_blind, rs_ctx);
Ron Eldor936d2842018-11-01 13:05:52 +0200468#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100469
470cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 mbedtls_hmac_drbg_free(&rng_ctx);
472 mbedtls_mpi_free(&h);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 ECDSA_RS_LEAVE(det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return ret;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100477}
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200478
479/*
TRodziewicz18efb732021-04-29 23:12:19 +0200480 * Deterministic signature wrapper
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
483 mbedtls_mpi *s, const mbedtls_mpi *d,
484 const unsigned char *buf, size_t blen,
485 mbedtls_md_type_t md_alg,
486 int (*f_rng_blind)(void *, unsigned char *,
487 size_t),
488 void *p_rng_blind)
Janos Follathdca667a2019-01-04 14:32:30 +0000489{
Paul Elliott2ba002c2022-12-09 18:59:26 +0000490 return mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg,
491 f_rng_blind, p_rng_blind, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100494
Ron Eldor936d2842018-11-01 13:05:52 +0200495#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100496/*
497 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
498 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
499 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000500int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
501 const unsigned char *buf, size_t blen,
502 const mbedtls_ecp_point *Q,
503 const mbedtls_mpi *r,
504 const mbedtls_mpi *s,
505 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100506{
Janos Follath24eed8d2019-11-22 13:21:35 +0000507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_mpi e, s_inv, u1, u2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200509 mbedtls_ecp_point R;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200510 mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 mbedtls_ecp_point_init(&R);
513 mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv);
514 mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100515
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100516 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
518 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
519 }
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 ECDSA_RS_ENTER(ver);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200522
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200523#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (rs_ctx != NULL && rs_ctx->ver != NULL) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200525 /* redirect to our context */
526 pu1 = &rs_ctx->ver->u1;
527 pu2 = &rs_ctx->ver->u2;
528
529 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (rs_ctx->ver->state == ecdsa_ver_muladd) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200531 goto muladd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 }
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200533 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200534#endif /* MBEDTLS_ECP_RESTARTABLE */
535
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100536 /*
537 * Step 1: make sure r and s are in range 1..n-1
538 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 ||
540 mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200542 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100543 }
544
545 /*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100546 * Step 3: derive MPI from hashed message
547 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100549
550 /*
551 * Step 4: u1 = e / s mod n, u2 = r / s mod n
552 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 ECDSA_BUDGET(MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2);
Manuel Pégourié-Gonnardbfa19722017-08-23 17:39:18 +0200554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&s_inv, s, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu1, &e, &s_inv));
558 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu1, pu1, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu2, r, &s_inv));
561 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu2, pu2, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100562
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200563#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (rs_ctx != NULL && rs_ctx->ver != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200565 rs_ctx->ver->state = ecdsa_ver_muladd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 }
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200567
568muladd:
569#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100570 /*
571 * Step 5: R = u1 G + u2 Q
572 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_MPI_CHK(mbedtls_ecp_muladd_restartable(grp,
574 &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (mbedtls_ecp_is_zero(&R)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200578 goto cleanup;
579 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100580
581 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100582 * Step 6: convert xR to an integer (no-op)
583 * Step 7: reduce xR mod n (gives v)
584 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&R.X, &R.X, &grp->N));
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100586
587 /*
588 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100589 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (mbedtls_mpi_cmp_mpi(&R.X, r) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200592 goto cleanup;
593 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100594
595cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 mbedtls_ecp_point_free(&R);
597 mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv);
598 mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 ECDSA_RS_LEAVE(ver);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100603}
604
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200605/*
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200606 * Verify ECDSA signature of hashed message
607 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
609 const unsigned char *buf, size_t blen,
610 const mbedtls_ecp_point *Q,
611 const mbedtls_mpi *r,
612 const mbedtls_mpi *s)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200613{
Paul Elliott2ba002c2022-12-09 18:59:26 +0000614 return mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200615}
Ron Eldor936d2842018-11-01 13:05:52 +0200616#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200617
618/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100619 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200620 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s,
622 unsigned char *sig, size_t sig_size,
623 size_t *slen)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200624{
Janos Follath24eed8d2019-11-22 13:21:35 +0000625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 };
627 unsigned char *p = buf + sizeof(buf);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200628 size_t len = 0;
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, s));
631 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, r));
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
634 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
635 MBEDTLS_ASN1_CONSTRUCTED |
636 MBEDTLS_ASN1_SEQUENCE));
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (len > sig_size) {
639 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
640 }
Gilles Peskinef00f1522021-06-22 00:09:00 +0200641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 memcpy(sig, p, len);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200643 *slen = len;
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 return 0;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200646}
647
648/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100649 * Compute and write signature
650 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
652 mbedtls_md_type_t md_alg,
653 const unsigned char *hash, size_t hlen,
654 unsigned char *sig, size_t sig_size, size_t *slen,
655 int (*f_rng)(void *, unsigned char *, size_t),
656 void *p_rng,
657 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100658{
Janos Follath24eed8d2019-11-22 13:21:35 +0000659 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_mpi r, s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (f_rng == NULL) {
662 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
663 }
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 mbedtls_mpi_init(&r);
666 mbedtls_mpi_init(&s);
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Paul Elliott2ba002c2022-12-09 18:59:26 +0000669 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d,
670 hash, hlen, md_alg, f_rng,
671 p_rng, rs_ctx));
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200672#else
673 (void) md_alg;
674
Ron Eldor8493f802018-11-01 11:32:15 +0200675#if defined(MBEDTLS_ECDSA_SIGN_ALT)
Steven Cooremanfa6641b2021-01-11 17:11:39 +0100676 (void) rs_ctx;
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ctx->grp, &r, &s, &ctx->d,
679 hash, hlen, f_rng, p_rng));
Ron Eldor8493f802018-11-01 11:32:15 +0200680#else
Janos Follathdca667a2019-01-04 14:32:30 +0000681 /* Use the same RNG for both blinding and ephemeral key generation */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000682 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d,
683 hash, hlen, f_rng, p_rng, f_rng,
684 p_rng, rs_ctx));
Ron Eldor936d2842018-11-01 13:05:52 +0200685#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Ron Eldor5ed8c1e2018-11-05 14:04:26 +0200686#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 MBEDTLS_MPI_CHK(ecdsa_signature_to_asn1(&r, &s, sig, sig_size, slen));
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200689
690cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 mbedtls_mpi_free(&r);
692 mbedtls_mpi_free(&s);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return ret;
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100695}
696
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200697/*
698 * Compute and write signature
699 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
701 mbedtls_md_type_t md_alg,
702 const unsigned char *hash, size_t hlen,
703 unsigned char *sig, size_t sig_size, size_t *slen,
704 int (*f_rng)(void *, unsigned char *, size_t),
705 void *p_rng)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200706{
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return mbedtls_ecdsa_write_signature_restartable(
708 ctx, md_alg, hash, hlen, sig, sig_size, slen,
709 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200710}
711
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100712/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200713 * Read and check signature
714 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
716 const unsigned char *hash, size_t hlen,
717 const unsigned char *sig, size_t slen)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200718{
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return mbedtls_ecdsa_read_signature_restartable(
720 ctx, hash, hlen, sig, slen, NULL);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200721}
722
723/*
724 * Restartable read and check signature
725 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
727 const unsigned char *hash, size_t hlen,
728 const unsigned char *sig, size_t slen,
729 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200730{
Janos Follath24eed8d2019-11-22 13:21:35 +0000731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200732 unsigned char *p = (unsigned char *) sig;
733 const unsigned char *end = sig + slen;
734 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_mpi r, s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_mpi_init(&r);
737 mbedtls_mpi_init(&s);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
740 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200742 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200743 }
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (p + len != end) {
746 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
747 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200748 goto cleanup;
749 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 ||
752 (ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200754 goto cleanup;
755 }
Ron Eldor8493f802018-11-01 11:32:15 +0200756#if defined(MBEDTLS_ECDSA_VERIFY_ALT)
Steven Cooremanfa6641b2021-01-11 17:11:39 +0100757 (void) rs_ctx;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if ((ret = mbedtls_ecdsa_verify(&ctx->grp, hash, hlen,
760 &ctx->Q, &r, &s)) != 0) {
Ron Eldor8493f802018-11-01 11:32:15 +0200761 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 }
Ron Eldor8493f802018-11-01 11:32:15 +0200763#else
Paul Elliott2ba002c2022-12-09 18:59:26 +0000764 if ((ret = mbedtls_ecdsa_verify_restartable(&ctx->grp, hash, hlen,
765 &ctx->Q, &r, &s, rs_ctx)) != 0) {
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200766 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 }
Ron Eldor936d2842018-11-01 13:05:52 +0200768#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200769
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200770 /* At this point we know that the buffer starts with a valid signature.
771 * Return 0 if the buffer just contains the signature, and a specific
772 * error code if the valid signature is followed by more data. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (p != end) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 }
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200776
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200777cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 mbedtls_mpi_free(&r);
779 mbedtls_mpi_free(&s);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return ret;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200782}
783
Ron Eldor314adb62017-10-10 18:28:25 +0300784#if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200785/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200786 * Generate key pair
787 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
789 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200790{
Ron Eldoradb52342018-12-17 10:06:12 +0200791 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 ret = mbedtls_ecp_group_load(&ctx->grp, gid);
793 if (ret != 0) {
794 return ret;
795 }
Ron Eldoradb52342018-12-17 10:06:12 +0200796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 return mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d,
798 &ctx->Q, f_rng, p_rng);
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200799}
Ron Eldor936d2842018-11-01 13:05:52 +0200800#endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200801
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200802/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 * Set context from an mbedtls_ecp_keypair
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200804 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200806{
Janos Follath24eed8d2019-11-22 13:21:35 +0000807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 ||
809 (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
810 (ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
811 mbedtls_ecdsa_free(ctx);
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100812 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200815}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200816
817/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200818 * Initialize context
819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200821{
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 mbedtls_ecp_keypair_init(ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200823}
824
825/*
826 * Free context
827 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200829{
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (ctx == NULL) {
Hanno Becker319ae112018-12-14 16:43:29 +0000831 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 }
Hanno Becker319ae112018-12-14 16:43:29 +0000833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_ecp_keypair_free(ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200835}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100836
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200837#if defined(MBEDTLS_ECP_RESTARTABLE)
838/*
839 * Initialize a restart context
840 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200842{
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 mbedtls_ecp_restart_init(&ctx->ecp);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200844
845 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200846 ctx->sig = NULL;
847#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
848 ctx->det = NULL;
849#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200850}
851
852/*
853 * Free the components of a restart context
854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200856{
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (ctx == NULL) {
Hanno Becker319ae112018-12-14 16:43:29 +0000858 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 }
Hanno Becker319ae112018-12-14 16:43:29 +0000860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 mbedtls_ecp_restart_free(&ctx->ecp);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 ecdsa_restart_ver_free(ctx->ver);
864 mbedtls_free(ctx->ver);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200865 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 ecdsa_restart_sig_free(ctx->sig);
868 mbedtls_free(ctx->sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200869 ctx->sig = NULL;
870
871#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 ecdsa_restart_det_free(ctx->det);
873 mbedtls_free(ctx->det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200874 ctx->det = NULL;
875#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200876}
877#endif /* MBEDTLS_ECP_RESTARTABLE */
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_ECDSA_C */