blob: bc219dcad75adca914a9d84129f6f937887e7d80 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +020015 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
16 *
17 * This file is provided under the Apache License 2.0, or the
18 * GNU General Public License v2.0 or later.
19 *
20 * **********
21 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020022 *
23 * Licensed under the Apache License, Version 2.0 (the "License"); you may
24 * not use this file except in compliance with the License.
25 * You may obtain a copy of the License at
26 *
27 * http://www.apache.org/licenses/LICENSE-2.0
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
31 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010034 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020035 * **********
36 *
37 * **********
38 * GNU General Public License v2.0 or later:
39 *
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
44 *
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
49 *
50 * You should have received a copy of the GNU General Public License along
51 * with this program; if not, write to the Free Software Foundation, Inc.,
52 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
53 *
54 * **********
55 *
Rose Zadikbff87d92018-01-25 21:58:53 +000056 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010057 */
Rose Zadikbff87d92018-01-25 21:58:53 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#ifndef MBEDTLS_ECDSA_H
60#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010061
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020062#if !defined(MBEDTLS_CONFIG_FILE)
63#include "config.h"
64#else
65#include MBEDTLS_CONFIG_FILE
66#endif
67
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020068#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020069#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010070
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020071/*
Rose Zadikbff87d92018-01-25 21:58:53 +000072 * RFC-4492 page 20:
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020073 *
74 * Ecdsa-Sig-Value ::= SEQUENCE {
75 * r INTEGER,
76 * s INTEGER
77 * }
78 *
79 * Size is at most
80 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
81 * twice that + 1 (tag) + 2 (len) for the sequence
82 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
83 * and less than 124 (total len <= 255) for the sequence)
84 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if MBEDTLS_ECP_MAX_BYTES > 124
86#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020087#endif
Rose Zadikbff87d92018-01-25 21:58:53 +000088/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020090
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020091#ifdef __cplusplus
92extern "C" {
93#endif
94
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020095/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020096 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020097 *
98 * \warning Performing multiple operations concurrently on the same
99 * ECDSA context is not supported; objects of this type
100 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +0200101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +0200103
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200104#if defined(MBEDTLS_ECP_RESTARTABLE)
105
106/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200107 * \brief Internal restart context for ecdsa_verify()
108 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200109 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200110 */
111typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
112
113/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200114 * \brief Internal restart context for ecdsa_sign()
115 *
116 * \note Opaque struct, defined in ecdsa.c
117 */
118typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
119
120#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
121/**
122 * \brief Internal restart context for ecdsa_sign_det()
123 *
124 * \note Opaque struct, defined in ecdsa.c
125 */
126typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
127#endif
128
129/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200130 * \brief General context for resuming ECDSA operations
131 */
132typedef struct
133{
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200134 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
135 shared administrative info */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200136 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200137 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
138#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
139 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
140#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200141} mbedtls_ecdsa_restart_ctx;
142
143#else /* MBEDTLS_ECP_RESTARTABLE */
144
145/* Now we can declare functions that take a pointer to that */
146typedef void mbedtls_ecdsa_restart_ctx;
147
148#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100149
150/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000151 * \brief This function computes the ECDSA signature of a
152 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100153 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000154 * \note The deterministic version implemented in
155 * mbedtls_ecdsa_sign_det() is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100156 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000157 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100158 * bitlength of the group order, then the hash is truncated
159 * as defined in <em>Standards for Efficient Cryptography Group
160 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
161 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000162 *
Rose Zadik817297f2018-03-27 11:30:14 +0100163 * \see ecp.h
164 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000165 * \param grp The context for the elliptic curve to use.
166 * This must be initialized and have group parameters
167 * set, for example through mbedtls_ecp_group_load().
168 * \param r The MPI context in which to store the first part
169 * the signature. This must be initialized.
170 * \param s The MPI context in which to store the second part
171 * the signature. This must be initialized.
172 * \param d The private signing key. This must be initialized.
173 * \param buf The content to be signed. This is usually the hash of
174 * the original data to be signed. This must be a readable
175 * buffer of length \p blen Bytes. It may be \c NULL if
176 * \p blen is zero.
177 * \param blen The length of \p buf in Bytes.
178 * \param f_rng The RNG function. This must not be \c NULL.
179 * \param p_rng The RNG context to be passed to \p f_rng. This may be
180 * \c NULL if \p f_rng doesn't need a context parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100181 *
Rose Zadik817297f2018-03-27 11:30:14 +0100182 * \return \c 0 on success.
183 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000184 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
187 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100188 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100191/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000192 * \brief This function computes the ECDSA signature of a
193 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100194 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000195 * For more information, see <em>RFC-6979: Deterministic
196 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
197 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100198 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000199 * \note If the bitlength of the message hash is larger than the
200 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100201 * defined in <em>Standards for Efficient Cryptography Group
202 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
203 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000204 *
Janos Follathf1713e92019-01-04 14:32:30 +0000205 * \warning Since the output of the internal RNG is always the same for
206 * the same key and message, this limits the efficiency of
207 * blinding and leaks information through side channels. For
208 * secure behavior use mbedtls_ecdsa_sign_det_ext() instead.
209 *
210 * (Optimally the blinding is a random value that is different
211 * on every execution. In this case the blinding is still
212 * random from the attackers perspective, but is the same on
213 * each execution. This means that this blinding does not
214 * prevent attackers from recovering secrets by combining
215 * several measurement traces, but may prevent some attacks
216 * that exploit relationships between secret data.)
217 *
Rose Zadik817297f2018-03-27 11:30:14 +0100218 * \see ecp.h
219 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000220 * \param grp The context for the elliptic curve to use.
221 * This must be initialized and have group parameters
222 * set, for example through mbedtls_ecp_group_load().
223 * \param r The MPI context in which to store the first part
224 * the signature. This must be initialized.
225 * \param s The MPI context in which to store the second part
226 * the signature. This must be initialized.
227 * \param d The private signing key. This must be initialized
228 * and setup, for example through mbedtls_ecp_gen_privkey().
229 * \param buf The hashed content to be signed. This must be a readable
230 * buffer of length \p blen Bytes. It may be \c NULL if
231 * \p blen is zero.
232 * \param blen The length of \p buf in Bytes.
233 * \param md_alg The hash algorithm used to hash the original data.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100234 *
Rose Zadik817297f2018-03-27 11:30:14 +0100235 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100236 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000237 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100238 */
Hanno Beckere2e509c2018-12-14 16:43:20 +0000239int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
240 mbedtls_mpi *s, const mbedtls_mpi *d,
241 const unsigned char *buf, size_t blen,
242 mbedtls_md_type_t md_alg );
Janos Follathf1713e92019-01-04 14:32:30 +0000243/**
244 * \brief This function computes the ECDSA signature of a
245 * previously-hashed message, deterministic version.
246 *
247 * For more information, see <em>RFC-6979: Deterministic
248 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
249 * Curve Digital Signature Algorithm (ECDSA)</em>.
250 *
251 * \note If the bitlength of the message hash is larger than the
252 * bitlength of the group order, then the hash is truncated as
253 * defined in <em>Standards for Efficient Cryptography Group
254 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
255 * 4.1.3, step 5.
256 *
257 * \see ecp.h
258 *
259 * \param grp The context for the elliptic curve to use.
260 * This must be initialized and have group parameters
261 * set, for example through mbedtls_ecp_group_load().
262 * \param r The MPI context in which to store the first part
263 * the signature. This must be initialized.
264 * \param s The MPI context in which to store the second part
265 * the signature. This must be initialized.
266 * \param d The private signing key. This must be initialized
267 * and setup, for example through mbedtls_ecp_gen_privkey().
268 * \param buf The hashed content to be signed. This must be a readable
269 * buffer of length \p blen Bytes. It may be \c NULL if
270 * \p blen is zero.
271 * \param blen The length of \p buf in Bytes.
272 * \param md_alg The hash algorithm used to hash the original data.
273 * \param f_rng_blind The RNG function used for blinding. This must not be
274 * \c NULL.
275 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
276 * \c NULL if \p f_rng doesn't need a context parameter.
277 *
278 * \return \c 0 on success.
279 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
280 * error code on failure.
281 */
282int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
283 mbedtls_mpi *s, const mbedtls_mpi *d,
284 const unsigned char *buf, size_t blen,
285 mbedtls_md_type_t md_alg,
286 int (*f_rng_blind)(void *, unsigned char *,
287 size_t),
288 void *p_rng_blind );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100290
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100291/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000292 * \brief This function verifies the ECDSA signature of a
293 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100294 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000295 * \note If the bitlength of the message hash is larger than the
296 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100297 * defined in <em>Standards for Efficient Cryptography Group
298 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
299 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000300 *
Rose Zadik817297f2018-03-27 11:30:14 +0100301 * \see ecp.h
302 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000303 * \param grp The ECP group to use.
304 * This must be initialized and have group parameters
305 * set, for example through mbedtls_ecp_group_load().
306 * \param buf The hashed content that was signed. This must be a readable
307 * buffer of length \p blen Bytes. It may be \c NULL if
308 * \p blen is zero.
309 * \param blen The length of \p buf in Bytes.
310 * \param Q The public key to use for verification. This must be
311 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000312 * \param r The first integer of the signature.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000313 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000314 * \param s The second integer of the signature.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000315 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100316 *
Rose Zadik817297f2018-03-27 11:30:14 +0100317 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100318 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
319 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100320 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000321 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Hanno Beckere2e509c2018-12-14 16:43:20 +0000324 const unsigned char *buf, size_t blen,
325 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
326 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100327
328/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000329 * \brief This function computes the ECDSA signature and writes it
330 * to a buffer, serialized as defined in <em>RFC-4492:
331 * Elliptic Curve Cryptography (ECC) Cipher Suites for
332 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200333 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000334 * \warning It is not thread-safe to use the same context in
335 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200336 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000337 * \note The deterministic version is used if
338 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
339 * information, see <em>RFC-6979: Deterministic Usage
340 * of the Digital Signature Algorithm (DSA) and Elliptic
341 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200342 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000343 * \note If the bitlength of the message hash is larger than the
344 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000345 * defined in <em>Standards for Efficient Cryptography Group
346 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
347 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000348 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000349 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100350 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000351 * \param ctx The ECDSA context to use. This must be initialized
352 * and have a group and private key bound to it, for example
353 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100354 * \param md_alg The message digest that was used to hash the message.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000355 * \param hash The message hash to be signed. This must be a readable
356 * buffer of length \p blen Bytes.
357 * \param hlen The length of the hash \p hash in Bytes.
358 * \param sig The buffer to which to write the signature. This must be a
359 * writable buffer of length at least twice as large as the
360 * size of the curve used, plus 9. For example, 73 Bytes if
361 * a 256-bit curve is used. A buffer length of
362 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
363 * \param slen The address at which to store the actual length of
364 * the signature written. Must not be \c NULL.
365 * \param f_rng The RNG function. This must not be \c NULL if
366 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
367 * it is unused and may be set to \c NULL.
368 * \param p_rng The RNG context to be passed to \p f_rng. This may be
369 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100370 *
371 * \return \c 0 on success.
372 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
373 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200374 */
Hanno Beckere2e509c2018-12-14 16:43:20 +0000375int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
376 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200377 const unsigned char *hash, size_t hlen,
378 unsigned char *sig, size_t *slen,
379 int (*f_rng)(void *, unsigned char *, size_t),
380 void *p_rng );
381
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200382/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200383 * \brief This function computes the ECDSA signature and writes it
384 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200385 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200386 * \see \c mbedtls_ecdsa_write_signature()
387 *
388 * \note This function is like \c mbedtls_ecdsa_write_signature()
389 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200390 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
391 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000392 * \param ctx The ECDSA context to use. This must be initialized
393 * and have a group and private key bound to it, for example
394 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200395 * \param md_alg The message digest that was used to hash the message.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000396 * \param hash The message hash to be signed. This must be a readable
397 * buffer of length \p blen Bytes.
398 * \param hlen The length of the hash \p hash in Bytes.
399 * \param sig The buffer to which to write the signature. This must be a
400 * writable buffer of length at least twice as large as the
401 * size of the curve used, plus 9. For example, 73 Bytes if
402 * a 256-bit curve is used. A buffer length of
403 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
404 * \param slen The address at which to store the actual length of
405 * the signature written. Must not be \c NULL.
406 * \param f_rng The RNG function. This must not be \c NULL if
407 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
408 * it is unused and may be set to \c NULL.
409 * \param p_rng The RNG context to be passed to \p f_rng. This may be
410 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
411 * \param rs_ctx The restart context to use. This may be \c NULL to disable
412 * restarting. If it is not \c NULL, it must point to an
413 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200414 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200415 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200416 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200417 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200418 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
419 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200420 */
421int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
422 mbedtls_md_type_t md_alg,
423 const unsigned char *hash, size_t hlen,
424 unsigned char *sig, size_t *slen,
425 int (*f_rng)(void *, unsigned char *, size_t),
426 void *p_rng,
427 mbedtls_ecdsa_restart_ctx *rs_ctx );
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
430#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
431#if defined(MBEDTLS_DEPRECATED_WARNING)
432#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200433#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200435#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100436/**
Rose Zadik817297f2018-03-27 11:30:14 +0100437 * \brief This function computes an ECDSA signature and writes
438 * it to a buffer, serialized as defined in <em>RFC-4492:
439 * Elliptic Curve Cryptography (ECC) Cipher Suites for
440 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100441 *
Rose Zadik817297f2018-03-27 11:30:14 +0100442 * The deterministic version is defined in <em>RFC-6979:
443 * Deterministic Usage of the Digital Signature Algorithm (DSA)
444 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200445 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000446 * \warning It is not thread-safe to use the same context in
447 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100448 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000449 * \note If the bitlength of the message hash is larger than the
450 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000451 * defined in <em>Standards for Efficient Cryptography Group
452 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
453 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000454 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000455 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100456 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100457 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
458 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100459 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000460 * \param ctx The ECDSA context to use. This must be initialized
461 * and have a group and private key bound to it, for example
462 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
463 * \param hash The message hash to be signed. This must be a readable
464 * buffer of length \p blen Bytes.
465 * \param hlen The length of the hash \p hash in Bytes.
466 * \param sig The buffer to which to write the signature. This must be a
467 * writable buffer of length at least twice as large as the
468 * size of the curve used, plus 9. For example, 73 Bytes if
469 * a 256-bit curve is used. A buffer length of
470 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
471 * \param slen The address at which to store the actual length of
472 * the signature written. Must not be \c NULL.
473 * \param md_alg The message digest that was used to hash the message.
Rose Zadik817297f2018-03-27 11:30:14 +0100474 *
475 * \return \c 0 on success.
476 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
477 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100480 const unsigned char *hash, size_t hlen,
481 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
483#undef MBEDTLS_DEPRECATED
484#endif /* MBEDTLS_DEPRECATED_REMOVED */
485#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100486
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200487/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000488 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200489 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000490 * \note If the bitlength of the message hash is larger than the
491 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000492 * defined in <em>Standards for Efficient Cryptography Group
493 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
494 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000495 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000496 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100497 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000498 * \param ctx The ECDSA context to use. This must be initialized
499 * and have a group and public key bound to it.
500 * \param hash The message hash that was signed. This must be a readable
501 * buffer of length \p size Bytes.
502 * \param hlen The size of the hash \p hash.
503 * \param sig The signature to read and verify. This must be a readable
504 * buffer of length \p slen Bytes.
505 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100506 *
507 * \return \c 0 on success.
508 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100509 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
510 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100511 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
512 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200515 const unsigned char *hash, size_t hlen,
516 const unsigned char *sig, size_t slen );
517
518/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200519 * \brief This function reads and verifies an ECDSA signature,
520 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200521 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200522 * \see \c mbedtls_ecdsa_read_signature()
523 *
524 * \note This function is like \c mbedtls_ecdsa_read_signature()
525 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200526 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
527 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000528 * \param ctx The ECDSA context to use. This must be initialized
529 * and have a group and public key bound to it.
530 * \param hash The message hash that was signed. This must be a readable
531 * buffer of length \p size Bytes.
532 * \param hlen The size of the hash \p hash.
533 * \param sig The signature to read and verify. This must be a readable
534 * buffer of length \p slen Bytes.
535 * \param slen The size of \p sig in Bytes.
536 * \param rs_ctx The restart context to use. This may be \c NULL to disable
537 * restarting. If it is not \c NULL, it must point to an
538 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200539 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200540 * \return \c 0 on success.
541 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
542 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
543 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200544 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200545 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200546 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
547 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200548 */
549int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
550 const unsigned char *hash, size_t hlen,
551 const unsigned char *sig, size_t slen,
552 mbedtls_ecdsa_restart_ctx *rs_ctx );
553
554/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000555 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200556 *
Rose Zadik817297f2018-03-27 11:30:14 +0100557 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200558 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000559 * \param ctx The ECDSA context to store the keypair in.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000560 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000561 * \param gid The elliptic curve to use. One of the various
562 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000563 * \param f_rng The RNG function to use. This must not be \c NULL.
564 * \param p_rng The RNG context to be passed to \p f_rng. This may be
565 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200566 *
Rose Zadik817297f2018-03-27 11:30:14 +0100567 * \return \c 0 on success.
568 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200571 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
572
573/**
Hanno Becker035c6ba2018-12-18 23:35:53 +0000574 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200575 *
Rose Zadik817297f2018-03-27 11:30:14 +0100576 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200577 *
Hanno Becker035c6ba2018-12-18 23:35:53 +0000578 * \param ctx The ECDSA context to setup. This must be initialized.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000579 * \param key The EC key to use. This must be initialized and hold
580 * a private-public key pair or a public key. In the former
581 * case, the ECDSA context may be used for signature creation
Hanno Becker035c6ba2018-12-18 23:35:53 +0000582 * and verification after this call. In the latter case, it
583 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200584 *
Rose Zadik817297f2018-03-27 11:30:14 +0100585 * \return \c 0 on success.
586 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200587 */
Hanno Beckere2e509c2018-12-14 16:43:20 +0000588int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
589 const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200590
591/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000592 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200593 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000594 * \param ctx The ECDSA context to initialize.
Hanno Beckere2e509c2018-12-14 16:43:20 +0000595 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200598
599/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000600 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200601 *
Hanno Beckere2e509c2018-12-14 16:43:20 +0000602 * \param ctx The ECDSA context to free. This may be \c NULL,
603 * in which case this function does nothing. If it
604 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200607
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200608#if defined(MBEDTLS_ECP_RESTARTABLE)
609/**
Hanno Beckere2e509c2018-12-14 16:43:20 +0000610 * \brief Initialize a restart context.
611 *
612 * \param ctx The restart context to initialize.
613 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200614 */
615void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
616
617/**
Hanno Beckere2e509c2018-12-14 16:43:20 +0000618 * \brief Free the components of a restart context.
619 *
620 * \param ctx The restart context to free. This may be \c NULL,
621 * in which case this function does nothing. If it
622 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200623 */
624void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
625#endif /* MBEDTLS_ECP_RESTARTABLE */
626
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100627#ifdef __cplusplus
628}
629#endif
630
Paul Bakker9af723c2014-05-01 13:03:14 +0200631#endif /* ecdsa.h */