blob: faec04db0fdfdd16d33da0af2a110b6e65c6413b [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
Rose Zadikbff87d92018-01-25 21:58:53 +00004 * \brief The Elliptic Curve Digital Signature Algorithm (ECDSA).
5 *
6 * ECDSA is defined in <em>Standards for Efficient Cryptography Group (SECG):
7 * SEC1 Elliptic Curve Cryptography</em>.
8 * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve
9 * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
10 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Rose Zadikbff87d92018-01-25 21:58:53 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020014 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
15 *
16 * This file is provided under the Apache License 2.0, or the
17 * GNU General Public License v2.0 or later.
18 *
19 * **********
20 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020021 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010033 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020034 * **********
35 *
36 * **********
37 * GNU General Public License v2.0 or later:
38 *
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License along
50 * with this program; if not, write to the Free Software Foundation, Inc.,
51 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
52 *
53 * **********
54 *
Rose Zadikbff87d92018-01-25 21:58:53 +000055 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010056 */
Rose Zadikbff87d92018-01-25 21:58:53 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#ifndef MBEDTLS_ECDSA_H
59#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010060
Ron Eldor0559c662018-02-14 16:02:41 +020061#if !defined(MBEDTLS_CONFIG_FILE)
62#include "config.h"
63#else
64#include MBEDTLS_CONFIG_FILE
65#endif
66
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020067#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020068#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010069
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020070/*
Rose Zadikbff87d92018-01-25 21:58:53 +000071 * RFC-4492 page 20:
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020072 *
73 * Ecdsa-Sig-Value ::= SEQUENCE {
74 * r INTEGER,
75 * s INTEGER
76 * }
77 *
78 * Size is at most
79 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
80 * twice that + 1 (tag) + 2 (len) for the sequence
81 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
82 * and less than 124 (total len <= 255) for the sequence)
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if MBEDTLS_ECP_MAX_BYTES > 124
85#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020086#endif
Rose Zadikbff87d92018-01-25 21:58:53 +000087/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020089
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020090/**
Rose Zadikbff87d92018-01-25 21:58:53 +000091 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020094
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010095#ifdef __cplusplus
96extern "C" {
97#endif
98
99/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000100 * \brief This function computes the ECDSA signature of a
101 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100102 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000103 * \note The deterministic version is usually preferred.
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +0200104 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000105 * \param grp The ECP group.
106 * \param r The first output integer.
107 * \param s The second output integer.
108 * \param d The private signing key.
109 * \param buf The message hash.
110 * \param blen The length of \p buf.
111 * \param f_rng The RNG function.
112 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100113 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000114 * \note If the bitlength of the message hash is larger than the
Rose Zadikbff87d92018-01-25 21:58:53 +0000115 * bitlength of the group order, then the hash is truncated
116 * as defined in <em>Standards for Efficient Cryptography Group
117 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
118 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000119 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000120 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX
121 * or \c MBEDTLS_MPI_XXX error code on failure.
122 *
123 * \see ecp.h
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
126 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100127 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100130/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000131 * \brief This function computes the ECDSA signature of a
132 * previously-hashed message, deterministic version.
133 * For more information, see <em>RFC-6979: Deterministic
134 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
135 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100136 *
Janos Follath2934c322019-01-04 14:32:30 +0000137 *
138 * \warning Since the output of the internal RNG is always the same for
139 * the same key and message, this limits the efficiency of
140 * blinding and leaks information through side channels. For
141 * secure behavior use mbedtls_ecdsa_sign_det_ext() instead.
142 *
143 * (Optimally the blinding is a random value that is different
144 * on every execution. In this case the blinding is still
145 * random from the attackers perspective, but is the same on
146 * each execution. This means that this blinding does not
147 * prevent attackers from recovering secrets by combining
148 * several measurement traces, but may prevent some attacks
149 * that exploit relationships between secret data.)
150 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000151 * \param grp The ECP group.
152 * \param r The first output integer.
153 * \param s The second output integer.
154 * \param d The private signing key.
155 * \param buf The message hash.
156 * \param blen The length of \p buf.
157 * \param md_alg The MD algorithm used to hash the message.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100158 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000159 * \note If the bitlength of the message hash is larger than the
160 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000161 * defined in <em>Standards for Efficient Cryptography Group
162 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
163 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000164 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000165 * \return \c 0 on success,
166 * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
167 * error code on failure.
168 *
169 * \see ecp.h
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100170 */
Janos Follath2934c322019-01-04 14:32:30 +0000171int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
172 mbedtls_mpi *s, const mbedtls_mpi *d,
173 const unsigned char *buf, size_t blen,
174 mbedtls_md_type_t md_alg );
175/**
176 * \brief This function computes the ECDSA signature of a
177 * previously-hashed message, deterministic version.
178 *
179 * For more information, see <em>RFC-6979: Deterministic
180 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
181 * Curve Digital Signature Algorithm (ECDSA)</em>.
182 *
183 * \note If the bitlength of the message hash is larger than the
184 * bitlength of the group order, then the hash is truncated as
185 * defined in <em>Standards for Efficient Cryptography Group
186 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
187 * 4.1.3, step 5.
188 *
189 * \see ecp.h
190 *
191 * \param grp The context for the elliptic curve to use.
192 * This must be initialized and have group parameters
193 * set, for example through mbedtls_ecp_group_load().
194 * \param r The MPI context in which to store the first part
195 * the signature. This must be initialized.
196 * \param s The MPI context in which to store the second part
197 * the signature. This must be initialized.
198 * \param d The private signing key. This must be initialized
199 * and setup, for example through mbedtls_ecp_gen_privkey().
200 * \param buf The hashed content to be signed. This must be a readable
201 * buffer of length \p blen Bytes. It may be \c NULL if
202 * \p blen is zero.
203 * \param blen The length of \p buf in Bytes.
204 * \param md_alg The hash algorithm used to hash the original data.
205 * \param f_rng_blind The RNG function used for blinding. This must not be
206 * \c NULL.
207 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
208 * \c NULL if \p f_rng doesn't need a context parameter.
209 *
210 * \return \c 0 on success.
211 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
212 * error code on failure.
213 */
214int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
215 mbedtls_mpi *s, const mbedtls_mpi *d,
216 const unsigned char *buf, size_t blen,
217 mbedtls_md_type_t md_alg,
218 int (*f_rng_blind)(void *, unsigned char *,
219 size_t),
220 void *p_rng_blind );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100222
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100223/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000224 * \brief This function verifies the ECDSA signature of a
225 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100226 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000227 * \param grp The ECP group.
228 * \param buf The message hash.
229 * \param blen The length of \p buf.
230 * \param Q The public key to use for verification.
231 * \param r The first integer of the signature.
232 * \param s The second integer of the signature.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100233 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000234 * \note If the bitlength of the message hash is larger than the
235 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000236 * defined in <em>Standards for Efficient Cryptography Group
237 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
238 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000239 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000240 * \return \c 0 on success,
241 * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
242 * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
243 * error code on failure for any other reason.
244 *
245 * \see ecp.h
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100248 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100250
251/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000252 * \brief This function computes the ECDSA signature and writes it
253 * to a buffer, serialized as defined in <em>RFC-4492:
254 * Elliptic Curve Cryptography (ECC) Cipher Suites for
255 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200256 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000257 * \warning It is not thread-safe to use the same context in
258 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200259 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000260 * \note The deterministic version is used if
261 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
262 * information, see <em>RFC-6979: Deterministic Usage
263 * of the Digital Signature Algorithm (DSA) and Elliptic
264 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200265 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000266 * \param ctx The ECDSA context.
267 * \param md_alg The message digest that was used to hash the message.
268 * \param hash The message hash.
269 * \param hlen The length of the hash.
270 * \param sig The buffer that holds the signature.
271 * \param slen The length of the signature written.
272 * \param f_rng The RNG function.
273 * \param p_rng The RNG parameter.
274 *
275 * \note The \p sig buffer must be at least twice as large as the
276 * size of the curve used, plus 9. For example, 73 Bytes if
277 * a 256-bit curve is used. A buffer length of
278 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200279 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000280 * \note If the bitlength of the message hash is larger than the
281 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000282 * defined in <em>Standards for Efficient Cryptography Group
283 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
284 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000285 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000286 * \return \c 0 on success,
287 * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
288 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
289 *
290 * \see ecp.h
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200293 const unsigned char *hash, size_t hlen,
294 unsigned char *sig, size_t *slen,
295 int (*f_rng)(void *, unsigned char *, size_t),
296 void *p_rng );
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
299#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
300#if defined(MBEDTLS_DEPRECATED_WARNING)
301#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200302#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200304#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100305/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000306 * \brief This function computes an ECDSA signature and writes it to a buffer,
307 * serialized as defined in <em>RFC-4492: Elliptic Curve Cryptography
308 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
309 *
310 * The deterministic version is defined in <em>RFC-6979:
311 * Deterministic Usage of the Digital Signature Algorithm (DSA) and
312 * Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
313 *
314 * \warning It is not thread-safe to use the same context in
315 * multiple threads.
316
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100317 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200319 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000320 * \param ctx The ECDSA context.
321 * \param hash The Message hash.
322 * \param hlen The length of the hash.
323 * \param sig The buffer that holds the signature.
324 * \param slen The length of the signature written.
325 * \param md_alg The MD algorithm used to hash the message.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100326 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000327 * \note The \p sig buffer must be at least twice as large as the
328 * size of the curve used, plus 9. For example, 73 Bytes if a
329 * 256-bit curve is used. A buffer length of
330 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100331 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000332 * \note If the bitlength of the message hash is larger than the
333 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000334 * defined in <em>Standards for Efficient Cryptography Group
335 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
336 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000337 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000338 * \return \c 0 on success,
339 * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
340 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
341 *
342 * \see ecp.h
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100345 const unsigned char *hash, size_t hlen,
346 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
348#undef MBEDTLS_DEPRECATED
349#endif /* MBEDTLS_DEPRECATED_REMOVED */
350#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100351
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200352/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000353 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200354 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000355 * \param ctx The ECDSA context.
356 * \param hash The message hash.
357 * \param hlen The size of the hash.
358 * \param sig The signature to read and verify.
359 * \param slen The size of \p sig.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200360 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000361 * \note If the bitlength of the message hash is larger than the
362 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000363 * defined in <em>Standards for Efficient Cryptography Group
364 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
365 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000366 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000367 * \return \c 0 on success,
368 * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200369 * #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
370 * signature in sig but its length is less than \p siglen,
Rose Zadikbff87d92018-01-25 21:58:53 +0000371 * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
372 * error code on failure for any other reason.
373 *
374 * \see ecp.h
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200377 const unsigned char *hash, size_t hlen,
378 const unsigned char *sig, size_t slen );
379
380/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000381 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200382 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000383 * \param ctx The ECDSA context to store the keypair in.
384 * \param gid The elliptic curve to use. One of the various
385 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
386 * \param f_rng The RNG function.
387 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200388 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000389 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on
390 * failure.
391 *
392 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200395 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
396
397/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000398 * \brief This function sets an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200399 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000400 * \param ctx The ECDSA context to set.
401 * \param key The EC key to use.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200402 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000403 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on
404 * failure.
405 *
406 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200409
410/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000411 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200412 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000413 * \param ctx The ECDSA context to initialize.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200416
417/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000418 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200419 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000420 * \param ctx The ECDSA context to free.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200423
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100424#ifdef __cplusplus
425}
426#endif
427
Paul Bakker9af723c2014-05-01 13:03:14 +0200428#endif /* ecdsa.h */