blob: e797c1a2f473a93114bb7fffec291ec774f8984d [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
Rose Zadik817297f2018-03-27 11:30:14 +01004 * \brief This file contains ECDSA definitions and functions.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01005 *
Rose Zadik817297f2018-03-27 11:30:14 +01006 * The Elliptic Curve Digital Signature Algorithm (ECDSA) is defined in
7 * <em>Standards for Efficient Cryptography Group (SECG):
Rose Zadikbff87d92018-01-25 21:58:53 +00008 * SEC1 Elliptic Curve Cryptography</em>.
9 * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve
10 * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
11 *
Darryl Greena40a1012018-01-05 15:33:17 +000012 */
13/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020014 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 * SPDX-License-Identifier: Apache-2.0
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010028 */
Rose Zadikbff87d92018-01-25 21:58:53 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#ifndef MBEDTLS_ECDSA_H
31#define MBEDTLS_ECDSA_H
Mateusz Starzyk2abe51c2021-06-07 11:08:01 +020032#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010033
Bence Szépkútic662b362021-05-27 11:25:03 +020034#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050035
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/ecp.h"
37#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010038
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010039/**
40 * \brief Maximum ECDSA signature size for a given curve bit size
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020041 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010042 * \param bits Curve size in bits
43 * \return Maximum signature size in bytes
44 *
45 * \note This macro returns a compile-time constant if its argument
46 * is one. It may evaluate its argument multiple times.
47 */
48/*
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020049 * Ecdsa-Sig-Value ::= SEQUENCE {
50 * r INTEGER,
51 * s INTEGER
52 * }
53 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010054 * For each of r and s, the value (V) may include an extra initial "0" bit.
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020055 */
Gilles Peskine449bd832023-01-11 14:50:10 +010056#define MBEDTLS_ECDSA_MAX_SIG_LEN(bits) \
57 (/*T,L of SEQUENCE*/ ((bits) >= 61 * 8 ? 3 : 2) + \
58 /*T,L of r,s*/ 2 * (((bits) >= 127 * 8 ? 3 : 2) + \
59 /*V of r,s*/ ((bits) + 8) / 8))
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010060
Rose Zadikbff87d92018-01-25 21:58:53 +000061/** The maximal size of an ECDSA signature in Bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +010062#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020063
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020064#ifdef __cplusplus
65extern "C" {
66#endif
67
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020068/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020069 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020070 *
71 * \warning Performing multiple operations concurrently on the same
72 * ECDSA context is not supported; objects of this type
73 * should not be shared between multiple threads.
Valerio Setti0568dec2023-02-02 12:05:41 +010074 *
75 * \note pk_wrap module assumes that "ecdsa_context" is identical
76 * to "ecp_keypair" (see for example structure
77 * "mbedtls_eckey_info" where ECDSA sign/verify functions
78 * are used also for EC key)
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020081
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020082#if defined(MBEDTLS_ECP_RESTARTABLE)
83
84/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020085 * \brief Internal restart context for ecdsa_verify()
86 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020087 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020088 */
89typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
90
91/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020092 * \brief Internal restart context for ecdsa_sign()
93 *
94 * \note Opaque struct, defined in ecdsa.c
95 */
96typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
97
98#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
99/**
100 * \brief Internal restart context for ecdsa_sign_det()
101 *
102 * \note Opaque struct, defined in ecdsa.c
103 */
104typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
105#endif
106
107/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200108 * \brief General context for resuming ECDSA operations
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110typedef struct {
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200111 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(ecp); /*!< base context for ECP restart and
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 shared administrative info */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200113 mbedtls_ecdsa_restart_ver_ctx *MBEDTLS_PRIVATE(ver); /*!< ecdsa_verify() sub-context */
114 mbedtls_ecdsa_restart_sig_ctx *MBEDTLS_PRIVATE(sig); /*!< ecdsa_sign() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200115#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200116 mbedtls_ecdsa_restart_det_ctx *MBEDTLS_PRIVATE(det); /*!< ecdsa_sign_det() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200117#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200118} mbedtls_ecdsa_restart_ctx;
119
120#else /* MBEDTLS_ECP_RESTARTABLE */
121
122/* Now we can declare functions that take a pointer to that */
123typedef void mbedtls_ecdsa_restart_ctx;
124
125#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100126
127/**
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000128 * \brief This function checks whether a given group can be used
129 * for ECDSA.
130 *
131 * \param gid The ECP group ID to check.
132 *
133 * \return \c 1 if the group can be used, \c 0 otherwise
134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid);
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000136
137/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000138 * \brief This function computes the ECDSA signature of a
139 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100140 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 * \note The deterministic version implemented in
TRodziewicz18efb732021-04-29 23:12:19 +0200142 * mbedtls_ecdsa_sign_det_ext() is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100143 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000144 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100145 * bitlength of the group order, then the hash is truncated
146 * as defined in <em>Standards for Efficient Cryptography Group
147 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
148 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000149 *
Rose Zadik817297f2018-03-27 11:30:14 +0100150 * \see ecp.h
151 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 * \param grp The context for the elliptic curve to use.
153 * This must be initialized and have group parameters
154 * set, for example through mbedtls_ecp_group_load().
155 * \param r The MPI context in which to store the first part
156 * the signature. This must be initialized.
157 * \param s The MPI context in which to store the second part
158 * the signature. This must be initialized.
159 * \param d The private signing key. This must be initialized.
160 * \param buf The content to be signed. This is usually the hash of
161 * the original data to be signed. This must be a readable
162 * buffer of length \p blen Bytes. It may be \c NULL if
163 * \p blen is zero.
164 * \param blen The length of \p buf in Bytes.
165 * \param f_rng The RNG function. This must not be \c NULL.
166 * \param p_rng The RNG context to be passed to \p f_rng. This may be
167 * \c NULL if \p f_rng doesn't need a context parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100168 *
Rose Zadik817297f2018-03-27 11:30:14 +0100169 * \return \c 0 on success.
170 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000171 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100172 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
174 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
175 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Janos Follathdca667a2019-01-04 14:32:30 +0000178/**
179 * \brief This function computes the ECDSA signature of a
180 * previously-hashed message, deterministic version.
181 *
182 * For more information, see <em>RFC-6979: Deterministic
183 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
184 * Curve Digital Signature Algorithm (ECDSA)</em>.
185 *
186 * \note If the bitlength of the message hash is larger than the
187 * bitlength of the group order, then the hash is truncated as
188 * defined in <em>Standards for Efficient Cryptography Group
189 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
190 * 4.1.3, step 5.
191 *
192 * \see ecp.h
193 *
194 * \param grp The context for the elliptic curve to use.
195 * This must be initialized and have group parameters
196 * set, for example through mbedtls_ecp_group_load().
197 * \param r The MPI context in which to store the first part
198 * the signature. This must be initialized.
199 * \param s The MPI context in which to store the second part
200 * the signature. This must be initialized.
201 * \param d The private signing key. This must be initialized
202 * and setup, for example through mbedtls_ecp_gen_privkey().
203 * \param buf The hashed content to be signed. This must be a readable
204 * buffer of length \p blen Bytes. It may be \c NULL if
205 * \p blen is zero.
206 * \param blen The length of \p buf in Bytes.
207 * \param md_alg The hash algorithm used to hash the original data.
208 * \param f_rng_blind The RNG function used for blinding. This must not be
209 * \c NULL.
210 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
211 * \c NULL if \p f_rng doesn't need a context parameter.
212 *
213 * \return \c 0 on success.
214 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
215 * error code on failure.
216 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
218 mbedtls_mpi *s, const mbedtls_mpi *d,
219 const unsigned char *buf, size_t blen,
220 mbedtls_md_type_t md_alg,
221 int (*f_rng_blind)(void *, unsigned char *, size_t),
222 void *p_rng_blind);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100224
Paul Elliott2ba002c2022-12-09 18:59:26 +0000225#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
226/**
227 * \brief This function computes the ECDSA signature of a
228 * previously-hashed message, in a restartable way.
229 *
230 * \note The deterministic version implemented in
231 * mbedtls_ecdsa_sign_det_restartable() is usually
232 * preferred.
233 *
234 * \note This function is like \c mbedtls_ecdsa_sign() but
235 * it can return early and restart according to the
236 * limit set with \c mbedtls_ecp_set_max_ops() to
237 * reduce blocking.
238 *
239 * \note If the bitlength of the message hash is larger
240 * than the bitlength of the group order, then the
241 * hash is truncated as defined in <em>Standards for
242 * Efficient Cryptography Group (SECG): SEC1 Elliptic
243 * Curve Cryptography</em>, section 4.1.3, step 5.
244 *
245 * \see ecp.h
246 *
247 * \param grp The context for the elliptic curve to use.
248 * This must be initialized and have group parameters
249 * set, for example through mbedtls_ecp_group_load().
250 * \param r The MPI context in which to store the first part
251 * the signature. This must be initialized.
252 * \param s The MPI context in which to store the second part
253 * the signature. This must be initialized.
254 * \param d The private signing key. This must be initialized
255 * and setup, for example through
256 * mbedtls_ecp_gen_privkey().
257 * \param buf The hashed content to be signed. This must be a readable
258 * buffer of length \p blen Bytes. It may be \c NULL if
259 * \p blen is zero.
260 * \param blen The length of \p buf in Bytes.
261 * \param f_rng The RNG function. This must not be \c NULL.
262 * \param p_rng The RNG context to be passed to \p f_rng. This may be
263 * \c NULL if \p f_rng doesn't need a context parameter.
264 * \param f_rng_blind The RNG function used for blinding. This must not be
265 * \c NULL.
266 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
267 * \c NULL if \p f_rng doesn't need a context parameter.
268 * \param rs_ctx The restart context to use. This may be \c NULL
269 * to disable restarting. If it is not \c NULL, it
270 * must point to an initialized restart context.
271 *
272 * \return \c 0 on success.
273 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
274 * operations was reached: see \c
275 * mbedtls_ecp_set_max_ops().
276 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c
277 * MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
278 * error code on failure.
279 */
280int mbedtls_ecdsa_sign_restartable(
281 mbedtls_ecp_group *grp,
282 mbedtls_mpi *r, mbedtls_mpi *s,
283 const mbedtls_mpi *d,
284 const unsigned char *buf, size_t blen,
285 int (*f_rng)(void *, unsigned char *, size_t),
286 void *p_rng,
287 int (*f_rng_blind)(void *, unsigned char *, size_t),
288 void *p_rng_blind,
289 mbedtls_ecdsa_restart_ctx *rs_ctx);
290
harshal.patil8c776442023-04-07 16:17:16 +0530291#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
292
Paul Elliott2ba002c2022-12-09 18:59:26 +0000293#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
294
295/**
296 * \brief This function computes the ECDSA signature of a
297 * previously-hashed message, in a restartable way.
298 *
299 * \note This function is like \c
300 * mbedtls_ecdsa_sign_det_ext() but it can return
301 * early and restart according to the limit set with
302 * \c mbedtls_ecp_set_max_ops() to reduce blocking.
303 *
304 * \note If the bitlength of the message hash is larger
305 * than the bitlength of the group order, then the
306 * hash is truncated as defined in <em>Standards for
307 * Efficient Cryptography Group (SECG): SEC1 Elliptic
308 * Curve Cryptography</em>, section 4.1.3, step 5.
309 *
310 * \see ecp.h
311 *
312 * \param grp The context for the elliptic curve to use.
313 * This must be initialized and have group parameters
314 * set, for example through mbedtls_ecp_group_load().
315 * \param r The MPI context in which to store the first part
316 * the signature. This must be initialized.
317 * \param s The MPI context in which to store the second part
318 * the signature. This must be initialized.
319 * \param d The private signing key. This must be initialized
320 * and setup, for example through
321 * mbedtls_ecp_gen_privkey().
322 * \param buf The hashed content to be signed. This must be a readable
323 * buffer of length \p blen Bytes. It may be \c NULL if
324 * \p blen is zero.
325 * \param blen The length of \p buf in Bytes.
harshal.patil8c776442023-04-07 16:17:16 +0530326 * \param md_alg The hash algorithm used to hash the original data.
Paul Elliott2ba002c2022-12-09 18:59:26 +0000327 * \param f_rng_blind The RNG function used for blinding. This must not be
328 * \c NULL.
329 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
330 * \c NULL if \p f_rng doesn't need a context parameter.
331 * \param rs_ctx The restart context to use. This may be \c NULL
332 * to disable restarting. If it is not \c NULL, it
333 * must point to an initialized restart context.
334 *
335 * \return \c 0 on success.
336 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
337 * operations was reached: see \c
338 * mbedtls_ecp_set_max_ops().
339 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c
340 * MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
341 * error code on failure.
342 */
343int mbedtls_ecdsa_sign_det_restartable(
344 mbedtls_ecp_group *grp,
345 mbedtls_mpi *r, mbedtls_mpi *s,
346 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
347 mbedtls_md_type_t md_alg,
348 int (*f_rng_blind)(void *, unsigned char *, size_t),
349 void *p_rng_blind,
350 mbedtls_ecdsa_restart_ctx *rs_ctx);
351
352#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
353
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100354/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000355 * \brief This function verifies the ECDSA signature of a
356 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100357 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000358 * \note If the bitlength of the message hash is larger than the
359 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100360 * defined in <em>Standards for Efficient Cryptography Group
361 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
362 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000363 *
Rose Zadik817297f2018-03-27 11:30:14 +0100364 * \see ecp.h
365 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500366 * \param grp The ECP group to use.
367 * This must be initialized and have group parameters
368 * set, for example through mbedtls_ecp_group_load().
369 * \param buf The hashed content that was signed. This must be a readable
370 * buffer of length \p blen Bytes. It may be \c NULL if
371 * \p blen is zero.
372 * \param blen The length of \p buf in Bytes.
373 * \param Q The public key to use for verification. This must be
374 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000375 * \param r The first integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000377 * \param s The second integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100379 *
Rose Zadik817297f2018-03-27 11:30:14 +0100380 * \return \c 0 on success.
Rose Zadik817297f2018-03-27 11:30:14 +0100381 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Dave Rodgman34ff6a72022-08-19 11:08:07 +0100382 * error code on failure.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
385 const unsigned char *buf, size_t blen,
386 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
387 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100388
Paul Elliott2ba002c2022-12-09 18:59:26 +0000389#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
390/**
391 * \brief This function verifies the ECDSA signature of a
392 * previously-hashed message, in a restartable manner
393 *
394 * \note If the bitlength of the message hash is larger than the
395 * bitlength of the group order, then the hash is truncated as
396 * defined in <em>Standards for Efficient Cryptography Group
397 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
398 * 4.1.4, step 3.
399 *
400 * \see ecp.h
401 *
402 * \param grp The ECP group to use.
403 * This must be initialized and have group parameters
404 * set, for example through mbedtls_ecp_group_load().
405 * \param buf The hashed content that was signed. This must be a readable
406 * buffer of length \p blen Bytes. It may be \c NULL if
407 * \p blen is zero.
408 * \param blen The length of \p buf in Bytes.
409 * \param Q The public key to use for verification. This must be
410 * initialized and setup.
411 * \param r The first integer of the signature.
412 * This must be initialized.
413 * \param s The second integer of the signature.
414 * This must be initialized.
415 * \param rs_ctx The restart context to use. This may be \c NULL to disable
416 * restarting. If it is not \c NULL, it must point to an
417 * initialized restart context.
418 *
419 * \return \c 0 on success.
Paul Elliott3225f192023-01-10 12:03:12 +0000420 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
421 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Paul Elliott2ba002c2022-12-09 18:59:26 +0000422 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
423 * error code on failure.
424 */
425int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
426 const unsigned char *buf, size_t blen,
427 const mbedtls_ecp_point *Q,
428 const mbedtls_mpi *r,
429 const mbedtls_mpi *s,
430 mbedtls_ecdsa_restart_ctx *rs_ctx);
431
432#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
433
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100434/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000435 * \brief This function computes the ECDSA signature and writes it
436 * to a buffer, serialized as defined in <em>RFC-4492:
437 * Elliptic Curve Cryptography (ECC) Cipher Suites for
438 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200439 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000440 * \warning It is not thread-safe to use the same context in
441 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200442 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000443 * \note The deterministic version is used if
444 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
445 * information, see <em>RFC-6979: Deterministic Usage
446 * of the Digital Signature Algorithm (DSA) and Elliptic
447 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200448 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000449 * \note If the bitlength of the message hash is larger than the
450 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000451 * defined in <em>Standards for Efficient Cryptography Group
452 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
453 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000454 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000455 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100456 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500457 * \param ctx The ECDSA context to use. This must be initialized
458 * and have a group and private key bound to it, for example
459 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100460 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500461 * \param hash The message hash to be signed. This must be a readable
462 * buffer of length \p blen Bytes.
463 * \param hlen The length of the hash \p hash in Bytes.
464 * \param sig The buffer to which to write the signature. This must be a
465 * writable buffer of length at least twice as large as the
466 * size of the curve used, plus 9. For example, 73 Bytes if
467 * a 256-bit curve is used. A buffer length of
468 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200469 * \param sig_size The size of the \p sig buffer in bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470 * \param slen The address at which to store the actual length of
471 * the signature written. Must not be \c NULL.
472 * \param f_rng The RNG function. This must not be \c NULL if
473 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
Janos Follathe65e0592019-01-04 15:55:43 +0000474 * it is used only for blinding and may be set to \c NULL, but
475 * doing so is DEPRECATED.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 * \param p_rng The RNG context to be passed to \p f_rng. This may be
477 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100478 *
479 * \return \c 0 on success.
480 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
481 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
484 mbedtls_md_type_t md_alg,
485 const unsigned char *hash, size_t hlen,
486 unsigned char *sig, size_t sig_size, size_t *slen,
487 int (*f_rng)(void *, unsigned char *, size_t),
488 void *p_rng);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200489
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200490/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200491 * \brief This function computes the ECDSA signature and writes it
492 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200493 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200494 * \see \c mbedtls_ecdsa_write_signature()
495 *
496 * \note This function is like \c mbedtls_ecdsa_write_signature()
497 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200498 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
499 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500500 * \param ctx The ECDSA context to use. This must be initialized
501 * and have a group and private key bound to it, for example
502 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200503 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500504 * \param hash The message hash to be signed. This must be a readable
505 * buffer of length \p blen Bytes.
506 * \param hlen The length of the hash \p hash in Bytes.
507 * \param sig The buffer to which to write the signature. This must be a
508 * writable buffer of length at least twice as large as the
509 * size of the curve used, plus 9. For example, 73 Bytes if
510 * a 256-bit curve is used. A buffer length of
511 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200512 * \param sig_size The size of the \p sig buffer in bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500513 * \param slen The address at which to store the actual length of
514 * the signature written. Must not be \c NULL.
515 * \param f_rng The RNG function. This must not be \c NULL if
516 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
517 * it is unused and may be set to \c NULL.
518 * \param p_rng The RNG context to be passed to \p f_rng. This may be
519 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
520 * \param rs_ctx The restart context to use. This may be \c NULL to disable
521 * restarting. If it is not \c NULL, it must point to an
522 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200523 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200524 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200525 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200526 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200527 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
528 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200529 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
531 mbedtls_md_type_t md_alg,
532 const unsigned char *hash, size_t hlen,
533 unsigned char *sig, size_t sig_size, size_t *slen,
534 int (*f_rng)(void *, unsigned char *, size_t),
535 void *p_rng,
536 mbedtls_ecdsa_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200537
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200538/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000539 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200540 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000541 * \note If the bitlength of the message hash is larger than the
542 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000543 * defined in <em>Standards for Efficient Cryptography Group
544 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
545 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000546 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000547 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100548 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500549 * \param ctx The ECDSA context to use. This must be initialized
550 * and have a group and public key bound to it.
551 * \param hash The message hash that was signed. This must be a readable
552 * buffer of length \p size Bytes.
553 * \param hlen The size of the hash \p hash.
554 * \param sig The signature to read and verify. This must be a readable
555 * buffer of length \p slen Bytes.
556 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100557 *
558 * \return \c 0 on success.
559 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100560 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
561 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100562 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
563 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200564 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100565int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
566 const unsigned char *hash, size_t hlen,
567 const unsigned char *sig, size_t slen);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200568
569/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200570 * \brief This function reads and verifies an ECDSA signature,
571 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200572 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200573 * \see \c mbedtls_ecdsa_read_signature()
574 *
575 * \note This function is like \c mbedtls_ecdsa_read_signature()
576 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200577 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
578 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500579 * \param ctx The ECDSA context to use. This must be initialized
580 * and have a group and public key bound to it.
581 * \param hash The message hash that was signed. This must be a readable
582 * buffer of length \p size Bytes.
583 * \param hlen The size of the hash \p hash.
584 * \param sig The signature to read and verify. This must be a readable
585 * buffer of length \p slen Bytes.
586 * \param slen The size of \p sig in Bytes.
587 * \param rs_ctx The restart context to use. This may be \c NULL to disable
588 * restarting. If it is not \c NULL, it must point to an
589 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200590 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200591 * \return \c 0 on success.
592 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
593 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
594 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200595 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200596 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200597 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
598 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200599 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
601 const unsigned char *hash, size_t hlen,
602 const unsigned char *sig, size_t slen,
603 mbedtls_ecdsa_restart_ctx *rs_ctx);
Christoph M. Wintersteigeref17e3b2019-02-15 12:52:09 +0000604
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200605/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000606 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200607 *
Rose Zadik817297f2018-03-27 11:30:14 +0100608 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200609 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000610 * \param ctx The ECDSA context to store the keypair in.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500611 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000612 * \param gid The elliptic curve to use. One of the various
613 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500614 * \param f_rng The RNG function to use. This must not be \c NULL.
615 * \param p_rng The RNG context to be passed to \p f_rng. This may be
616 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200617 *
Rose Zadik817297f2018-03-27 11:30:14 +0100618 * \return \c 0 on success.
619 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200620 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
622 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200623
624/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500625 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200626 *
Rose Zadik817297f2018-03-27 11:30:14 +0100627 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200628 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500629 * \param ctx The ECDSA context to setup. This must be initialized.
630 * \param key The EC key to use. This must be initialized and hold
631 * a private-public key pair or a public key. In the former
632 * case, the ECDSA context may be used for signature creation
633 * and verification after this call. In the latter case, it
634 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200635 *
Rose Zadik817297f2018-03-27 11:30:14 +0100636 * \return \c 0 on success.
637 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx,
640 const mbedtls_ecp_keypair *key);
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200641
642/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000643 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200644 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000645 * \param ctx The ECDSA context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500646 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200649
650/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000651 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200652 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500653 * \param ctx The ECDSA context to free. This may be \c NULL,
654 * in which case this function does nothing. If it
655 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200656 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200658
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200659#if defined(MBEDTLS_ECP_RESTARTABLE)
660/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500661 * \brief Initialize a restart context.
662 *
663 * \param ctx The restart context to initialize.
664 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200667
668/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500669 * \brief Free the components of a restart context.
670 *
671 * \param ctx The restart context to free. This may be \c NULL,
672 * in which case this function does nothing. If it
673 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200674 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200676#endif /* MBEDTLS_ECP_RESTARTABLE */
677
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100678#ifdef __cplusplus
679}
680#endif
681
Paul Bakker9af723c2014-05-01 13:03:14 +0200682#endif /* ecdsa.h */