blob: f5f2207584e477219193a55c683ba98325fed560 [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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056#define MBEDTLS_ECDSA_MAX_SIG_LEN(bits) \
57 (/*T,L of SEQUENCE*/ ((bits) >= 61 * 8 ? 3 : 2) + \
58 /*T,L of r,s*/ 2 * \
59 (((bits) >= 127 * 8 ? 3 : 2) + /*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. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062#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.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020076
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020077#if defined(MBEDTLS_ECP_RESTARTABLE)
78
79/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020080 * \brief Internal restart context for ecdsa_verify()
81 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020082 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020083 */
84typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
85
86/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020087 * \brief Internal restart context for ecdsa_sign()
88 *
89 * \note Opaque struct, defined in ecdsa.c
90 */
91typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
92
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020093# if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020094/**
95 * \brief Internal restart context for ecdsa_sign_det()
96 *
97 * \note Opaque struct, defined in ecdsa.c
98 */
99typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100# endif
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200101
102/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200103 * \brief General context for resuming ECDSA operations
104 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105typedef struct {
106 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(ecp); /*!< base context for ECP
107 restart and shared administrative info */
108 mbedtls_ecdsa_restart_ver_ctx *MBEDTLS_PRIVATE(ver); /*!< ecdsa_verify()
109 sub-context */
110 mbedtls_ecdsa_restart_sig_ctx *MBEDTLS_PRIVATE(sig); /*!< ecdsa_sign()
111 sub-context */
112# if defined(MBEDTLS_ECDSA_DETERMINISTIC)
113 mbedtls_ecdsa_restart_det_ctx *MBEDTLS_PRIVATE(det); /*!< ecdsa_sign_det()
114 sub-context */
115# endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200116} mbedtls_ecdsa_restart_ctx;
117
118#else /* MBEDTLS_ECP_RESTARTABLE */
119
120/* Now we can declare functions that take a pointer to that */
121typedef void mbedtls_ecdsa_restart_ctx;
122
123#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100124
125/**
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000126 * \brief This function checks whether a given group can be used
127 * for ECDSA.
128 *
129 * \param gid The ECP group ID to check.
130 *
131 * \return \c 1 if the group can be used, \c 0 otherwise
132 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid);
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000134
135/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000136 * \brief This function computes the ECDSA signature of a
137 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100138 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500139 * \note The deterministic version implemented in
TRodziewicz18efb732021-04-29 23:12:19 +0200140 * mbedtls_ecdsa_sign_det_ext() is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100141 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000142 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100143 * bitlength of the group order, then the hash is truncated
144 * as defined in <em>Standards for Efficient Cryptography Group
145 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
146 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000147 *
Rose Zadik817297f2018-03-27 11:30:14 +0100148 * \see ecp.h
149 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500150 * \param grp The context for the elliptic curve to use.
151 * This must be initialized and have group parameters
152 * set, for example through mbedtls_ecp_group_load().
153 * \param r The MPI context in which to store the first part
154 * the signature. This must be initialized.
155 * \param s The MPI context in which to store the second part
156 * the signature. This must be initialized.
157 * \param d The private signing key. This must be initialized.
158 * \param buf The content to be signed. This is usually the hash of
159 * the original data to be signed. This must be a readable
160 * buffer of length \p blen Bytes. It may be \c NULL if
161 * \p blen is zero.
162 * \param blen The length of \p buf in Bytes.
163 * \param f_rng The RNG function. This must not be \c NULL.
164 * \param p_rng The RNG context to be passed to \p f_rng. This may be
165 * \c NULL if \p f_rng doesn't need a context parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100166 *
Rose Zadik817297f2018-03-27 11:30:14 +0100167 * \return \c 0 on success.
168 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000169 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100170 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200171int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp,
172 mbedtls_mpi *r,
173 mbedtls_mpi *s,
174 const mbedtls_mpi *d,
175 const unsigned char *buf,
176 size_t blen,
177 int (*f_rng)(void *, unsigned char *, size_t),
178 void *p_rng);
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Janos Follathdca667a2019-01-04 14:32:30 +0000181/**
182 * \brief This function computes the ECDSA signature of a
183 * previously-hashed message, deterministic version.
184 *
185 * For more information, see <em>RFC-6979: Deterministic
186 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
187 * Curve Digital Signature Algorithm (ECDSA)</em>.
188 *
189 * \note If the bitlength of the message hash is larger than the
190 * bitlength of the group order, then the hash is truncated as
191 * defined in <em>Standards for Efficient Cryptography Group
192 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
193 * 4.1.3, step 5.
194 *
195 * \see ecp.h
196 *
197 * \param grp The context for the elliptic curve to use.
198 * This must be initialized and have group parameters
199 * set, for example through mbedtls_ecp_group_load().
200 * \param r The MPI context in which to store the first part
201 * the signature. This must be initialized.
202 * \param s The MPI context in which to store the second part
203 * the signature. This must be initialized.
204 * \param d The private signing key. This must be initialized
205 * and setup, for example through mbedtls_ecp_gen_privkey().
206 * \param buf The hashed content to be signed. This must be a readable
207 * buffer of length \p blen Bytes. It may be \c NULL if
208 * \p blen is zero.
209 * \param blen The length of \p buf in Bytes.
210 * \param md_alg The hash algorithm used to hash the original data.
211 * \param f_rng_blind The RNG function used for blinding. This must not be
212 * \c NULL.
213 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
214 * \c NULL if \p f_rng doesn't need a context parameter.
215 *
216 * \return \c 0 on success.
217 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
218 * error code on failure.
219 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200220int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp,
221 mbedtls_mpi *r,
222 mbedtls_mpi *s,
223 const mbedtls_mpi *d,
224 const unsigned char *buf,
225 size_t blen,
226 mbedtls_md_type_t md_alg,
227 int (*f_rng_blind)(void *,
228 unsigned char *,
229 size_t),
230 void *p_rng_blind);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100232
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100233/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000234 * \brief This function verifies the ECDSA signature of a
235 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100236 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000237 * \note If the bitlength of the message hash is larger than the
238 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100239 * defined in <em>Standards for Efficient Cryptography Group
240 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
241 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000242 *
Rose Zadik817297f2018-03-27 11:30:14 +0100243 * \see ecp.h
244 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500245 * \param grp The ECP group to use.
246 * This must be initialized and have group parameters
247 * set, for example through mbedtls_ecp_group_load().
248 * \param buf The hashed content that was signed. This must be a readable
249 * buffer of length \p blen Bytes. It may be \c NULL if
250 * \p blen is zero.
251 * \param blen The length of \p buf in Bytes.
252 * \param Q The public key to use for verification. This must be
253 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000254 * \param r The first integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000256 * \param s The second integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100258 *
Rose Zadik817297f2018-03-27 11:30:14 +0100259 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100260 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
261 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100262 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000263 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100264 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
266 const unsigned char *buf,
267 size_t blen,
268 const mbedtls_ecp_point *Q,
269 const mbedtls_mpi *r,
270 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100271
272/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000273 * \brief This function computes the ECDSA signature and writes it
274 * to a buffer, serialized as defined in <em>RFC-4492:
275 * Elliptic Curve Cryptography (ECC) Cipher Suites for
276 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200277 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000278 * \warning It is not thread-safe to use the same context in
279 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200280 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000281 * \note The deterministic version is used if
282 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
283 * information, see <em>RFC-6979: Deterministic Usage
284 * of the Digital Signature Algorithm (DSA) and Elliptic
285 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200286 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000287 * \note If the bitlength of the message hash is larger than the
288 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000289 * defined in <em>Standards for Efficient Cryptography Group
290 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
291 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000292 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000293 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100294 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 * \param ctx The ECDSA context to use. This must be initialized
296 * and have a group and private key bound to it, for example
297 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100298 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299 * \param hash The message hash to be signed. This must be a readable
300 * buffer of length \p blen Bytes.
301 * \param hlen The length of the hash \p hash in Bytes.
302 * \param sig The buffer to which to write the signature. This must be a
303 * writable buffer of length at least twice as large as the
304 * size of the curve used, plus 9. For example, 73 Bytes if
305 * a 256-bit curve is used. A buffer length of
306 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200307 * \param sig_size The size of the \p sig buffer in bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308 * \param slen The address at which to store the actual length of
309 * the signature written. Must not be \c NULL.
310 * \param f_rng The RNG function. This must not be \c NULL if
311 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
Janos Follathe65e0592019-01-04 15:55:43 +0000312 * it is used only for blinding and may be set to \c NULL, but
313 * doing so is DEPRECATED.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500314 * \param p_rng The RNG context to be passed to \p f_rng. This may be
315 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100316 *
317 * \return \c 0 on success.
318 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
319 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200320 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
322 mbedtls_md_type_t md_alg,
323 const unsigned char *hash,
324 size_t hlen,
325 unsigned char *sig,
326 size_t sig_size,
327 size_t *slen,
328 int (*f_rng)(void *, unsigned char *, size_t),
329 void *p_rng);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200330
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200331/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200332 * \brief This function computes the ECDSA signature and writes it
333 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200334 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200335 * \see \c mbedtls_ecdsa_write_signature()
336 *
337 * \note This function is like \c mbedtls_ecdsa_write_signature()
338 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200339 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
340 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341 * \param ctx The ECDSA context to use. This must be initialized
342 * and have a group and private key bound to it, for example
343 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200344 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 * \param hash The message hash to be signed. This must be a readable
346 * buffer of length \p blen Bytes.
347 * \param hlen The length of the hash \p hash in Bytes.
348 * \param sig The buffer to which to write the signature. This must be a
349 * writable buffer of length at least twice as large as the
350 * size of the curve used, plus 9. For example, 73 Bytes if
351 * a 256-bit curve is used. A buffer length of
352 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200353 * \param sig_size The size of the \p sig buffer in bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500354 * \param slen The address at which to store the actual length of
355 * the signature written. Must not be \c NULL.
356 * \param f_rng The RNG function. This must not be \c NULL if
357 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
358 * it is unused and may be set to \c NULL.
359 * \param p_rng The RNG context to be passed to \p f_rng. This may be
360 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
361 * \param rs_ctx The restart context to use. This may be \c NULL to disable
362 * restarting. If it is not \c NULL, it must point to an
363 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200364 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200365 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200366 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200367 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200368 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
369 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200370 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371int mbedtls_ecdsa_write_signature_restartable(
372 mbedtls_ecdsa_context *ctx,
373 mbedtls_md_type_t md_alg,
374 const unsigned char *hash,
375 size_t hlen,
376 unsigned char *sig,
377 size_t sig_size,
378 size_t *slen,
379 int (*f_rng)(void *, unsigned char *, size_t),
380 void *p_rng,
381 mbedtls_ecdsa_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200382
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200383/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000384 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200385 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000386 * \note If the bitlength of the message hash is larger than the
387 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000388 * defined in <em>Standards for Efficient Cryptography Group
389 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
390 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000391 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000392 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100393 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500394 * \param ctx The ECDSA context to use. This must be initialized
395 * and have a group and public key bound to it.
396 * \param hash The message hash that was signed. This must be a readable
397 * buffer of length \p size Bytes.
398 * \param hlen The size of the hash \p hash.
399 * \param sig The signature to read and verify. This must be a readable
400 * buffer of length \p slen Bytes.
401 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100402 *
403 * \return \c 0 on success.
404 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100405 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
406 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100407 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
408 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200409 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200410int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
411 const unsigned char *hash,
412 size_t hlen,
413 const unsigned char *sig,
414 size_t slen);
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200415
416/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200417 * \brief This function reads and verifies an ECDSA signature,
418 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200419 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200420 * \see \c mbedtls_ecdsa_read_signature()
421 *
422 * \note This function is like \c mbedtls_ecdsa_read_signature()
423 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200424 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
425 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426 * \param ctx The ECDSA context to use. This must be initialized
427 * and have a group and public key bound to it.
428 * \param hash The message hash that was signed. This must be a readable
429 * buffer of length \p size Bytes.
430 * \param hlen The size of the hash \p hash.
431 * \param sig The signature to read and verify. This must be a readable
432 * buffer of length \p slen Bytes.
433 * \param slen The size of \p sig in Bytes.
434 * \param rs_ctx The restart context to use. This may be \c NULL to disable
435 * restarting. If it is not \c NULL, it must point to an
436 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200437 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200438 * \return \c 0 on success.
439 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
440 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
441 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200442 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200443 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200444 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
445 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200446 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
448 const unsigned char *hash,
449 size_t hlen,
450 const unsigned char *sig,
451 size_t slen,
452 mbedtls_ecdsa_restart_ctx *rs_ctx);
Christoph M. Wintersteigeref17e3b2019-02-15 12:52:09 +0000453
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200454/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000455 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200456 *
Rose Zadik817297f2018-03-27 11:30:14 +0100457 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200458 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000459 * \param ctx The ECDSA context to store the keypair in.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500460 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000461 * \param gid The elliptic curve to use. One of the various
462 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500463 * \param f_rng The RNG function to use. This must not be \c NULL.
464 * \param p_rng The RNG context to be passed to \p f_rng. This may be
465 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200466 *
Rose Zadik817297f2018-03-27 11:30:14 +0100467 * \return \c 0 on success.
468 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200469 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200470int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx,
471 mbedtls_ecp_group_id gid,
472 int (*f_rng)(void *, unsigned char *, size_t),
473 void *p_rng);
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200474
475/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200477 *
Rose Zadik817297f2018-03-27 11:30:14 +0100478 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200479 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 * \param ctx The ECDSA context to setup. This must be initialized.
481 * \param key The EC key to use. This must be initialized and hold
482 * a private-public key pair or a public key. In the former
483 * case, the ECDSA context may be used for signature creation
484 * and verification after this call. In the latter case, it
485 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200486 *
Rose Zadik817297f2018-03-27 11:30:14 +0100487 * \return \c 0 on success.
488 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200489 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200490int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx,
491 const mbedtls_ecp_keypair *key);
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200492
493/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000494 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200495 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000496 * \param ctx The ECDSA context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500497 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200498 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200499void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200500
501/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000502 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200503 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500504 * \param ctx The ECDSA context to free. This may be \c NULL,
505 * in which case this function does nothing. If it
506 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200507 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200508void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx);
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200509
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200510#if defined(MBEDTLS_ECP_RESTARTABLE)
511/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500512 * \brief Initialize a restart context.
513 *
514 * \param ctx The restart context to initialize.
515 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200516 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200517void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200518
519/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500520 * \brief Free the components of a restart context.
521 *
522 * \param ctx The restart context to free. This may be \c NULL,
523 * in which case this function does nothing. If it
524 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200525 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200526void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx);
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200527#endif /* MBEDTLS_ECP_RESTARTABLE */
528
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100529#ifdef __cplusplus
530}
531#endif
532
Paul Bakker9af723c2014-05-01 13:03:14 +0200533#endif /* ecdsa.h */