blob: 7971ef4ae21b9a9c38151d44ee44c800ddacff0c [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01006 */
7
8/*
9 * References:
10 *
Xiaokang Qianb92a2f62023-04-19 02:59:15 +000011 * SEC1 https://www.secg.org/sec1-v2.pdf
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010012 */
13
Gilles Peskinedb09ef62020-06-03 01:43:33 +020014#include "common.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010017
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/ecdsa.h"
19#include "mbedtls/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010020
Rich Evans00ab4702015-02-06 13:43:58 +000021#include <string.h>
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010025#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010026
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020027#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020028
Hanno Becker319ae112018-12-14 16:43:29 +000029#include "mbedtls/platform_util.h"
Harry Ramseya05bfee2024-10-14 07:19:01 +010030#include "mbedtls/error_common.h"
Hanno Becker319ae112018-12-14 16:43:29 +000031
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020032#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020033
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020034/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +020035 * Sub-context for ecdsa_verify()
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020036 */
Gilles Peskine449bd832023-01-11 14:50:10 +010037struct mbedtls_ecdsa_restart_ver {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020038 mbedtls_mpi u1, u2; /* intermediate values */
39 enum { /* what to do next? */
40 ecdsa_ver_init = 0, /* getting started */
41 ecdsa_ver_muladd, /* muladd step */
42 } state;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020043};
44
45/*
46 * Init verify restart sub-context
47 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048static void ecdsa_restart_ver_init(mbedtls_ecdsa_restart_ver_ctx *ctx)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020049{
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_mpi_init(&ctx->u1);
51 mbedtls_mpi_init(&ctx->u2);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +020052 ctx->state = ecdsa_ver_init;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020053}
54
55/*
56 * Free the components of a verify restart sub-context
57 */
Gilles Peskine449bd832023-01-11 14:50:10 +010058static void ecdsa_restart_ver_free(mbedtls_ecdsa_restart_ver_ctx *ctx)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020059{
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (ctx == NULL) {
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020061 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010062 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 mbedtls_mpi_free(&ctx->u1);
65 mbedtls_mpi_free(&ctx->u2);
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 ecdsa_restart_ver_init(ctx);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020068}
69
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020070/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +020071 * Sub-context for ecdsa_sign()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020072 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073struct mbedtls_ecdsa_restart_sig {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +020074 int sign_tries;
75 int key_tries;
76 mbedtls_mpi k; /* per-signature random */
77 mbedtls_mpi r; /* r value */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020078 enum { /* what to do next? */
79 ecdsa_sig_init = 0, /* getting started */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +020080 ecdsa_sig_mul, /* doing ecp_mul() */
81 ecdsa_sig_modn, /* mod N computations */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020082 } state;
83};
84
85/*
86 * Init verify sign sub-context
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088static void ecdsa_restart_sig_init(mbedtls_ecdsa_restart_sig_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020089{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +020090 ctx->sign_tries = 0;
91 ctx->key_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_mpi_init(&ctx->k);
93 mbedtls_mpi_init(&ctx->r);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +020094 ctx->state = ecdsa_sig_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020095}
96
97/*
98 * Free the components of a sign restart sub-context
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static void ecdsa_restart_sig_free(mbedtls_ecdsa_restart_sig_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200101{
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (ctx == NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200103 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_mpi_free(&ctx->k);
107 mbedtls_mpi_free(&ctx->r);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200108}
109
110#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
111/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +0200112 * Sub-context for ecdsa_sign_det()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114struct mbedtls_ecdsa_restart_det {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200115 mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200116 enum { /* what to do next? */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200117 ecdsa_det_init = 0, /* getting started */
118 ecdsa_det_sign, /* make signature */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200119 } state;
120};
121
122/*
123 * Init verify sign_det sub-context
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125static void ecdsa_restart_det_init(mbedtls_ecdsa_restart_det_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200126{
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_hmac_drbg_init(&ctx->rng_ctx);
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200128 ctx->state = ecdsa_det_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200129}
130
131/*
132 * Free the components of a sign_det restart sub-context
133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134static void ecdsa_restart_det_free(mbedtls_ecdsa_restart_det_ctx *ctx)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200135{
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (ctx == NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200137 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_hmac_drbg_free(&ctx->rng_ctx);
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 ecdsa_restart_det_init(ctx);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200143}
144#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146#define ECDSA_RS_ECP (rs_ctx == NULL ? NULL : &rs_ctx->ecp)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200147
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200148/* Utility macro for checking and updating ops budget */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149#define ECDSA_BUDGET(ops) \
150 MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, ECDSA_RS_ECP, ops));
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200151
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200152/* Call this when entering a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153#define ECDSA_RS_ENTER(SUB) do { \
154 /* reset ops count for this call if top-level */ \
155 if (rs_ctx != NULL && rs_ctx->ecp.depth++ == 0) \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200156 rs_ctx->ecp.ops_done = 0; \
157 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 /* set up our own sub-context if needed */ \
159 if (mbedtls_ecp_restart_is_enabled() && \
160 rs_ctx != NULL && rs_ctx->SUB == NULL) \
161 { \
162 rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
163 if (rs_ctx->SUB == NULL) \
164 return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
165 \
166 ecdsa_restart_## SUB ##_init(rs_ctx->SUB); \
167 } \
168} while (0)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200169
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200170/* Call this when leaving a function that needs its own sub-context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171#define ECDSA_RS_LEAVE(SUB) do { \
172 /* clear our sub-context when not in progress (done or error) */ \
173 if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
174 ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
175 { \
176 ecdsa_restart_## SUB ##_free(rs_ctx->SUB); \
177 mbedtls_free(rs_ctx->SUB); \
178 rs_ctx->SUB = NULL; \
179 } \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200180 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (rs_ctx != NULL) \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200182 rs_ctx->ecp.depth--; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100183} while (0)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200184
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200185#else /* MBEDTLS_ECP_RESTARTABLE */
186
187#define ECDSA_RS_ECP NULL
188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189#define ECDSA_BUDGET(ops) /* no-op; for compatibility */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191#define ECDSA_RS_ENTER(SUB) (void) rs_ctx
192#define ECDSA_RS_LEAVE(SUB) (void) rs_ctx
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200193
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200194#endif /* MBEDTLS_ECP_RESTARTABLE */
195
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100196/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100197 * Derive a suitable integer for group grp from a buffer of length len
198 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200static int derive_mpi(const mbedtls_ecp_group *grp, mbedtls_mpi *x,
201 const unsigned char *buf, size_t blen)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100202{
Janos Follath24eed8d2019-11-22 13:21:35 +0000203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 size_t n_size = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100205 size_t use_size = blen > n_size ? n_size : blen;
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(x, buf, use_size));
208 if (use_size * 8 > grp->nbits) {
209 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(x, use_size * 8 - grp->nbits));
210 }
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100211
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100212 /* While at it, reduce modulo N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (mbedtls_mpi_cmp_mpi(x, &grp->N) >= 0) {
214 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(x, x, &grp->N));
215 }
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100216
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100217cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100219}
220
JonathanWitthoeft405ec942023-04-26 10:24:12 -0500221int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid)
222{
223 switch (gid) {
224#ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
225 case MBEDTLS_ECP_DP_CURVE25519: return 0;
226#endif
227#ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
228 case MBEDTLS_ECP_DP_CURVE448: return 0;
229#endif
230 default: return 1;
231 }
232}
233
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100234/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100235 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
236 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
237 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000238int mbedtls_ecdsa_sign_restartable(mbedtls_ecp_group *grp,
239 mbedtls_mpi *r, mbedtls_mpi *s,
240 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
241 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
242 int (*f_rng_blind)(void *, unsigned char *, size_t),
243 void *p_rng_blind,
244 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100245{
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200246 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200247 int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_ecp_point R;
249 mbedtls_mpi k, e, t;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200250 mbedtls_mpi *pk = &k, *pr = r;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100251
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100252 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
254 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
255 }
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100256
Darryl Greenc64a48b2017-11-17 17:09:17 +0000257 /* Make sure d is in range 1..n-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
259 return MBEDTLS_ERR_ECP_INVALID_KEY;
260 }
Darryl Greenc64a48b2017-11-17 17:09:17 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_ecp_point_init(&R);
263 mbedtls_mpi_init(&k); mbedtls_mpi_init(&e); mbedtls_mpi_init(&t);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 ECDSA_RS_ENTER(sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200266
267#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200269 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200270 p_sign_tries = &rs_ctx->sig->sign_tries;
271 p_key_tries = &rs_ctx->sig->key_tries;
272 pk = &rs_ctx->sig->k;
273 pr = &rs_ctx->sig->r;
274
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200275 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (rs_ctx->sig->state == ecdsa_sig_mul) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200277 goto mul;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 }
279 if (rs_ctx->sig->state == ecdsa_sig_modn) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200280 goto modn;
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200282 }
283#endif /* MBEDTLS_ECP_RESTARTABLE */
284
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200285 *p_sign_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 do {
287 if ((*p_sign_tries)++ > 10) {
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200288 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
289 goto cleanup;
290 }
291
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100292 /*
293 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100294 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100295 */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200296 *p_key_tries = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 do {
298 if ((*p_key_tries)++ > 10) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200300 goto cleanup;
301 }
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, pk, f_rng, p_rng));
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200304
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200305#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200307 rs_ctx->sig->state = ecdsa_sig_mul;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200309
310mul:
311#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &R, pk, &grp->G,
313 f_rng_blind,
314 p_rng_blind,
315 ECDSA_RS_ECP));
316 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pr, &R.X, &grp->N));
317 } while (mbedtls_mpi_cmp_int(pr, 0) == 0);
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200318
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200319#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200321 rs_ctx->sig->state = ecdsa_sig_modn;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200323
324modn:
325#endif
326 /*
327 * Accounting for everything up to the end of the loop
328 * (step 6, but checking now avoids saving e and t)
329 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 ECDSA_BUDGET(MBEDTLS_ECP_OPS_INV + 4);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100331
332 /*
333 * Step 5: derive MPI from hashed message
334 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100336
337 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200338 * Generate a random value to blind inv_mod in next step,
339 * avoiding a potential timing leak.
340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, &t, f_rng_blind,
342 p_rng_blind));
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200343
344 /*
345 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, d));
348 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, s));
349 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&e, &e, &t));
350 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pk, pk, &t));
351 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pk, pk, &grp->N));
352 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(s, pk, &grp->N));
353 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e));
354 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N));
355 } while (mbedtls_mpi_cmp_int(s, 0) == 0);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100356
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200357#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (rs_ctx != NULL && rs_ctx->sig != NULL) {
Chien Wonge2caf412023-08-01 21:38:46 +0800359 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(r, pr));
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200361#endif
362
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100363cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_ecp_point_free(&R);
365 mbedtls_mpi_free(&k); mbedtls_mpi_free(&e); mbedtls_mpi_free(&t);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 ECDSA_RS_LEAVE(sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return ret;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100370}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100371
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200372/*
373 * Compute ECDSA signature of a hashed message
374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
376 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
377 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200378{
Janos Follathdca667a2019-01-04 14:32:30 +0000379 /* Use the same RNG for both blinding and ephemeral key generation */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000380 return mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
381 f_rng, p_rng, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100385/*
386 * Deterministic signature wrapper
TRodziewicz18efb732021-04-29 23:12:19 +0200387 *
TRodziewicz7e9422d2021-04-30 10:32:58 +0200388 * note: The f_rng_blind parameter must not be NULL.
TRodziewicz18efb732021-04-29 23:12:19 +0200389 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100390 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000391int mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
392 mbedtls_mpi *r, mbedtls_mpi *s,
393 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
394 mbedtls_md_type_t md_alg,
395 int (*f_rng_blind)(void *, unsigned char *, size_t),
396 void *p_rng_blind,
397 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100398{
Janos Follath24eed8d2019-11-22 13:21:35 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200401 mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 size_t grp_len = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 const mbedtls_md_info_t *md_info;
405 mbedtls_mpi h;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) {
408 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
409 }
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_mpi_init(&h);
412 mbedtls_hmac_drbg_init(&rng_ctx);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 ECDSA_RS_ENTER(det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200415
416#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (rs_ctx != NULL && rs_ctx->det != NULL) {
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200418 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200419 p_rng = &rs_ctx->det->rng_ctx;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200420
421 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (rs_ctx->det->state == ecdsa_det_sign) {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200423 goto sign;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 }
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200425 }
426#endif /* MBEDTLS_ECP_RESTARTABLE */
427
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100428 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(d, data, grp_len));
430 MBEDTLS_MPI_CHK(derive_mpi(grp, &h, buf, blen));
431 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&h, data + grp_len, grp_len));
Chien Wonge2caf412023-08-01 21:38:46 +0800432 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 +0100433
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200434#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (rs_ctx != NULL && rs_ctx->det != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200436 rs_ctx->det->state = ecdsa_det_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200438
439sign:
440#endif
Paul Elliott2ba002c2022-12-09 18:59:26 +0000441 ret = mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
442 mbedtls_hmac_drbg_random, p_rng,
443 f_rng_blind, p_rng_blind, rs_ctx);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100444
445cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 mbedtls_hmac_drbg_free(&rng_ctx);
447 mbedtls_mpi_free(&h);
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 ECDSA_RS_LEAVE(det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return ret;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100452}
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200453
454/*
TRodziewicz18efb732021-04-29 23:12:19 +0200455 * Deterministic signature wrapper
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
458 mbedtls_mpi *s, const mbedtls_mpi *d,
459 const unsigned char *buf, size_t blen,
460 mbedtls_md_type_t md_alg,
461 int (*f_rng_blind)(void *, unsigned char *,
462 size_t),
463 void *p_rng_blind)
Janos Follathdca667a2019-01-04 14:32:30 +0000464{
Paul Elliott2ba002c2022-12-09 18:59:26 +0000465 return mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg,
466 f_rng_blind, p_rng_blind, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100469
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100470/*
471 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
472 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
473 */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000474int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
475 const unsigned char *buf, size_t blen,
476 const mbedtls_ecp_point *Q,
477 const mbedtls_mpi *r,
478 const mbedtls_mpi *s,
479 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100480{
Janos Follath24eed8d2019-11-22 13:21:35 +0000481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_mpi e, s_inv, u1, u2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200483 mbedtls_ecp_point R;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200484 mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 mbedtls_ecp_point_init(&R);
487 mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv);
488 mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100489
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100490 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
492 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
493 }
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 ECDSA_RS_ENTER(ver);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200496
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200497#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (rs_ctx != NULL && rs_ctx->ver != NULL) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200499 /* redirect to our context */
500 pu1 = &rs_ctx->ver->u1;
501 pu2 = &rs_ctx->ver->u2;
502
503 /* jump to current step */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (rs_ctx->ver->state == ecdsa_ver_muladd) {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200505 goto muladd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 }
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200507 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200508#endif /* MBEDTLS_ECP_RESTARTABLE */
509
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100510 /*
511 * Step 1: make sure r and s are in range 1..n-1
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 ||
514 mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200516 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100517 }
518
519 /*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100520 * Step 3: derive MPI from hashed message
521 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100523
524 /*
525 * Step 4: u1 = e / s mod n, u2 = r / s mod n
526 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 ECDSA_BUDGET(MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2);
Manuel Pégourié-Gonnardbfa19722017-08-23 17:39:18 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&s_inv, s, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu1, &e, &s_inv));
532 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu1, pu1, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu2, r, &s_inv));
535 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu2, pu2, &grp->N));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100536
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200537#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (rs_ctx != NULL && rs_ctx->ver != NULL) {
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200539 rs_ctx->ver->state = ecdsa_ver_muladd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 }
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200541
542muladd:
543#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100544 /*
545 * Step 5: R = u1 G + u2 Q
546 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_MPI_CHK(mbedtls_ecp_muladd_restartable(grp,
548 &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP));
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (mbedtls_ecp_is_zero(&R)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200552 goto cleanup;
553 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100554
555 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100556 * Step 6: convert xR to an integer (no-op)
557 * Step 7: reduce xR mod n (gives v)
558 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&R.X, &R.X, &grp->N));
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100560
561 /*
562 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100563 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (mbedtls_mpi_cmp_mpi(&R.X, r) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200566 goto cleanup;
567 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100568
569cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mbedtls_ecp_point_free(&R);
571 mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv);
572 mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 ECDSA_RS_LEAVE(ver);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100577}
578
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200579/*
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200580 * Verify ECDSA signature of hashed message
581 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
583 const unsigned char *buf, size_t blen,
584 const mbedtls_ecp_point *Q,
585 const mbedtls_mpi *r,
586 const mbedtls_mpi *s)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200587{
Paul Elliott2ba002c2022-12-09 18:59:26 +0000588 return mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200589}
590
591/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100592 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200593 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s,
595 unsigned char *sig, size_t sig_size,
596 size_t *slen)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200597{
Janos Follath24eed8d2019-11-22 13:21:35 +0000598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 };
600 unsigned char *p = buf + sizeof(buf);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200601 size_t len = 0;
602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, s));
604 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, r));
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
607 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
608 MBEDTLS_ASN1_CONSTRUCTED |
609 MBEDTLS_ASN1_SEQUENCE));
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (len > sig_size) {
612 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
613 }
Gilles Peskinef00f1522021-06-22 00:09:00 +0200614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 memcpy(sig, p, len);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200616 *slen = len;
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return 0;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200619}
620
621/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100622 * Compute and write signature
623 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
625 mbedtls_md_type_t md_alg,
626 const unsigned char *hash, size_t hlen,
627 unsigned char *sig, size_t sig_size, size_t *slen,
628 int (*f_rng)(void *, unsigned char *, size_t),
629 void *p_rng,
630 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100631{
Janos Follath24eed8d2019-11-22 13:21:35 +0000632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_mpi r, s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (f_rng == NULL) {
635 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
636 }
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 mbedtls_mpi_init(&r);
639 mbedtls_mpi_init(&s);
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Paul Elliott2ba002c2022-12-09 18:59:26 +0000642 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d,
643 hash, hlen, md_alg, f_rng,
644 p_rng, rs_ctx));
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200645#else
646 (void) md_alg;
647
Janos Follathdca667a2019-01-04 14:32:30 +0000648 /* Use the same RNG for both blinding and ephemeral key generation */
Paul Elliott2ba002c2022-12-09 18:59:26 +0000649 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d,
650 hash, hlen, f_rng, p_rng, f_rng,
651 p_rng, rs_ctx));
Ron Eldor5ed8c1e2018-11-05 14:04:26 +0200652#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 MBEDTLS_MPI_CHK(ecdsa_signature_to_asn1(&r, &s, sig, sig_size, slen));
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200655
656cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 mbedtls_mpi_free(&r);
658 mbedtls_mpi_free(&s);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 return ret;
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100661}
662
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200663/*
664 * Compute and write signature
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
667 mbedtls_md_type_t md_alg,
668 const unsigned char *hash, size_t hlen,
669 unsigned char *sig, size_t sig_size, size_t *slen,
670 int (*f_rng)(void *, unsigned char *, size_t),
671 void *p_rng)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200672{
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return mbedtls_ecdsa_write_signature_restartable(
674 ctx, md_alg, hash, hlen, sig, sig_size, slen,
675 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200676}
677
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100678/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200679 * Read and check signature
680 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100681int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
682 const unsigned char *hash, size_t hlen,
683 const unsigned char *sig, size_t slen)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200684{
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return mbedtls_ecdsa_read_signature_restartable(
686 ctx, hash, hlen, sig, slen, NULL);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200687}
688
689/*
690 * Restartable read and check signature
691 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100692int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
693 const unsigned char *hash, size_t hlen,
694 const unsigned char *sig, size_t slen,
695 mbedtls_ecdsa_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200696{
Janos Follath24eed8d2019-11-22 13:21:35 +0000697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200698 unsigned char *p = (unsigned char *) sig;
699 const unsigned char *end = sig + slen;
700 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi r, s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 mbedtls_mpi_init(&r);
703 mbedtls_mpi_init(&s);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
706 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200708 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200709 }
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (p + len != end) {
712 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
713 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200714 goto cleanup;
715 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 ||
718 (ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200720 goto cleanup;
721 }
Steven Cooremanfa6641b2021-01-11 17:11:39 +0100722
Paul Elliott2ba002c2022-12-09 18:59:26 +0000723 if ((ret = mbedtls_ecdsa_verify_restartable(&ctx->grp, hash, hlen,
724 &ctx->Q, &r, &s, rs_ctx)) != 0) {
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200725 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200727
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200728 /* At this point we know that the buffer starts with a valid signature.
729 * Return 0 if the buffer just contains the signature, and a specific
730 * error code if the valid signature is followed by more data. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (p != end) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 }
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200734
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200735cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_mpi_free(&r);
737 mbedtls_mpi_free(&s);
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return ret;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200740}
741
742/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200743 * Generate key pair
744 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
746 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200747{
Ron Eldoradb52342018-12-17 10:06:12 +0200748 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 ret = mbedtls_ecp_group_load(&ctx->grp, gid);
750 if (ret != 0) {
751 return ret;
752 }
Ron Eldoradb52342018-12-17 10:06:12 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d,
755 &ctx->Q, f_rng, p_rng);
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200756}
757
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200758/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 * Set context from an mbedtls_ecp_keypair
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200762{
Janos Follath24eed8d2019-11-22 13:21:35 +0000763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 ||
765 (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
766 (ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
767 mbedtls_ecdsa_free(ctx);
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100768 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200771}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200772
773/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200774 * Initialize context
775 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200777{
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 mbedtls_ecp_keypair_init(ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200779}
780
781/*
782 * Free context
783 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200785{
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (ctx == NULL) {
Hanno Becker319ae112018-12-14 16:43:29 +0000787 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 }
Hanno Becker319ae112018-12-14 16:43:29 +0000789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 mbedtls_ecp_keypair_free(ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200791}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100792
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200793#if defined(MBEDTLS_ECP_RESTARTABLE)
794/*
795 * Initialize a restart context
796 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200798{
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_ecp_restart_init(&ctx->ecp);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200800
801 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200802 ctx->sig = NULL;
803#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
804 ctx->det = NULL;
805#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200806}
807
808/*
809 * Free the components of a restart context
810 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200812{
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (ctx == NULL) {
Hanno Becker319ae112018-12-14 16:43:29 +0000814 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 }
Hanno Becker319ae112018-12-14 16:43:29 +0000816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 mbedtls_ecp_restart_free(&ctx->ecp);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 ecdsa_restart_ver_free(ctx->ver);
820 mbedtls_free(ctx->ver);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200821 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 ecdsa_restart_sig_free(ctx->sig);
824 mbedtls_free(ctx->sig);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200825 ctx->sig = NULL;
826
827#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 ecdsa_restart_det_free(ctx->det);
829 mbedtls_free(ctx->det);
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200830 ctx->det = NULL;
831#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200832}
833#endif /* MBEDTLS_ECP_RESTARTABLE */
834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_ECDSA_C */