blob: 618c7b05983d95eec045c22e4db86d9b6dccdcbf [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 *
23 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
24 */
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 -0500237#if !defined(MBEDTLS_ECDSA_SIGN_ALT) || \
238 !defined(MBEDTLS_ECDSA_VERIFY_ALT)
239
240int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid)
241{
242 switch (gid) {
243#ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
244 case MBEDTLS_ECP_DP_CURVE25519: return 0;
245#endif
246#ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
247 case MBEDTLS_ECP_DP_CURVE448: return 0;
248#endif
249 default: return 1;
250 }
251}
252
253#endif /* !ECDSA_SIGN_ALT || !ECDSA_VERIFY_ALT */
254
Ron Eldor936d2842018-11-01 13:05:52 +0200255#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100256/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100257 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
258 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
259 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000260int mbedtls_ecdsa_sign_restartable(mbedtls_ecp_group *grp,
261 mbedtls_mpi *r, mbedtls_mpi *s,
262 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
263 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
264 int (*f_rng_blind)(void *, unsigned char *, size_t),
265 void *p_rng_blind,
266 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100267{
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200268 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200269 int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_ecp_point R;
271 mbedtls_mpi k, e, t;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200272 mbedtls_mpi *pk = &k, *pr = r;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100273
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100274 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
276 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
277 }
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100278
Darryl Greenc64a48b2017-11-17 17:09:17 +0000279 /* Make sure d is in range 1..n-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
281 return MBEDTLS_ERR_ECP_INVALID_KEY;
282 }
Darryl Greenc64a48b2017-11-17 17:09:17 +0000283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_ecp_point_init(&R);
285 mbedtls_mpi_init(&k); mbedtls_mpi_init(&e); mbedtls_mpi_init(&t);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 ECDSA_RS_ENTER(sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200288
289#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200291 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200292 p_sign_tries = &rs_ctx->sig->sign_tries;
293 p_key_tries = &rs_ctx->sig->key_tries;
294 pk = &rs_ctx->sig->k;
295 pr = &rs_ctx->sig->r;
296
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200297 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (rs_ctx->sig->state == ecdsa_sig_mul) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200299 goto mul;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 }
301 if (rs_ctx->sig->state == ecdsa_sig_modn) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200302 goto modn;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200304 }
305#endif /* MBEDTLS_ECP_RESTARTABLE */
306
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200307 *p_sign_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 do {
309 if ((*p_sign_tries)++ > 10) {
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200310 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
311 goto cleanup;
312 }
313
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100314 /*
315 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100316 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100317 */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200318 *p_key_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 do {
320 if ((*p_key_tries)++ > 10) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200322 goto cleanup;
323 }
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, pk, f_rng, p_rng));
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200326
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200327#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200329 rs_ctx->sig->state = ecdsa_sig_mul;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200331
332mul:
333#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &R, pk, &grp->G,
335 f_rng_blind,
336 p_rng_blind,
337 ECDSA_RS_ECP));
338 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pr, &R.X, &grp->N));
339 } while (mbedtls_mpi_cmp_int(pr, 0) == 0);
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200340
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200341#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200343 rs_ctx->sig->state = ecdsa_sig_modn;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200345
346modn:
347#endif
348 /*
349 * Accounting for everything up to the end of the loop
350 * (step 6, but checking now avoids saving e and t)
351 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 ECDSA_BUDGET(MBEDTLS_ECP_OPS_INV + 4);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100353
354 /*
355 * Step 5: derive MPI from hashed message
356 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100358
359 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200360 * Generate a random value to blind inv_mod in next step,
361 * avoiding a potential timing leak.
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, &t, f_rng_blind,
364 p_rng_blind));
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200365
366 /*
367 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100368 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, d));
370 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, s));
371 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&e, &e, &t));
372 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pk, pk, &t));
373 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pk, pk, &grp->N));
374 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(s, pk, &grp->N));
375 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e));
376 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N));
377 } while (mbedtls_mpi_cmp_int(s, 0) == 0);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100378
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200379#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
381 mbedtls_mpi_copy(r, pr);
382 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200383#endif
384
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100385cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_ecp_point_free(&R);
387 mbedtls_mpi_free(&k); mbedtls_mpi_free(&e); mbedtls_mpi_free(&t);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 ECDSA_RS_LEAVE(sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return ret;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100392}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100393
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200394/*
395 * Compute ECDSA signature of a hashed message
396 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
398 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
399 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200400{
Janos Follathdca667a2019-01-04 14:32:30 +0000401 /* Use the same RNG for both blinding and ephemeral key generation */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000402 return mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
403 f_rng, p_rng, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200404}
Ron Eldor936d2842018-11-01 13:05:52 +0200405#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100408/*
409 * Deterministic signature wrapper
TRodziewicz18efb732021-04-29 23:12:19 +0200410 *
TRodziewicz7e9422d2021-04-30 10:32:58 +0200411 * note: The f_rng_blind parameter must not be NULL.
TRodziewicz18efb732021-04-29 23:12:19 +0200412 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100413 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000414int mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
415 mbedtls_mpi *r, mbedtls_mpi *s,
416 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
417 mbedtls_md_type_t md_alg,
418 int (*f_rng_blind)(void *, unsigned char *, size_t),
419 void *p_rng_blind,
420 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100421{
Janos Follath24eed8d2019-11-22 13:21:35 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200424 mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 size_t grp_len = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 const mbedtls_md_info_t *md_info;
428 mbedtls_mpi h;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) {
431 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
432 }
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_mpi_init(&h);
435 mbedtls_hmac_drbg_init(&rng_ctx);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 ECDSA_RS_ENTER(det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200438
439#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (rs_ctx != NULL && rs_ctx->det != NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200441 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200442 p_rng = &rs_ctx->det->rng_ctx;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200443
444 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (rs_ctx->det->state == ecdsa_det_sign) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200446 goto sign;
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200448 }
449#endif /* MBEDTLS_ECP_RESTARTABLE */
450
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100451 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(d, data, grp_len));
453 MBEDTLS_MPI_CHK(derive_mpi(grp, &h, buf, blen));
454 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&h, data + grp_len, grp_len));
455 mbedtls_hmac_drbg_seed_buf(p_rng, md_info, data, 2 * grp_len);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100456
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200457#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (rs_ctx != NULL && rs_ctx->det != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200459 rs_ctx->det->state = ecdsa_det_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200461
462sign:
463#endif
Ron Eldor8493f802018-11-01 11:32:15 +0200464#if defined(MBEDTLS_ECDSA_SIGN_ALT)
Steven Cooreman6dce4bb2021-02-10 17:07:20 +0100465 (void) f_rng_blind;
466 (void) p_rng_blind;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 ret = mbedtls_ecdsa_sign(grp, r, s, d, buf, blen,
468 mbedtls_hmac_drbg_random, p_rng);
Ron Eldor8493f802018-11-01 11:32:15 +0200469#else
Paul Elliott2ba002c2022-12-09 18:59:26 +0000470 ret = mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
471 mbedtls_hmac_drbg_random, p_rng,
472 f_rng_blind, p_rng_blind, rs_ctx);
Ron Eldor936d2842018-11-01 13:05:52 +0200473#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100474
475cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_hmac_drbg_free(&rng_ctx);
477 mbedtls_mpi_free(&h);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 ECDSA_RS_LEAVE(det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return ret;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100482}
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200483
484/*
TRodziewicz18efb732021-04-29 23:12:19 +0200485 * Deterministic signature wrapper
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
488 mbedtls_mpi *s, const mbedtls_mpi *d,
489 const unsigned char *buf, size_t blen,
490 mbedtls_md_type_t md_alg,
491 int (*f_rng_blind)(void *, unsigned char *,
492 size_t),
493 void *p_rng_blind)
Janos Follathdca667a2019-01-04 14:32:30 +0000494{
Paul Elliott2ba002c2022-12-09 18:59:26 +0000495 return mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg,
496 f_rng_blind, p_rng_blind, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100499
Ron Eldor936d2842018-11-01 13:05:52 +0200500#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100501/*
502 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
503 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
504 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000505int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
506 const unsigned char *buf, size_t blen,
507 const mbedtls_ecp_point *Q,
508 const mbedtls_mpi *r,
509 const mbedtls_mpi *s,
510 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100511{
Janos Follath24eed8d2019-11-22 13:21:35 +0000512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi e, s_inv, u1, u2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200514 mbedtls_ecp_point R;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200515 mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_ecp_point_init(&R);
518 mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv);
519 mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100520
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100521 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
523 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
524 }
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 ECDSA_RS_ENTER(ver);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200527
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200528#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (rs_ctx != NULL && rs_ctx->ver != NULL) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200530 /* redirect to our context */
531 pu1 = &rs_ctx->ver->u1;
532 pu2 = &rs_ctx->ver->u2;
533
534 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (rs_ctx->ver->state == ecdsa_ver_muladd) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200536 goto muladd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 }
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200538 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200539#endif /* MBEDTLS_ECP_RESTARTABLE */
540
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100541 /*
542 * Step 1: make sure r and s are in range 1..n-1
543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 ||
545 mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200547 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100548 }
549
550 /*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100551 * Step 3: derive MPI from hashed message
552 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100554
555 /*
556 * Step 4: u1 = e / s mod n, u2 = r / s mod n
557 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 ECDSA_BUDGET(MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2);
Manuel Pégourié-Gonnardbfa19722017-08-23 17:39:18 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&s_inv, s, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu1, &e, &s_inv));
563 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu1, pu1, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu2, r, &s_inv));
566 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu2, pu2, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100567
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200568#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (rs_ctx != NULL && rs_ctx->ver != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200570 rs_ctx->ver->state = ecdsa_ver_muladd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 }
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200572
573muladd:
574#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100575 /*
576 * Step 5: R = u1 G + u2 Q
577 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 MBEDTLS_MPI_CHK(mbedtls_ecp_muladd_restartable(grp,
579 &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (mbedtls_ecp_is_zero(&R)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200583 goto cleanup;
584 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100585
586 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100587 * Step 6: convert xR to an integer (no-op)
588 * Step 7: reduce xR mod n (gives v)
589 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&R.X, &R.X, &grp->N));
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100591
592 /*
593 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100594 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (mbedtls_mpi_cmp_mpi(&R.X, r) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200597 goto cleanup;
598 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100599
600cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 mbedtls_ecp_point_free(&R);
602 mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv);
603 mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 ECDSA_RS_LEAVE(ver);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100608}
609
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200610/*
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200611 * Verify ECDSA signature of hashed message
612 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
614 const unsigned char *buf, size_t blen,
615 const mbedtls_ecp_point *Q,
616 const mbedtls_mpi *r,
617 const mbedtls_mpi *s)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200618{
Paul Elliott2ba002c2022-12-09 18:59:26 +0000619 return mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200620}
Ron Eldor936d2842018-11-01 13:05:52 +0200621#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200622
623/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100624 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200625 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s,
627 unsigned char *sig, size_t sig_size,
628 size_t *slen)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200629{
Janos Follath24eed8d2019-11-22 13:21:35 +0000630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 };
632 unsigned char *p = buf + sizeof(buf);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200633 size_t len = 0;
634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, s));
636 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, r));
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
639 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
640 MBEDTLS_ASN1_CONSTRUCTED |
641 MBEDTLS_ASN1_SEQUENCE));
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (len > sig_size) {
644 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
645 }
Gilles Peskinef00f1522021-06-22 00:09:00 +0200646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 memcpy(sig, p, len);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200648 *slen = len;
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return 0;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200651}
652
653/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100654 * Compute and write signature
655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
657 mbedtls_md_type_t md_alg,
658 const unsigned char *hash, size_t hlen,
659 unsigned char *sig, size_t sig_size, size_t *slen,
660 int (*f_rng)(void *, unsigned char *, size_t),
661 void *p_rng,
662 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100663{
Janos Follath24eed8d2019-11-22 13:21:35 +0000664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_mpi r, s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (f_rng == NULL) {
667 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
668 }
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 mbedtls_mpi_init(&r);
671 mbedtls_mpi_init(&s);
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Paul Elliott2ba002c2022-12-09 18:59:26 +0000674 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d,
675 hash, hlen, md_alg, f_rng,
676 p_rng, rs_ctx));
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200677#else
678 (void) md_alg;
679
Ron Eldor8493f802018-11-01 11:32:15 +0200680#if defined(MBEDTLS_ECDSA_SIGN_ALT)
Steven Cooremanfa6641b2021-01-11 17:11:39 +0100681 (void) rs_ctx;
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ctx->grp, &r, &s, &ctx->d,
684 hash, hlen, f_rng, p_rng));
Ron Eldor8493f802018-11-01 11:32:15 +0200685#else
Janos Follathdca667a2019-01-04 14:32:30 +0000686 /* Use the same RNG for both blinding and ephemeral key generation */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000687 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d,
688 hash, hlen, f_rng, p_rng, f_rng,
689 p_rng, rs_ctx));
Ron Eldor936d2842018-11-01 13:05:52 +0200690#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Ron Eldor5ed8c1e2018-11-05 14:04:26 +0200691#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 MBEDTLS_MPI_CHK(ecdsa_signature_to_asn1(&r, &s, sig, sig_size, slen));
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200694
695cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_mpi_free(&r);
697 mbedtls_mpi_free(&s);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return ret;
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100700}
701
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200702/*
703 * Compute and write signature
704 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
706 mbedtls_md_type_t md_alg,
707 const unsigned char *hash, size_t hlen,
708 unsigned char *sig, size_t sig_size, size_t *slen,
709 int (*f_rng)(void *, unsigned char *, size_t),
710 void *p_rng)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200711{
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 return mbedtls_ecdsa_write_signature_restartable(
713 ctx, md_alg, hash, hlen, sig, sig_size, slen,
714 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200715}
716
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100717/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200718 * Read and check signature
719 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
721 const unsigned char *hash, size_t hlen,
722 const unsigned char *sig, size_t slen)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200723{
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return mbedtls_ecdsa_read_signature_restartable(
725 ctx, hash, hlen, sig, slen, NULL);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200726}
727
728/*
729 * Restartable read and check signature
730 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
732 const unsigned char *hash, size_t hlen,
733 const unsigned char *sig, size_t slen,
734 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200735{
Janos Follath24eed8d2019-11-22 13:21:35 +0000736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200737 unsigned char *p = (unsigned char *) sig;
738 const unsigned char *end = sig + slen;
739 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_mpi r, s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 mbedtls_mpi_init(&r);
742 mbedtls_mpi_init(&s);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
745 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200747 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200748 }
749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (p + len != end) {
751 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
752 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200753 goto cleanup;
754 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 ||
757 (ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200759 goto cleanup;
760 }
Ron Eldor8493f802018-11-01 11:32:15 +0200761#if defined(MBEDTLS_ECDSA_VERIFY_ALT)
Steven Cooremanfa6641b2021-01-11 17:11:39 +0100762 (void) rs_ctx;
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if ((ret = mbedtls_ecdsa_verify(&ctx->grp, hash, hlen,
765 &ctx->Q, &r, &s)) != 0) {
Ron Eldor8493f802018-11-01 11:32:15 +0200766 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 }
Ron Eldor8493f802018-11-01 11:32:15 +0200768#else
Paul Elliott2ba002c2022-12-09 18:59:26 +0000769 if ((ret = mbedtls_ecdsa_verify_restartable(&ctx->grp, hash, hlen,
770 &ctx->Q, &r, &s, rs_ctx)) != 0) {
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200771 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 }
Ron Eldor936d2842018-11-01 13:05:52 +0200773#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200774
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200775 /* At this point we know that the buffer starts with a valid signature.
776 * Return 0 if the buffer just contains the signature, and a specific
777 * error code if the valid signature is followed by more data. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (p != end) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 }
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200781
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200782cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 mbedtls_mpi_free(&r);
784 mbedtls_mpi_free(&s);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 return ret;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200787}
788
Ron Eldor314adb62017-10-10 18:28:25 +0300789#if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200790/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200791 * Generate key pair
792 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
794 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200795{
Ron Eldoradb52342018-12-17 10:06:12 +0200796 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 ret = mbedtls_ecp_group_load(&ctx->grp, gid);
798 if (ret != 0) {
799 return ret;
800 }
Ron Eldoradb52342018-12-17 10:06:12 +0200801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d,
803 &ctx->Q, f_rng, p_rng);
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200804}
Ron Eldor936d2842018-11-01 13:05:52 +0200805#endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200806
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200807/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 * Set context from an mbedtls_ecp_keypair
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200809 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200811{
Janos Follath24eed8d2019-11-22 13:21:35 +0000812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 ||
814 (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
815 (ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
816 mbedtls_ecdsa_free(ctx);
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100817 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 return ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200820}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200821
822/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200823 * Initialize context
824 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200826{
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 mbedtls_ecp_keypair_init(ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200828}
829
830/*
831 * Free context
832 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200834{
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (ctx == NULL) {
Hanno Becker319ae112018-12-14 16:43:29 +0000836 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 }
Hanno Becker319ae112018-12-14 16:43:29 +0000838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 mbedtls_ecp_keypair_free(ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200840}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100841
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200842#if defined(MBEDTLS_ECP_RESTARTABLE)
843/*
844 * Initialize a restart context
845 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200847{
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 mbedtls_ecp_restart_init(&ctx->ecp);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200849
850 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200851 ctx->sig = NULL;
852#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
853 ctx->det = NULL;
854#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200855}
856
857/*
858 * Free the components of a restart context
859 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200861{
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (ctx == NULL) {
Hanno Becker319ae112018-12-14 16:43:29 +0000863 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 }
Hanno Becker319ae112018-12-14 16:43:29 +0000865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 mbedtls_ecp_restart_free(&ctx->ecp);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 ecdsa_restart_ver_free(ctx->ver);
869 mbedtls_free(ctx->ver);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200870 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 ecdsa_restart_sig_free(ctx->sig);
873 mbedtls_free(ctx->sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200874 ctx->sig = NULL;
875
876#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 ecdsa_restart_det_free(ctx->det);
878 mbedtls_free(ctx->det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200879 ctx->det = NULL;
880#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200881}
882#endif /* MBEDTLS_ECP_RESTARTABLE */
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_ECDSA_C */