blob: 735d37764fe68653b61c2d92cdc1f2f04a6af82b [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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050034#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010035#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050036#else
37#include MBEDTLS_CONFIG_FILE
38#endif
39
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010040#include "mbedtls/ecp.h"
41#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010042
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010043/**
44 * \brief Maximum ECDSA signature size for a given curve bit size
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020045 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010046 * \param bits Curve size in bits
47 * \return Maximum signature size in bytes
48 *
49 * \note This macro returns a compile-time constant if its argument
50 * is one. It may evaluate its argument multiple times.
51 */
52/*
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020053 * Ecdsa-Sig-Value ::= SEQUENCE {
54 * r INTEGER,
55 * s INTEGER
56 * }
57 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010058 * 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 +020059 */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010060#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
61 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
62 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
63 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
64
Rose Zadikbff87d92018-01-25 21:58:53 +000065/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010066#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020067
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020068#ifdef __cplusplus
69extern "C" {
70#endif
71
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020072/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020073 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020074 *
75 * \warning Performing multiple operations concurrently on the same
76 * ECDSA context is not supported; objects of this type
77 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020080
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020081#if defined(MBEDTLS_ECP_RESTARTABLE)
82
83/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020084 * \brief Internal restart context for ecdsa_verify()
85 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020086 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020087 */
88typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
89
90/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020091 * \brief Internal restart context for ecdsa_sign()
92 *
93 * \note Opaque struct, defined in ecdsa.c
94 */
95typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
96
97#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
98/**
99 * \brief Internal restart context for ecdsa_sign_det()
100 *
101 * \note Opaque struct, defined in ecdsa.c
102 */
103typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
104#endif
105
106/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200107 * \brief General context for resuming ECDSA operations
108 */
109typedef struct
110{
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200111 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(ecp); /*!< base context for ECP restart and
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200112 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 */
135int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid );
136
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int 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,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100175 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
176
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 */
217int 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
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100225/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000226 * \brief This function verifies the ECDSA signature of a
227 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100228 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000229 * \note If the bitlength of the message hash is larger than the
230 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100231 * defined in <em>Standards for Efficient Cryptography Group
232 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
233 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000234 *
Rose Zadik817297f2018-03-27 11:30:14 +0100235 * \see ecp.h
236 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 * \param grp The ECP group to use.
238 * This must be initialized and have group parameters
239 * set, for example through mbedtls_ecp_group_load().
240 * \param buf The hashed content that was signed. This must be a readable
241 * buffer of length \p blen Bytes. It may be \c NULL if
242 * \p blen is zero.
243 * \param blen The length of \p buf in Bytes.
244 * \param Q The public key to use for verification. This must be
245 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000246 * \param r The first integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500247 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000248 * \param s The second integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500249 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100250 *
Rose Zadik817297f2018-03-27 11:30:14 +0100251 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100252 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
253 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100254 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000255 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500258 const unsigned char *buf, size_t blen,
259 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
260 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100261
262/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000263 * \brief This function computes the ECDSA signature and writes it
264 * to a buffer, serialized as defined in <em>RFC-4492:
265 * Elliptic Curve Cryptography (ECC) Cipher Suites for
266 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200267 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000268 * \warning It is not thread-safe to use the same context in
269 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200270 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000271 * \note The deterministic version is used if
272 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
273 * information, see <em>RFC-6979: Deterministic Usage
274 * of the Digital Signature Algorithm (DSA) and Elliptic
275 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200276 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000277 * \note If the bitlength of the message hash is larger than the
278 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000279 * defined in <em>Standards for Efficient Cryptography Group
280 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
281 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000282 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000283 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100284 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 * \param ctx The ECDSA context to use. This must be initialized
286 * and have a group and private key bound to it, for example
287 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100288 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500289 * \param hash The message hash to be signed. This must be a readable
290 * buffer of length \p blen Bytes.
291 * \param hlen The length of the hash \p hash in Bytes.
292 * \param sig The buffer to which to write the signature. This must be a
293 * writable buffer of length at least twice as large as the
294 * size of the curve used, plus 9. For example, 73 Bytes if
295 * a 256-bit curve is used. A buffer length of
296 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
297 * \param slen The address at which to store the actual length of
298 * the signature written. Must not be \c NULL.
299 * \param f_rng The RNG function. This must not be \c NULL if
300 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
Janos Follathe65e0592019-01-04 15:55:43 +0000301 * it is used only for blinding and may be set to \c NULL, but
302 * doing so is DEPRECATED.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303 * \param p_rng The RNG context to be passed to \p f_rng. This may be
304 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100305 *
306 * \return \c 0 on success.
307 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
308 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200309 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500310int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
311 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200312 const unsigned char *hash, size_t hlen,
313 unsigned char *sig, size_t *slen,
314 int (*f_rng)(void *, unsigned char *, size_t),
315 void *p_rng );
316
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200317/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200318 * \brief This function computes the ECDSA signature and writes it
319 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200320 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200321 * \see \c mbedtls_ecdsa_write_signature()
322 *
323 * \note This function is like \c mbedtls_ecdsa_write_signature()
324 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200325 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
326 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327 * \param ctx The ECDSA context to use. This must be initialized
328 * and have a group and private key bound to it, for example
329 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200330 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500331 * \param hash The message hash to be signed. This must be a readable
332 * buffer of length \p blen Bytes.
333 * \param hlen The length of the hash \p hash in Bytes.
334 * \param sig The buffer to which to write the signature. This must be a
335 * writable buffer of length at least twice as large as the
336 * size of the curve used, plus 9. For example, 73 Bytes if
337 * a 256-bit curve is used. A buffer length of
338 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
339 * \param slen The address at which to store the actual length of
340 * the signature written. Must not be \c NULL.
341 * \param f_rng The RNG function. This must not be \c NULL if
342 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
343 * it is unused and may be set to \c NULL.
344 * \param p_rng The RNG context to be passed to \p f_rng. This may be
345 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
346 * \param rs_ctx The restart context to use. This may be \c NULL to disable
347 * restarting. If it is not \c NULL, it must point to an
348 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200349 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200350 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200351 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200352 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200353 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
354 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200355 */
356int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
357 mbedtls_md_type_t md_alg,
358 const unsigned char *hash, size_t hlen,
359 unsigned char *sig, size_t *slen,
360 int (*f_rng)(void *, unsigned char *, size_t),
361 void *p_rng,
362 mbedtls_ecdsa_restart_ctx *rs_ctx );
363
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200364/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000365 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200366 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000367 * \note If the bitlength of the message hash is larger than the
368 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000369 * defined in <em>Standards for Efficient Cryptography Group
370 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
371 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000372 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000373 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100374 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375 * \param ctx The ECDSA context to use. This must be initialized
376 * and have a group and public key bound to it.
377 * \param hash The message hash that was signed. This must be a readable
378 * buffer of length \p size Bytes.
379 * \param hlen The size of the hash \p hash.
380 * \param sig The signature to read and verify. This must be a readable
381 * buffer of length \p slen Bytes.
382 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100383 *
384 * \return \c 0 on success.
385 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100386 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
387 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100388 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
389 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200392 const unsigned char *hash, size_t hlen,
393 const unsigned char *sig, size_t slen );
394
395/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200396 * \brief This function reads and verifies an ECDSA signature,
397 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200398 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200399 * \see \c mbedtls_ecdsa_read_signature()
400 *
401 * \note This function is like \c mbedtls_ecdsa_read_signature()
402 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200403 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
404 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500405 * \param ctx The ECDSA context to use. This must be initialized
406 * and have a group and public key bound to it.
407 * \param hash The message hash that was signed. This must be a readable
408 * buffer of length \p size Bytes.
409 * \param hlen The size of the hash \p hash.
410 * \param sig The signature to read and verify. This must be a readable
411 * buffer of length \p slen Bytes.
412 * \param slen The size of \p sig in Bytes.
413 * \param rs_ctx The restart context to use. This may be \c NULL to disable
414 * restarting. If it is not \c NULL, it must point to an
415 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200416 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200417 * \return \c 0 on success.
418 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
419 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
420 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200421 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200422 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200423 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
424 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200425 */
426int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
427 const unsigned char *hash, size_t hlen,
428 const unsigned char *sig, size_t slen,
429 mbedtls_ecdsa_restart_ctx *rs_ctx );
Christoph M. Wintersteigeref17e3b2019-02-15 12:52:09 +0000430
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200431/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000432 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200433 *
Rose Zadik817297f2018-03-27 11:30:14 +0100434 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200435 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000436 * \param ctx The ECDSA context to store the keypair in.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500437 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000438 * \param gid The elliptic curve to use. One of the various
439 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500440 * \param f_rng The RNG function to use. This must not be \c NULL.
441 * \param p_rng The RNG context to be passed to \p f_rng. This may be
442 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200443 *
Rose Zadik817297f2018-03-27 11:30:14 +0100444 * \return \c 0 on success.
445 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200448 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
449
450/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500451 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200452 *
Rose Zadik817297f2018-03-27 11:30:14 +0100453 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200454 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500455 * \param ctx The ECDSA context to setup. This must be initialized.
456 * \param key The EC key to use. This must be initialized and hold
457 * a private-public key pair or a public key. In the former
458 * case, the ECDSA context may be used for signature creation
459 * and verification after this call. In the latter case, it
460 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200461 *
Rose Zadik817297f2018-03-27 11:30:14 +0100462 * \return \c 0 on success.
463 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200464 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
466 const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200467
468/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000469 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200470 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000471 * \param ctx The ECDSA context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500472 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200475
476/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000477 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200478 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500479 * \param ctx The ECDSA context to free. This may be \c NULL,
480 * in which case this function does nothing. If it
481 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200484
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200485#if defined(MBEDTLS_ECP_RESTARTABLE)
486/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500487 * \brief Initialize a restart context.
488 *
489 * \param ctx The restart context to initialize.
490 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200491 */
492void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
493
494/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495 * \brief Free the components of a restart context.
496 *
497 * \param ctx The restart context to free. This may be \c NULL,
498 * in which case this function does nothing. If it
499 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200500 */
501void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
502#endif /* MBEDTLS_ECP_RESTARTABLE */
503
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100504#ifdef __cplusplus
505}
506#endif
507
Paul Bakker9af723c2014-05-01 13:03:14 +0200508#endif /* ecdsa.h */