blob: 118f7cedb1ee45cb723c7fad54178d8e533f2d09 [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
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010032
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050033#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050035#else
36#include MBEDTLS_CONFIG_FILE
37#endif
38
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010039#include "mbedtls/ecp.h"
40#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010041
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010042/**
43 * \brief Maximum ECDSA signature size for a given curve bit size
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020044 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010045 * \param bits Curve size in bits
46 * \return Maximum signature size in bytes
47 *
48 * \note This macro returns a compile-time constant if its argument
49 * is one. It may evaluate its argument multiple times.
50 */
51/*
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020052 * Ecdsa-Sig-Value ::= SEQUENCE {
53 * r INTEGER,
54 * s INTEGER
55 * }
56 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010057 * 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 +020058 */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010059#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
60 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
61 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
62 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
63
Rose Zadikbff87d92018-01-25 21:58:53 +000064/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010065#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020066
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020067#ifdef __cplusplus
68extern "C" {
69#endif
70
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020071/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020072 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020073 *
74 * \warning Performing multiple operations concurrently on the same
75 * ECDSA context is not supported; objects of this type
76 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020079
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020080#if defined(MBEDTLS_ECP_RESTARTABLE)
81
82/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020083 * \brief Internal restart context for ecdsa_verify()
84 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020085 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020086 */
87typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
88
89/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020090 * \brief Internal restart context for ecdsa_sign()
91 *
92 * \note Opaque struct, defined in ecdsa.c
93 */
94typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
95
96#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
97/**
98 * \brief Internal restart context for ecdsa_sign_det()
99 *
100 * \note Opaque struct, defined in ecdsa.c
101 */
102typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
103#endif
104
105/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200106 * \brief General context for resuming ECDSA operations
107 */
108typedef struct
109{
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200110 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
111 shared administrative info */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200112 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200113 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
114#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
115 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
116#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200117} mbedtls_ecdsa_restart_ctx;
118
119#else /* MBEDTLS_ECP_RESTARTABLE */
120
121/* Now we can declare functions that take a pointer to that */
122typedef void mbedtls_ecdsa_restart_ctx;
123
124#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100125
126/**
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000127 * \brief This function checks whether a given group can be used
128 * for ECDSA.
129 *
130 * \param gid The ECP group ID to check.
131 *
132 * \return \c 1 if the group can be used, \c 0 otherwise
133 */
134int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid );
135
136/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000137 * \brief This function computes the ECDSA signature of a
138 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100139 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * \note The deterministic version implemented in
141 * mbedtls_ecdsa_sign_det() is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100142 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000143 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100144 * bitlength of the group order, then the hash is truncated
145 * as defined in <em>Standards for Efficient Cryptography Group
146 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
147 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000148 *
Rose Zadik817297f2018-03-27 11:30:14 +0100149 * \see ecp.h
150 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500151 * \param grp The context for the elliptic curve to use.
152 * This must be initialized and have group parameters
153 * set, for example through mbedtls_ecp_group_load().
154 * \param r The MPI context in which to store the first part
155 * the signature. This must be initialized.
156 * \param s The MPI context in which to store the second part
157 * the signature. This must be initialized.
158 * \param d The private signing key. This must be initialized.
159 * \param buf The content to be signed. This is usually the hash of
160 * the original data to be signed. This must be a readable
161 * buffer of length \p blen Bytes. It may be \c NULL if
162 * \p blen is zero.
163 * \param blen The length of \p buf in Bytes.
164 * \param f_rng The RNG function. This must not be \c NULL.
165 * \param p_rng The RNG context to be passed to \p f_rng. This may be
166 * \c NULL if \p f_rng doesn't need a context parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100167 *
Rose Zadik817297f2018-03-27 11:30:14 +0100168 * \return \c 0 on success.
169 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000170 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
173 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100174 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Janos Follathe65e0592019-01-04 15:55:43 +0000177#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
178#if defined(MBEDTLS_DEPRECATED_WARNING)
179#define MBEDTLS_DEPRECATED __attribute__((deprecated))
180#else
181#define MBEDTLS_DEPRECATED
182#endif
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100183/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000184 * \brief This function computes the ECDSA signature of a
185 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100186 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000187 * For more information, see <em>RFC-6979: Deterministic
188 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
189 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100190 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000191 * \note If the bitlength of the message hash is larger than the
192 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100193 * defined in <em>Standards for Efficient Cryptography Group
194 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
195 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000196 *
Janos Follath75f2c202019-01-15 11:44:31 +0000197 * \warning Since the output of the internal RNG is always the same for
198 * the same key and message, this limits the efficiency of
199 * blinding and leaks information through side channels. For
200 * secure behavior use mbedtls_ecdsa_sign_det_ext() instead.
201 *
202 * (Optimally the blinding is a random value that is different
203 * on every execution. In this case the blinding is still
204 * random from the attackers perspective, but is the same on
205 * each execution. This means that this blinding does not
206 * prevent attackers from recovering secrets by combining
207 * several measurement traces, but may prevent some attacks
208 * that exploit relationships between secret data.)
209 *
Rose Zadik817297f2018-03-27 11:30:14 +0100210 * \see ecp.h
211 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 * \param grp The context for the elliptic curve to use.
213 * This must be initialized and have group parameters
214 * set, for example through mbedtls_ecp_group_load().
215 * \param r The MPI context in which to store the first part
216 * the signature. This must be initialized.
217 * \param s The MPI context in which to store the second part
218 * the signature. This must be initialized.
219 * \param d The private signing key. This must be initialized
220 * and setup, for example through mbedtls_ecp_gen_privkey().
221 * \param buf The hashed content to be signed. This must be a readable
222 * buffer of length \p blen Bytes. It may be \c NULL if
223 * \p blen is zero.
224 * \param blen The length of \p buf in Bytes.
225 * \param md_alg The hash algorithm used to hash the original data.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100226 *
Rose Zadik817297f2018-03-27 11:30:14 +0100227 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100228 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000229 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100230 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
232 mbedtls_mpi *s, const mbedtls_mpi *d,
233 const unsigned char *buf, size_t blen,
Janos Follathe65e0592019-01-04 15:55:43 +0000234 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
235#undef MBEDTLS_DEPRECATED
236#endif /* MBEDTLS_DEPRECATED_REMOVED */
237
Janos Follathdca667a2019-01-04 14:32:30 +0000238/**
239 * \brief This function computes the ECDSA signature of a
240 * previously-hashed message, deterministic version.
241 *
242 * For more information, see <em>RFC-6979: Deterministic
243 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
244 * Curve Digital Signature Algorithm (ECDSA)</em>.
245 *
246 * \note If the bitlength of the message hash is larger than the
247 * bitlength of the group order, then the hash is truncated as
248 * defined in <em>Standards for Efficient Cryptography Group
249 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
250 * 4.1.3, step 5.
251 *
252 * \see ecp.h
253 *
254 * \param grp The context for the elliptic curve to use.
255 * This must be initialized and have group parameters
256 * set, for example through mbedtls_ecp_group_load().
257 * \param r The MPI context in which to store the first part
258 * the signature. This must be initialized.
259 * \param s The MPI context in which to store the second part
260 * the signature. This must be initialized.
261 * \param d The private signing key. This must be initialized
262 * and setup, for example through mbedtls_ecp_gen_privkey().
263 * \param buf The hashed content to be signed. This must be a readable
264 * buffer of length \p blen Bytes. It may be \c NULL if
265 * \p blen is zero.
266 * \param blen The length of \p buf in Bytes.
267 * \param md_alg The hash algorithm used to hash the original data.
268 * \param f_rng_blind The RNG function used for blinding. This must not be
269 * \c NULL.
270 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
271 * \c NULL if \p f_rng doesn't need a context parameter.
272 *
273 * \return \c 0 on success.
274 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
275 * error code on failure.
276 */
277int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
278 mbedtls_mpi *s, const mbedtls_mpi *d,
279 const unsigned char *buf, size_t blen,
280 mbedtls_md_type_t md_alg,
281 int (*f_rng_blind)(void *, unsigned char *, size_t),
282 void *p_rng_blind );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100284
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100285/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000286 * \brief This function verifies the ECDSA signature of a
287 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100288 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000289 * \note If the bitlength of the message hash is larger than the
290 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100291 * defined in <em>Standards for Efficient Cryptography Group
292 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
293 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000294 *
Rose Zadik817297f2018-03-27 11:30:14 +0100295 * \see ecp.h
296 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297 * \param grp The ECP group to use.
298 * This must be initialized and have group parameters
299 * set, for example through mbedtls_ecp_group_load().
300 * \param buf The hashed content that was signed. This must be a readable
301 * buffer of length \p blen Bytes. It may be \c NULL if
302 * \p blen is zero.
303 * \param blen The length of \p buf in Bytes.
304 * \param Q The public key to use for verification. This must be
305 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000306 * \param r The first integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500307 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000308 * \param s The second integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100310 *
Rose Zadik817297f2018-03-27 11:30:14 +0100311 * \return \c 0 on success.
Rose Zadik817297f2018-03-27 11:30:14 +0100312 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Dave Rodgmanc60b10a2022-08-19 11:08:07 +0100313 * error code on failure.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500316 const unsigned char *buf, size_t blen,
317 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
318 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100319
320/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000321 * \brief This function computes the ECDSA signature and writes it
322 * to a buffer, serialized as defined in <em>RFC-4492:
323 * Elliptic Curve Cryptography (ECC) Cipher Suites for
324 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200325 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000326 * \warning It is not thread-safe to use the same context in
327 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200328 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000329 * \note The deterministic version is used if
330 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
331 * information, see <em>RFC-6979: Deterministic Usage
332 * of the Digital Signature Algorithm (DSA) and Elliptic
333 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200334 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000335 * \note If the bitlength of the message hash is larger than the
336 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000337 * defined in <em>Standards for Efficient Cryptography Group
338 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
339 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000340 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000341 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100342 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343 * \param ctx The ECDSA context to use. This must be initialized
344 * and have a group and private key bound to it, for example
345 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100346 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500347 * \param hash The message hash to be signed. This must be a readable
348 * buffer of length \p blen Bytes.
349 * \param hlen The length of the hash \p hash in Bytes.
350 * \param sig The buffer to which to write the signature. This must be a
351 * writable buffer of length at least twice as large as the
352 * size of the curve used, plus 9. For example, 73 Bytes if
353 * a 256-bit curve is used. A buffer length of
354 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
355 * \param slen The address at which to store the actual length of
356 * the signature written. Must not be \c NULL.
357 * \param f_rng The RNG function. This must not be \c NULL if
358 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
Janos Follathe65e0592019-01-04 15:55:43 +0000359 * it is used only for blinding and may be set to \c NULL, but
360 * doing so is DEPRECATED.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500361 * \param p_rng The RNG context to be passed to \p f_rng. This may be
362 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100363 *
364 * \return \c 0 on success.
365 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
366 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200367 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500368int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
369 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200370 const unsigned char *hash, size_t hlen,
371 unsigned char *sig, size_t *slen,
372 int (*f_rng)(void *, unsigned char *, size_t),
373 void *p_rng );
374
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200375/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200376 * \brief This function computes the ECDSA signature and writes it
377 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200378 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200379 * \see \c mbedtls_ecdsa_write_signature()
380 *
381 * \note This function is like \c mbedtls_ecdsa_write_signature()
382 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200383 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
384 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385 * \param ctx The ECDSA context to use. This must be initialized
386 * and have a group and private key bound to it, for example
387 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200388 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500389 * \param hash The message hash to be signed. This must be a readable
390 * buffer of length \p blen Bytes.
391 * \param hlen The length of the hash \p hash in Bytes.
392 * \param sig The buffer to which to write the signature. This must be a
393 * writable buffer of length at least twice as large as the
394 * size of the curve used, plus 9. For example, 73 Bytes if
395 * a 256-bit curve is used. A buffer length of
396 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
397 * \param slen The address at which to store the actual length of
398 * the signature written. Must not be \c NULL.
399 * \param f_rng The RNG function. This must not be \c NULL if
400 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
401 * it is unused and may be set to \c NULL.
402 * \param p_rng The RNG context to be passed to \p f_rng. This may be
403 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
404 * \param rs_ctx The restart context to use. This may be \c NULL to disable
405 * restarting. If it is not \c NULL, it must point to an
406 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200407 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200408 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200409 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200410 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200411 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
412 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200413 */
414int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
415 mbedtls_md_type_t md_alg,
416 const unsigned char *hash, size_t hlen,
417 unsigned char *sig, size_t *slen,
418 int (*f_rng)(void *, unsigned char *, size_t),
419 void *p_rng,
420 mbedtls_ecdsa_restart_ctx *rs_ctx );
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
423#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
424#if defined(MBEDTLS_DEPRECATED_WARNING)
425#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200426#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200428#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100429/**
Rose Zadik817297f2018-03-27 11:30:14 +0100430 * \brief This function computes an ECDSA signature and writes
431 * it to a buffer, serialized as defined in <em>RFC-4492:
432 * Elliptic Curve Cryptography (ECC) Cipher Suites for
433 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100434 *
Rose Zadik817297f2018-03-27 11:30:14 +0100435 * The deterministic version is defined in <em>RFC-6979:
436 * Deterministic Usage of the Digital Signature Algorithm (DSA)
437 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200438 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000439 * \warning It is not thread-safe to use the same context in
440 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100441 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000442 * \note If the bitlength of the message hash is larger than the
443 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000444 * defined in <em>Standards for Efficient Cryptography Group
445 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
446 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000447 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000448 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100449 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100450 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
451 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100452 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500453 * \param ctx The ECDSA context to use. This must be initialized
454 * and have a group and private key bound to it, for example
455 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
456 * \param hash The message hash to be signed. This must be a readable
457 * buffer of length \p blen Bytes.
458 * \param hlen The length of the hash \p hash in Bytes.
459 * \param sig The buffer to which to write the signature. This must be a
460 * writable buffer of length at least twice as large as the
461 * size of the curve used, plus 9. For example, 73 Bytes if
462 * a 256-bit curve is used. A buffer length of
463 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
464 * \param slen The address at which to store the actual length of
465 * the signature written. Must not be \c NULL.
466 * \param md_alg The message digest that was used to hash the message.
Rose Zadik817297f2018-03-27 11:30:14 +0100467 *
468 * \return \c 0 on success.
469 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
470 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100473 const unsigned char *hash, size_t hlen,
474 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
476#undef MBEDTLS_DEPRECATED
477#endif /* MBEDTLS_DEPRECATED_REMOVED */
478#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100479
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200480/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000481 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200482 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000483 * \note If the bitlength of the message hash is larger than the
484 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000485 * defined in <em>Standards for Efficient Cryptography Group
486 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
487 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000488 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000489 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100490 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500491 * \param ctx The ECDSA context to use. This must be initialized
492 * and have a group and public key bound to it.
493 * \param hash The message hash that was signed. This must be a readable
494 * buffer of length \p size Bytes.
495 * \param hlen The size of the hash \p hash.
496 * \param sig The signature to read and verify. This must be a readable
497 * buffer of length \p slen Bytes.
498 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100499 *
500 * \return \c 0 on success.
501 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100502 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
503 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100504 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
505 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200508 const unsigned char *hash, size_t hlen,
509 const unsigned char *sig, size_t slen );
510
511/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200512 * \brief This function reads and verifies an ECDSA signature,
513 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200514 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200515 * \see \c mbedtls_ecdsa_read_signature()
516 *
517 * \note This function is like \c mbedtls_ecdsa_read_signature()
518 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200519 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
520 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500521 * \param ctx The ECDSA context to use. This must be initialized
522 * and have a group and public key bound to it.
523 * \param hash The message hash that was signed. This must be a readable
524 * buffer of length \p size Bytes.
525 * \param hlen The size of the hash \p hash.
526 * \param sig The signature to read and verify. This must be a readable
527 * buffer of length \p slen Bytes.
528 * \param slen The size of \p sig in Bytes.
529 * \param rs_ctx The restart context to use. This may be \c NULL to disable
530 * restarting. If it is not \c NULL, it must point to an
531 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200532 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200533 * \return \c 0 on success.
534 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
535 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
536 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200537 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200538 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200539 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
540 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200541 */
542int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
543 const unsigned char *hash, size_t hlen,
544 const unsigned char *sig, size_t slen,
545 mbedtls_ecdsa_restart_ctx *rs_ctx );
Christoph M. Wintersteigeref17e3b2019-02-15 12:52:09 +0000546
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200547/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000548 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200549 *
Rose Zadik817297f2018-03-27 11:30:14 +0100550 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200551 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000552 * \param ctx The ECDSA context to store the keypair in.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500553 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000554 * \param gid The elliptic curve to use. One of the various
555 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 * \param f_rng The RNG function to use. This must not be \c NULL.
557 * \param p_rng The RNG context to be passed to \p f_rng. This may be
558 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200559 *
Rose Zadik817297f2018-03-27 11:30:14 +0100560 * \return \c 0 on success.
561 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200564 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
565
566/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200568 *
Rose Zadik817297f2018-03-27 11:30:14 +0100569 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200570 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500571 * \param ctx The ECDSA context to setup. This must be initialized.
572 * \param key The EC key to use. This must be initialized and hold
573 * a private-public key pair or a public key. In the former
574 * case, the ECDSA context may be used for signature creation
575 * and verification after this call. In the latter case, it
576 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200577 *
Rose Zadik817297f2018-03-27 11:30:14 +0100578 * \return \c 0 on success.
579 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200580 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500581int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
582 const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200583
584/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000585 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200586 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000587 * \param ctx The ECDSA context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500588 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200591
592/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000593 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200594 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500595 * \param ctx The ECDSA context to free. This may be \c NULL,
596 * in which case this function does nothing. If it
597 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200600
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200601#if defined(MBEDTLS_ECP_RESTARTABLE)
602/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500603 * \brief Initialize a restart context.
604 *
605 * \param ctx The restart context to initialize.
606 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200607 */
608void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
609
610/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500611 * \brief Free the components of a restart context.
612 *
613 * \param ctx The restart context to free. This may be \c NULL,
614 * in which case this function does nothing. If it
615 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200616 */
617void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
618#endif /* MBEDTLS_ECP_RESTARTABLE */
619
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100620#ifdef __cplusplus
621}
622#endif
623
Paul Bakker9af723c2014-05-01 13:03:14 +0200624#endif /* ecdsa.h */