blob: b009e734569eb3358f53d2fb459e73ec5e7aadb2 [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/*
Rose Zadikbff87d92018-01-25 21:58:53 +000014 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010030 */
Rose Zadikbff87d92018-01-25 21:58:53 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#ifndef MBEDTLS_ECDSA_H
33#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010034
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050035#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050037#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010041#include "mbedtls/ecp.h"
42#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010043
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010044/**
45 * \brief Maximum ECDSA signature size for a given curve bit size
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020046 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010047 * \param bits Curve size in bits
48 * \return Maximum signature size in bytes
49 *
50 * \note This macro returns a compile-time constant if its argument
51 * is one. It may evaluate its argument multiple times.
52 */
53/*
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020054 * Ecdsa-Sig-Value ::= SEQUENCE {
55 * r INTEGER,
56 * s INTEGER
57 * }
58 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010059 * 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 +020060 */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010061#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
62 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
63 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
64 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
65
Rose Zadikbff87d92018-01-25 21:58:53 +000066/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010067#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020068
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020069#ifdef __cplusplus
70extern "C" {
71#endif
72
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020073/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020074 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020075 *
76 * \warning Performing multiple operations concurrently on the same
77 * ECDSA context is not supported; objects of this type
78 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020081
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020082#if defined(MBEDTLS_ECP_RESTARTABLE)
83
84/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020085 * \brief Internal restart context for ecdsa_verify()
86 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020087 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020088 */
89typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
90
91/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020092 * \brief Internal restart context for ecdsa_sign()
93 *
94 * \note Opaque struct, defined in ecdsa.c
95 */
96typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
97
98#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
99/**
100 * \brief Internal restart context for ecdsa_sign_det()
101 *
102 * \note Opaque struct, defined in ecdsa.c
103 */
104typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
105#endif
106
107/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200108 * \brief General context for resuming ECDSA operations
109 */
110typedef struct
111{
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200112 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
113 shared administrative info */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200114 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200115 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
116#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
117 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
118#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200119} mbedtls_ecdsa_restart_ctx;
120
121#else /* MBEDTLS_ECP_RESTARTABLE */
122
123/* Now we can declare functions that take a pointer to that */
124typedef void mbedtls_ecdsa_restart_ctx;
125
126#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100127
128/**
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000129 * \brief This function checks whether a given group can be used
130 * for ECDSA.
131 *
132 * \param gid The ECP group ID to check.
133 *
134 * \return \c 1 if the group can be used, \c 0 otherwise
135 */
136int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid );
137
138/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000139 * \brief This function computes the ECDSA signature of a
140 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100141 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * \note The deterministic version implemented in
143 * mbedtls_ecdsa_sign_det() is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100144 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000145 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100146 * bitlength of the group order, then the hash is truncated
147 * as defined in <em>Standards for Efficient Cryptography Group
148 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
149 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000150 *
Rose Zadik817297f2018-03-27 11:30:14 +0100151 * \see ecp.h
152 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 * \param grp The context for the elliptic curve to use.
154 * This must be initialized and have group parameters
155 * set, for example through mbedtls_ecp_group_load().
156 * \param r The MPI context in which to store the first part
157 * the signature. This must be initialized.
158 * \param s The MPI context in which to store the second part
159 * the signature. This must be initialized.
160 * \param d The private signing key. This must be initialized.
161 * \param buf The content to be signed. This is usually the hash of
162 * the original data to be signed. This must be a readable
163 * buffer of length \p blen Bytes. It may be \c NULL if
164 * \p blen is zero.
165 * \param blen The length of \p buf in Bytes.
166 * \param f_rng The RNG function. This must not be \c NULL.
167 * \param p_rng The RNG context to be passed to \p f_rng. This may be
168 * \c NULL if \p f_rng doesn't need a context parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100169 *
Rose Zadik817297f2018-03-27 11:30:14 +0100170 * \return \c 0 on success.
171 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000172 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
175 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100176 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Janos Follathe65e0592019-01-04 15:55:43 +0000179#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
180#if defined(MBEDTLS_DEPRECATED_WARNING)
181#define MBEDTLS_DEPRECATED __attribute__((deprecated))
182#else
183#define MBEDTLS_DEPRECATED
184#endif
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100185/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000186 * \brief This function computes the ECDSA signature of a
187 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100188 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000189 * For more information, see <em>RFC-6979: Deterministic
190 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
191 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100192 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000193 * \note If the bitlength of the message hash is larger than the
194 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100195 * defined in <em>Standards for Efficient Cryptography Group
196 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
197 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000198 *
Janos Follath75f2c202019-01-15 11:44:31 +0000199 * \warning Since the output of the internal RNG is always the same for
200 * the same key and message, this limits the efficiency of
201 * blinding and leaks information through side channels. For
202 * secure behavior use mbedtls_ecdsa_sign_det_ext() instead.
203 *
204 * (Optimally the blinding is a random value that is different
205 * on every execution. In this case the blinding is still
206 * random from the attackers perspective, but is the same on
207 * each execution. This means that this blinding does not
208 * prevent attackers from recovering secrets by combining
209 * several measurement traces, but may prevent some attacks
210 * that exploit relationships between secret data.)
211 *
Rose Zadik817297f2018-03-27 11:30:14 +0100212 * \see ecp.h
213 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 * \param grp The context for the elliptic curve to use.
215 * This must be initialized and have group parameters
216 * set, for example through mbedtls_ecp_group_load().
217 * \param r The MPI context in which to store the first part
218 * the signature. This must be initialized.
219 * \param s The MPI context in which to store the second part
220 * the signature. This must be initialized.
221 * \param d The private signing key. This must be initialized
222 * and setup, for example through mbedtls_ecp_gen_privkey().
223 * \param buf The hashed content to be signed. This must be a readable
224 * buffer of length \p blen Bytes. It may be \c NULL if
225 * \p blen is zero.
226 * \param blen The length of \p buf in Bytes.
227 * \param md_alg The hash algorithm used to hash the original data.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100228 *
Rose Zadik817297f2018-03-27 11:30:14 +0100229 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100230 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000231 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100232 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500233int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
234 mbedtls_mpi *s, const mbedtls_mpi *d,
235 const unsigned char *buf, size_t blen,
Janos Follathe65e0592019-01-04 15:55:43 +0000236 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
237#undef MBEDTLS_DEPRECATED
238#endif /* MBEDTLS_DEPRECATED_REMOVED */
239
Janos Follathdca667a2019-01-04 14:32:30 +0000240/**
241 * \brief This function computes the ECDSA signature of a
242 * previously-hashed message, deterministic version.
243 *
244 * For more information, see <em>RFC-6979: Deterministic
245 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
246 * Curve Digital Signature Algorithm (ECDSA)</em>.
247 *
248 * \note If the bitlength of the message hash is larger than the
249 * bitlength of the group order, then the hash is truncated as
250 * defined in <em>Standards for Efficient Cryptography Group
251 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
252 * 4.1.3, step 5.
253 *
254 * \see ecp.h
255 *
256 * \param grp The context for the elliptic curve to use.
257 * This must be initialized and have group parameters
258 * set, for example through mbedtls_ecp_group_load().
259 * \param r The MPI context in which to store the first part
260 * the signature. This must be initialized.
261 * \param s The MPI context in which to store the second part
262 * the signature. This must be initialized.
263 * \param d The private signing key. This must be initialized
264 * and setup, for example through mbedtls_ecp_gen_privkey().
265 * \param buf The hashed content to be signed. This must be a readable
266 * buffer of length \p blen Bytes. It may be \c NULL if
267 * \p blen is zero.
268 * \param blen The length of \p buf in Bytes.
269 * \param md_alg The hash algorithm used to hash the original data.
270 * \param f_rng_blind The RNG function used for blinding. This must not be
271 * \c NULL.
272 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
273 * \c NULL if \p f_rng doesn't need a context parameter.
274 *
275 * \return \c 0 on success.
276 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
277 * error code on failure.
278 */
279int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
280 mbedtls_mpi *s, const mbedtls_mpi *d,
281 const unsigned char *buf, size_t blen,
282 mbedtls_md_type_t md_alg,
283 int (*f_rng_blind)(void *, unsigned char *, size_t),
284 void *p_rng_blind );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100286
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100287/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000288 * \brief This function verifies the ECDSA signature of a
289 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100290 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000291 * \note If the bitlength of the message hash is larger than the
292 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100293 * defined in <em>Standards for Efficient Cryptography Group
294 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
295 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000296 *
Rose Zadik817297f2018-03-27 11:30:14 +0100297 * \see ecp.h
298 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299 * \param grp The ECP group to use.
300 * This must be initialized and have group parameters
301 * set, for example through mbedtls_ecp_group_load().
302 * \param buf The hashed content that was signed. This must be a readable
303 * buffer of length \p blen Bytes. It may be \c NULL if
304 * \p blen is zero.
305 * \param blen The length of \p buf in Bytes.
306 * \param Q The public key to use for verification. This must be
307 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000308 * \param r The first integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000310 * \param s The second integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500311 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100312 *
Rose Zadik817297f2018-03-27 11:30:14 +0100313 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100314 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
315 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100316 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000317 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 const unsigned char *buf, size_t blen,
321 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
322 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100323
324/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000325 * \brief This function computes the ECDSA signature and writes it
326 * to a buffer, serialized as defined in <em>RFC-4492:
327 * Elliptic Curve Cryptography (ECC) Cipher Suites for
328 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200329 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000330 * \warning It is not thread-safe to use the same context in
331 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200332 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000333 * \note The deterministic version is used if
334 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
335 * information, see <em>RFC-6979: Deterministic Usage
336 * of the Digital Signature Algorithm (DSA) and Elliptic
337 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200338 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000339 * \note If the bitlength of the message hash is larger than the
340 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000341 * defined in <em>Standards for Efficient Cryptography Group
342 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
343 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000344 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000345 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100346 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500347 * \param ctx The ECDSA context to use. This must be initialized
348 * and have a group and private key bound to it, for example
349 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100350 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500351 * \param hash The message hash to be signed. This must be a readable
352 * buffer of length \p blen Bytes.
353 * \param hlen The length of the hash \p hash in Bytes.
354 * \param sig The buffer to which to write the signature. This must be a
355 * writable buffer of length at least twice as large as the
356 * size of the curve used, plus 9. For example, 73 Bytes if
357 * a 256-bit curve is used. A buffer length of
358 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
359 * \param slen The address at which to store the actual length of
360 * the signature written. Must not be \c NULL.
361 * \param f_rng The RNG function. This must not be \c NULL if
362 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
Janos Follathe65e0592019-01-04 15:55:43 +0000363 * it is used only for blinding and may be set to \c NULL, but
364 * doing so is DEPRECATED.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500365 * \param p_rng The RNG context to be passed to \p f_rng. This may be
366 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100367 *
368 * \return \c 0 on success.
369 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
370 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200371 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500372int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
373 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200374 const unsigned char *hash, size_t hlen,
375 unsigned char *sig, size_t *slen,
376 int (*f_rng)(void *, unsigned char *, size_t),
377 void *p_rng );
378
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200379/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200380 * \brief This function computes the ECDSA signature and writes it
381 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200382 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200383 * \see \c mbedtls_ecdsa_write_signature()
384 *
385 * \note This function is like \c mbedtls_ecdsa_write_signature()
386 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200387 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
388 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500389 * \param ctx The ECDSA context to use. This must be initialized
390 * and have a group and private key bound to it, for example
391 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200392 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393 * \param hash The message hash to be signed. This must be a readable
394 * buffer of length \p blen Bytes.
395 * \param hlen The length of the hash \p hash in Bytes.
396 * \param sig The buffer to which to write the signature. This must be a
397 * writable buffer of length at least twice as large as the
398 * size of the curve used, plus 9. For example, 73 Bytes if
399 * a 256-bit curve is used. A buffer length of
400 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
401 * \param slen The address at which to store the actual length of
402 * the signature written. Must not be \c NULL.
403 * \param f_rng The RNG function. This must not be \c NULL if
404 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
405 * it is unused and may be set to \c NULL.
406 * \param p_rng The RNG context to be passed to \p f_rng. This may be
407 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
408 * \param rs_ctx The restart context to use. This may be \c NULL to disable
409 * restarting. If it is not \c NULL, it must point to an
410 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200411 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200412 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200413 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200414 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200415 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
416 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200417 */
418int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
419 mbedtls_md_type_t md_alg,
420 const unsigned char *hash, size_t hlen,
421 unsigned char *sig, size_t *slen,
422 int (*f_rng)(void *, unsigned char *, size_t),
423 void *p_rng,
424 mbedtls_ecdsa_restart_ctx *rs_ctx );
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
427#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
428#if defined(MBEDTLS_DEPRECATED_WARNING)
429#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200430#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200432#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100433/**
Rose Zadik817297f2018-03-27 11:30:14 +0100434 * \brief This function computes an ECDSA signature and writes
435 * it to a buffer, serialized as defined in <em>RFC-4492:
436 * Elliptic Curve Cryptography (ECC) Cipher Suites for
437 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100438 *
Rose Zadik817297f2018-03-27 11:30:14 +0100439 * The deterministic version is defined in <em>RFC-6979:
440 * Deterministic Usage of the Digital Signature Algorithm (DSA)
441 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200442 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000443 * \warning It is not thread-safe to use the same context in
444 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100445 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000446 * \note If the bitlength of the message hash is larger than the
447 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000448 * defined in <em>Standards for Efficient Cryptography Group
449 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
450 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000451 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000452 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100453 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100454 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
455 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100456 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500457 * \param ctx The ECDSA context to use. This must be initialized
458 * and have a group and private key bound to it, for example
459 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
460 * \param hash The message hash to be signed. This must be a readable
461 * buffer of length \p blen Bytes.
462 * \param hlen The length of the hash \p hash in Bytes.
463 * \param sig The buffer to which to write the signature. This must be a
464 * writable buffer of length at least twice as large as the
465 * size of the curve used, plus 9. For example, 73 Bytes if
466 * a 256-bit curve is used. A buffer length of
467 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
468 * \param slen The address at which to store the actual length of
469 * the signature written. Must not be \c NULL.
470 * \param md_alg The message digest that was used to hash the message.
Rose Zadik817297f2018-03-27 11:30:14 +0100471 *
472 * \return \c 0 on success.
473 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
474 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100477 const unsigned char *hash, size_t hlen,
478 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
480#undef MBEDTLS_DEPRECATED
481#endif /* MBEDTLS_DEPRECATED_REMOVED */
482#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100483
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200484/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000485 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200486 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000487 * \note If the bitlength of the message hash is larger than the
488 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000489 * defined in <em>Standards for Efficient Cryptography Group
490 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
491 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000492 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000493 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100494 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495 * \param ctx The ECDSA context to use. This must be initialized
496 * and have a group and public key bound to it.
497 * \param hash The message hash that was signed. This must be a readable
498 * buffer of length \p size Bytes.
499 * \param hlen The size of the hash \p hash.
500 * \param sig The signature to read and verify. This must be a readable
501 * buffer of length \p slen Bytes.
502 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100503 *
504 * \return \c 0 on success.
505 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100506 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
507 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100508 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
509 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200512 const unsigned char *hash, size_t hlen,
513 const unsigned char *sig, size_t slen );
514
515/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200516 * \brief This function reads and verifies an ECDSA signature,
517 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200518 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200519 * \see \c mbedtls_ecdsa_read_signature()
520 *
521 * \note This function is like \c mbedtls_ecdsa_read_signature()
522 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200523 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
524 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500525 * \param ctx The ECDSA context to use. This must be initialized
526 * and have a group and public key bound to it.
527 * \param hash The message hash that was signed. This must be a readable
528 * buffer of length \p size Bytes.
529 * \param hlen The size of the hash \p hash.
530 * \param sig The signature to read and verify. This must be a readable
531 * buffer of length \p slen Bytes.
532 * \param slen The size of \p sig in Bytes.
533 * \param rs_ctx The restart context to use. This may be \c NULL to disable
534 * restarting. If it is not \c NULL, it must point to an
535 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200536 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200537 * \return \c 0 on success.
538 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
539 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
540 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200541 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200542 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200543 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
544 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200545 */
546int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
547 const unsigned char *hash, size_t hlen,
548 const unsigned char *sig, size_t slen,
549 mbedtls_ecdsa_restart_ctx *rs_ctx );
Christoph M. Wintersteigeref17e3b2019-02-15 12:52:09 +0000550
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200551/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000552 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200553 *
Rose Zadik817297f2018-03-27 11:30:14 +0100554 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200555 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000556 * \param ctx The ECDSA context to store the keypair in.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500557 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000558 * \param gid The elliptic curve to use. One of the various
559 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500560 * \param f_rng The RNG function to use. This must not be \c NULL.
561 * \param p_rng The RNG context to be passed to \p f_rng. This may be
562 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200563 *
Rose Zadik817297f2018-03-27 11:30:14 +0100564 * \return \c 0 on success.
565 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200568 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
569
570/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500571 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200572 *
Rose Zadik817297f2018-03-27 11:30:14 +0100573 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200574 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500575 * \param ctx The ECDSA context to setup. This must be initialized.
576 * \param key The EC key to use. This must be initialized and hold
577 * a private-public key pair or a public key. In the former
578 * case, the ECDSA context may be used for signature creation
579 * and verification after this call. In the latter case, it
580 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200581 *
Rose Zadik817297f2018-03-27 11:30:14 +0100582 * \return \c 0 on success.
583 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200584 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500585int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
586 const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200587
588/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000589 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200590 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000591 * \param ctx The ECDSA context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500592 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200595
596/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000597 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200598 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500599 * \param ctx The ECDSA context to free. This may be \c NULL,
600 * in which case this function does nothing. If it
601 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200604
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200605#if defined(MBEDTLS_ECP_RESTARTABLE)
606/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500607 * \brief Initialize a restart context.
608 *
609 * \param ctx The restart context to initialize.
610 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200611 */
612void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
613
614/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500615 * \brief Free the components of a restart context.
616 *
617 * \param ctx The restart context to free. This may be \c NULL,
618 * in which case this function does nothing. If it
619 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200620 */
621void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
622#endif /* MBEDTLS_ECP_RESTARTABLE */
623
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100624#ifdef __cplusplus
625}
626#endif
627
Paul Bakker9af723c2014-05-01 13:03:14 +0200628#endif /* ecdsa.h */