blob: effbb1ed04c144a8fdfb8b680a909fb97307a4c5 [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/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000129 * \brief This function computes the ECDSA signature of a
130 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100131 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * \note The deterministic version implemented in
133 * mbedtls_ecdsa_sign_det() is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100134 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000135 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100136 * bitlength of the group order, then the hash is truncated
137 * as defined in <em>Standards for Efficient Cryptography Group
138 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
139 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000140 *
Rose Zadik817297f2018-03-27 11:30:14 +0100141 * \see ecp.h
142 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500143 * \param grp The context for the elliptic curve to use.
144 * This must be initialized and have group parameters
145 * set, for example through mbedtls_ecp_group_load().
146 * \param r The MPI context in which to store the first part
147 * the signature. This must be initialized.
148 * \param s The MPI context in which to store the second part
149 * the signature. This must be initialized.
150 * \param d The private signing key. This must be initialized.
151 * \param buf The content to be signed. This is usually the hash of
152 * the original data to be signed. This must be a readable
153 * buffer of length \p blen Bytes. It may be \c NULL if
154 * \p blen is zero.
155 * \param blen The length of \p buf in Bytes.
156 * \param f_rng The RNG function. This must not be \c NULL.
157 * \param p_rng The RNG context to be passed to \p f_rng. This may be
158 * \c NULL if \p f_rng doesn't need a context parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100159 *
Rose Zadik817297f2018-03-27 11:30:14 +0100160 * \return \c 0 on success.
161 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000162 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
165 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100166 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100169/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000170 * \brief This function computes the ECDSA signature of a
171 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100172 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000173 * For more information, see <em>RFC-6979: Deterministic
174 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
175 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100176 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000177 * \note If the bitlength of the message hash is larger than the
178 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100179 * defined in <em>Standards for Efficient Cryptography Group
180 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
181 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000182 *
Rose Zadik817297f2018-03-27 11:30:14 +0100183 * \see ecp.h
184 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 * \param grp The context for the elliptic curve to use.
186 * This must be initialized and have group parameters
187 * set, for example through mbedtls_ecp_group_load().
188 * \param r The MPI context in which to store the first part
189 * the signature. This must be initialized.
190 * \param s The MPI context in which to store the second part
191 * the signature. This must be initialized.
192 * \param d The private signing key. This must be initialized
193 * and setup, for example through mbedtls_ecp_gen_privkey().
194 * \param buf The hashed content to be signed. This must be a readable
195 * buffer of length \p blen Bytes. It may be \c NULL if
196 * \p blen is zero.
197 * \param blen The length of \p buf in Bytes.
198 * \param md_alg The hash algorithm used to hash the original data.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100199 *
Rose Zadik817297f2018-03-27 11:30:14 +0100200 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100201 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000202 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100203 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
205 mbedtls_mpi *s, const mbedtls_mpi *d,
206 const unsigned char *buf, size_t blen,
207 mbedtls_md_type_t md_alg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100209
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100210/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000211 * \brief This function verifies the ECDSA signature of a
212 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100213 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000214 * \note If the bitlength of the message hash is larger than the
215 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100216 * defined in <em>Standards for Efficient Cryptography Group
217 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
218 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000219 *
Rose Zadik817297f2018-03-27 11:30:14 +0100220 * \see ecp.h
221 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500222 * \param grp The ECP group to use.
223 * This must be initialized and have group parameters
224 * set, for example through mbedtls_ecp_group_load().
225 * \param buf The hashed content that was signed. This must be a readable
226 * buffer of length \p blen Bytes. It may be \c NULL if
227 * \p blen is zero.
228 * \param blen The length of \p buf in Bytes.
229 * \param Q The public key to use for verification. This must be
230 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000231 * \param r The first integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000233 * \param s The second integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100235 *
Rose Zadik817297f2018-03-27 11:30:14 +0100236 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100237 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
238 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100239 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000240 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243 const unsigned char *buf, size_t blen,
244 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
245 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100246
247/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000248 * \brief This function computes the ECDSA signature and writes it
249 * to a buffer, serialized as defined in <em>RFC-4492:
250 * Elliptic Curve Cryptography (ECC) Cipher Suites for
251 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200252 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000253 * \warning It is not thread-safe to use the same context in
254 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200255 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000256 * \note The deterministic version is used if
257 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
258 * information, see <em>RFC-6979: Deterministic Usage
259 * of the Digital Signature Algorithm (DSA) and Elliptic
260 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200261 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000262 * \note If the bitlength of the message hash is larger than the
263 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000264 * defined in <em>Standards for Efficient Cryptography Group
265 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
266 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000267 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000268 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100269 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270 * \param ctx The ECDSA context to use. This must be initialized
271 * and have a group and private key bound to it, for example
272 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100273 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 * \param hash The message hash to be signed. This must be a readable
275 * buffer of length \p blen Bytes.
276 * \param hlen The length of the hash \p hash in Bytes.
277 * \param sig The buffer to which to write the signature. This must be a
278 * writable buffer of length at least twice as large as the
279 * size of the curve used, plus 9. For example, 73 Bytes if
280 * a 256-bit curve is used. A buffer length of
281 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
282 * \param slen The address at which to store the actual length of
283 * the signature written. Must not be \c NULL.
284 * \param f_rng The RNG function. This must not be \c NULL if
285 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
286 * it is unused and may be set to \c NULL.
287 * \param p_rng The RNG context to be passed to \p f_rng. This may be
288 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100289 *
290 * \return \c 0 on success.
291 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
292 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200293 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500294int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
295 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200296 const unsigned char *hash, size_t hlen,
297 unsigned char *sig, size_t *slen,
298 int (*f_rng)(void *, unsigned char *, size_t),
299 void *p_rng );
300
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200301/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200302 * \brief This function computes the ECDSA signature and writes it
303 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200304 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200305 * \see \c mbedtls_ecdsa_write_signature()
306 *
307 * \note This function is like \c mbedtls_ecdsa_write_signature()
308 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200309 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
310 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500311 * \param ctx The ECDSA context to use. This must be initialized
312 * and have a group and private key bound to it, for example
313 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200314 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 * \param hash The message hash to be signed. This must be a readable
316 * buffer of length \p blen Bytes.
317 * \param hlen The length of the hash \p hash in Bytes.
318 * \param sig The buffer to which to write the signature. This must be a
319 * writable buffer of length at least twice as large as the
320 * size of the curve used, plus 9. For example, 73 Bytes if
321 * a 256-bit curve is used. A buffer length of
322 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
323 * \param slen The address at which to store the actual length of
324 * the signature written. Must not be \c NULL.
325 * \param f_rng The RNG function. This must not be \c NULL if
326 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
327 * it is unused and may be set to \c NULL.
328 * \param p_rng The RNG context to be passed to \p f_rng. This may be
329 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
330 * \param rs_ctx The restart context to use. This may be \c NULL to disable
331 * restarting. If it is not \c NULL, it must point to an
332 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200333 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200334 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200335 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200336 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200337 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
338 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200339 */
340int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
341 mbedtls_md_type_t md_alg,
342 const unsigned char *hash, size_t hlen,
343 unsigned char *sig, size_t *slen,
344 int (*f_rng)(void *, unsigned char *, size_t),
345 void *p_rng,
346 mbedtls_ecdsa_restart_ctx *rs_ctx );
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
349#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
350#if defined(MBEDTLS_DEPRECATED_WARNING)
351#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200352#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200354#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100355/**
Rose Zadik817297f2018-03-27 11:30:14 +0100356 * \brief This function computes an ECDSA signature and writes
357 * it to a buffer, serialized as defined in <em>RFC-4492:
358 * Elliptic Curve Cryptography (ECC) Cipher Suites for
359 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100360 *
Rose Zadik817297f2018-03-27 11:30:14 +0100361 * The deterministic version is defined in <em>RFC-6979:
362 * Deterministic Usage of the Digital Signature Algorithm (DSA)
363 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200364 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000365 * \warning It is not thread-safe to use the same context in
366 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100367 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000368 * \note If the bitlength of the message hash is larger than the
369 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000370 * defined in <em>Standards for Efficient Cryptography Group
371 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
372 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000373 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000374 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100375 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100376 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
377 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100378 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500379 * \param ctx The ECDSA context to use. This must be initialized
380 * and have a group and private key bound to it, for example
381 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
382 * \param hash The message hash to be signed. This must be a readable
383 * buffer of length \p blen Bytes.
384 * \param hlen The length of the hash \p hash in Bytes.
385 * \param sig The buffer to which to write the signature. This must be a
386 * writable buffer of length at least twice as large as the
387 * size of the curve used, plus 9. For example, 73 Bytes if
388 * a 256-bit curve is used. A buffer length of
389 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
390 * \param slen The address at which to store the actual length of
391 * the signature written. Must not be \c NULL.
392 * \param md_alg The message digest that was used to hash the message.
Rose Zadik817297f2018-03-27 11:30:14 +0100393 *
394 * \return \c 0 on success.
395 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
396 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100399 const unsigned char *hash, size_t hlen,
400 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
402#undef MBEDTLS_DEPRECATED
403#endif /* MBEDTLS_DEPRECATED_REMOVED */
404#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100405
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200406/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000407 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200408 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000409 * \note If the bitlength of the message hash is larger than the
410 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000411 * defined in <em>Standards for Efficient Cryptography Group
412 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
413 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000414 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000415 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100416 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417 * \param ctx The ECDSA context to use. This must be initialized
418 * and have a group and public key bound to it.
419 * \param hash The message hash that was signed. This must be a readable
420 * buffer of length \p size Bytes.
421 * \param hlen The size of the hash \p hash.
422 * \param sig The signature to read and verify. This must be a readable
423 * buffer of length \p slen Bytes.
424 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100425 *
426 * \return \c 0 on success.
427 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100428 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
429 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100430 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
431 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200434 const unsigned char *hash, size_t hlen,
435 const unsigned char *sig, size_t slen );
436
437/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200438 * \brief This function reads and verifies an ECDSA signature,
439 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200440 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200441 * \see \c mbedtls_ecdsa_read_signature()
442 *
443 * \note This function is like \c mbedtls_ecdsa_read_signature()
444 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200445 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
446 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500447 * \param ctx The ECDSA context to use. This must be initialized
448 * and have a group and public key bound to it.
449 * \param hash The message hash that was signed. This must be a readable
450 * buffer of length \p size Bytes.
451 * \param hlen The size of the hash \p hash.
452 * \param sig The signature to read and verify. This must be a readable
453 * buffer of length \p slen Bytes.
454 * \param slen The size of \p sig in Bytes.
455 * \param rs_ctx The restart context to use. This may be \c NULL to disable
456 * restarting. If it is not \c NULL, it must point to an
457 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200458 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200459 * \return \c 0 on success.
460 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
461 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
462 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200463 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200464 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200465 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
466 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200467 */
468int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
469 const unsigned char *hash, size_t hlen,
470 const unsigned char *sig, size_t slen,
471 mbedtls_ecdsa_restart_ctx *rs_ctx );
472
473/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000474 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200475 *
Rose Zadik817297f2018-03-27 11:30:14 +0100476 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200477 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000478 * \param ctx The ECDSA context to store the keypair in.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500479 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000480 * \param gid The elliptic curve to use. One of the various
481 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 * \param f_rng The RNG function to use. This must not be \c NULL.
483 * \param p_rng The RNG context to be passed to \p f_rng. This may be
484 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200485 *
Rose Zadik817297f2018-03-27 11:30:14 +0100486 * \return \c 0 on success.
487 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200490 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
491
492/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500493 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200494 *
Rose Zadik817297f2018-03-27 11:30:14 +0100495 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200496 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500497 * \param ctx The ECDSA context to setup. This must be initialized.
498 * \param key The EC key to use. This must be initialized and hold
499 * a private-public key pair or a public key. In the former
500 * case, the ECDSA context may be used for signature creation
501 * and verification after this call. In the latter case, it
502 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200503 *
Rose Zadik817297f2018-03-27 11:30:14 +0100504 * \return \c 0 on success.
505 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200506 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500507int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
508 const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200509
510/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000511 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200512 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000513 * \param ctx The ECDSA context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500514 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200517
518/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000519 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200520 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500521 * \param ctx The ECDSA context to free. This may be \c NULL,
522 * in which case this function does nothing. If it
523 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200526
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200527#if defined(MBEDTLS_ECP_RESTARTABLE)
528/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500529 * \brief Initialize a restart context.
530 *
531 * \param ctx The restart context to initialize.
532 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200533 */
534void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
535
536/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500537 * \brief Free the components of a restart context.
538 *
539 * \param ctx The restart context to free. This may be \c NULL,
540 * in which case this function does nothing. If it
541 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200542 */
543void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
544#endif /* MBEDTLS_ECP_RESTARTABLE */
545
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100546#ifdef __cplusplus
547}
548#endif
549
Paul Bakker9af723c2014-05-01 13:03:14 +0200550#endif /* ecdsa.h */