blob: 2df3eb7243a0ca6c319f8f08eb8e61a51740d040 [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 Amero203fdf72019-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 Amero203fdf72019-07-04 20:01:14 +010041#include "mbedtls/ecp.h"
42#include "mbedtls/md.h"
Jaeden Amero59ebddf2019-07-10 11:03:03 +010043#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010044
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010045/**
46 * \brief Maximum ECDSA signature size for a given curve bit size
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020047 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010048 * \param bits Curve size in bits
49 * \return Maximum signature size in bytes
50 *
51 * \note This macro returns a compile-time constant if its argument
52 * is one. It may evaluate its argument multiple times.
53 */
54/*
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020055 * Ecdsa-Sig-Value ::= SEQUENCE {
56 * r INTEGER,
57 * s INTEGER
58 * }
59 *
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010060 * 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 +020061 */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010062#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
63 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
64 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
65 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
66
Rose Zadikbff87d92018-01-25 21:58:53 +000067/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2f2b3962018-11-12 15:06:57 +010068#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020069
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020070#ifdef __cplusplus
71extern "C" {
72#endif
73
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020074/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020075 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020076 *
77 * \warning Performing multiple operations concurrently on the same
78 * ECDSA context is not supported; objects of this type
79 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020082
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020083#if defined(MBEDTLS_ECP_RESTARTABLE)
84
85/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020086 * \brief Internal restart context for ecdsa_verify()
87 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020088 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020089 */
90typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
91
92/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020093 * \brief Internal restart context for ecdsa_sign()
94 *
95 * \note Opaque struct, defined in ecdsa.c
96 */
97typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
98
99#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
100/**
101 * \brief Internal restart context for ecdsa_sign_det()
102 *
103 * \note Opaque struct, defined in ecdsa.c
104 */
105typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
106#endif
107
108/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200109 * \brief General context for resuming ECDSA operations
110 */
111typedef struct
112{
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200113 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
114 shared administrative info */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200115 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200116 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
117#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
118 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
119#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200120} mbedtls_ecdsa_restart_ctx;
121
122#else /* MBEDTLS_ECP_RESTARTABLE */
123
124/* Now we can declare functions that take a pointer to that */
125typedef void mbedtls_ecdsa_restart_ctx;
126
127#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100128
129/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000130 * \brief This function computes the ECDSA signature of a
131 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100132 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * \note The deterministic version implemented in
134 * mbedtls_ecdsa_sign_det() is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100135 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000136 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100137 * bitlength of the group order, then the hash is truncated
138 * as defined in <em>Standards for Efficient Cryptography Group
139 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
140 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000141 *
Rose Zadik817297f2018-03-27 11:30:14 +0100142 * \see ecp.h
143 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * \param grp The context for the elliptic curve to use.
145 * This must be initialized and have group parameters
146 * set, for example through mbedtls_ecp_group_load().
147 * \param r The MPI context in which to store the first part
148 * the signature. This must be initialized.
149 * \param s The MPI context in which to store the second part
150 * the signature. This must be initialized.
151 * \param d The private signing key. This must be initialized.
152 * \param buf The content to be signed. This is usually the hash of
153 * the original data to be signed. This must be a readable
154 * buffer of length \p blen Bytes. It may be \c NULL if
155 * \p blen is zero.
156 * \param blen The length of \p buf in Bytes.
157 * \param f_rng The RNG function. This must not be \c NULL.
158 * \param p_rng The RNG context to be passed to \p f_rng. This may be
159 * \c NULL if \p f_rng doesn't need a context parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100160 *
Rose Zadik817297f2018-03-27 11:30:14 +0100161 * \return \c 0 on success.
162 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000163 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
166 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100167 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100170/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000171 * \brief This function computes the ECDSA signature of a
172 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100173 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000174 * For more information, see <em>RFC-6979: Deterministic
175 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
176 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100177 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000178 * \note If the bitlength of the message hash is larger than the
179 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100180 * defined in <em>Standards for Efficient Cryptography Group
181 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
182 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000183 *
Rose Zadik817297f2018-03-27 11:30:14 +0100184 * \see ecp.h
185 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186 * \param grp The context for the elliptic curve to use.
187 * This must be initialized and have group parameters
188 * set, for example through mbedtls_ecp_group_load().
189 * \param r The MPI context in which to store the first part
190 * the signature. This must be initialized.
191 * \param s The MPI context in which to store the second part
192 * the signature. This must be initialized.
193 * \param d The private signing key. This must be initialized
194 * and setup, for example through mbedtls_ecp_gen_privkey().
195 * \param buf The hashed content to be signed. This must be a readable
196 * buffer of length \p blen Bytes. It may be \c NULL if
197 * \p blen is zero.
198 * \param blen The length of \p buf in Bytes.
199 * \param md_alg The hash algorithm used to hash the original data.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100200 *
Rose Zadik817297f2018-03-27 11:30:14 +0100201 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100202 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000203 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100204 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
206 mbedtls_mpi *s, const mbedtls_mpi *d,
207 const unsigned char *buf, size_t blen,
208 mbedtls_md_type_t md_alg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100210
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100211/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000212 * \brief This function verifies the ECDSA signature of a
213 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100214 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000215 * \note If the bitlength of the message hash is larger than the
216 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100217 * defined in <em>Standards for Efficient Cryptography Group
218 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
219 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000220 *
Rose Zadik817297f2018-03-27 11:30:14 +0100221 * \see ecp.h
222 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500223 * \param grp The ECP group to use.
224 * This must be initialized and have group parameters
225 * set, for example through mbedtls_ecp_group_load().
226 * \param buf The hashed content that was signed. This must be a readable
227 * buffer of length \p blen Bytes. It may be \c NULL if
228 * \p blen is zero.
229 * \param blen The length of \p buf in Bytes.
230 * \param Q The public key to use for verification. This must be
231 * initialized and setup.
Rose Zadikbff87d92018-01-25 21:58:53 +0000232 * \param r The first integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500233 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000234 * \param s The second integer of the signature.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 * This must be initialized.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100236 *
Rose Zadik817297f2018-03-27 11:30:14 +0100237 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100238 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
239 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100240 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000241 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244 const unsigned char *buf, size_t blen,
245 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
246 const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100247
248/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000249 * \brief This function computes the ECDSA signature and writes it
250 * to a buffer, serialized as defined in <em>RFC-4492:
251 * Elliptic Curve Cryptography (ECC) Cipher Suites for
252 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200253 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000254 * \warning It is not thread-safe to use the same context in
255 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200256 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000257 * \note The deterministic version is used if
258 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
259 * information, see <em>RFC-6979: Deterministic Usage
260 * of the Digital Signature Algorithm (DSA) and Elliptic
261 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200262 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000263 * \note If the bitlength of the message hash is larger than the
264 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000265 * defined in <em>Standards for Efficient Cryptography Group
266 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
267 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000268 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000269 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100270 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 * \param ctx The ECDSA context to use. This must be initialized
272 * and have a group and private key bound to it, for example
273 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Rose Zadik817297f2018-03-27 11:30:14 +0100274 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275 * \param hash The message hash to be signed. This must be a readable
276 * buffer of length \p blen Bytes.
277 * \param hlen The length of the hash \p hash in Bytes.
278 * \param sig The buffer to which to write the signature. This must be a
279 * writable buffer of length at least twice as large as the
280 * size of the curve used, plus 9. For example, 73 Bytes if
281 * a 256-bit curve is used. A buffer length of
282 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
283 * \param slen The address at which to store the actual length of
284 * the signature written. Must not be \c NULL.
285 * \param f_rng The RNG function. This must not be \c NULL if
286 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
287 * it is unused and may be set to \c NULL.
288 * \param p_rng The RNG context to be passed to \p f_rng. This may be
289 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
Rose Zadik817297f2018-03-27 11:30:14 +0100290 *
291 * \return \c 0 on success.
292 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
293 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200294 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
296 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200297 const unsigned char *hash, size_t hlen,
298 unsigned char *sig, size_t *slen,
299 int (*f_rng)(void *, unsigned char *, size_t),
300 void *p_rng );
301
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200302/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200303 * \brief This function computes the ECDSA signature and writes it
304 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200305 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200306 * \see \c mbedtls_ecdsa_write_signature()
307 *
308 * \note This function is like \c mbedtls_ecdsa_write_signature()
309 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200310 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
311 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312 * \param ctx The ECDSA context to use. This must be initialized
313 * and have a group and private key bound to it, for example
314 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200315 * \param md_alg The message digest that was used to hash the message.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500316 * \param hash The message hash to be signed. This must be a readable
317 * buffer of length \p blen Bytes.
318 * \param hlen The length of the hash \p hash in Bytes.
319 * \param sig The buffer to which to write the signature. This must be a
320 * writable buffer of length at least twice as large as the
321 * size of the curve used, plus 9. For example, 73 Bytes if
322 * a 256-bit curve is used. A buffer length of
323 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
324 * \param slen The address at which to store the actual length of
325 * the signature written. Must not be \c NULL.
326 * \param f_rng The RNG function. This must not be \c NULL if
327 * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
328 * it is unused and may be set to \c NULL.
329 * \param p_rng The RNG context to be passed to \p f_rng. This may be
330 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
331 * \param rs_ctx The restart context to use. This may be \c NULL to disable
332 * restarting. If it is not \c NULL, it must point to an
333 * initialized restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200334 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200335 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200336 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200337 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200338 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
339 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200340 */
341int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
342 mbedtls_md_type_t md_alg,
343 const unsigned char *hash, size_t hlen,
344 unsigned char *sig, size_t *slen,
345 int (*f_rng)(void *, unsigned char *, size_t),
346 void *p_rng,
347 mbedtls_ecdsa_restart_ctx *rs_ctx );
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
350#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100351/**
Rose Zadik817297f2018-03-27 11:30:14 +0100352 * \brief This function computes an ECDSA signature and writes
353 * it to a buffer, serialized as defined in <em>RFC-4492:
354 * Elliptic Curve Cryptography (ECC) Cipher Suites for
355 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100356 *
Rose Zadik817297f2018-03-27 11:30:14 +0100357 * The deterministic version is defined in <em>RFC-6979:
358 * Deterministic Usage of the Digital Signature Algorithm (DSA)
359 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200360 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000361 * \warning It is not thread-safe to use the same context in
362 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100363 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000364 * \note If the bitlength of the message hash is larger than the
365 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000366 * defined in <em>Standards for Efficient Cryptography Group
367 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
368 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000369 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000370 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100371 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100372 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
373 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100374 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375 * \param ctx The ECDSA context to use. This must be initialized
376 * and have a group and private key bound to it, for example
377 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
378 * \param hash The message hash to be signed. This must be a readable
379 * buffer of length \p blen Bytes.
380 * \param hlen The length of the hash \p hash in Bytes.
381 * \param sig The buffer to which to write the signature. This must be a
382 * writable buffer of length at least twice as large as the
383 * size of the curve used, plus 9. For example, 73 Bytes if
384 * a 256-bit curve is used. A buffer length of
385 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
386 * \param slen The address at which to store the actual length of
387 * the signature written. Must not be \c NULL.
388 * \param md_alg The message digest that was used to hash the message.
Rose Zadik817297f2018-03-27 11:30:14 +0100389 *
390 * \return \c 0 on success.
391 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
392 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100395 const unsigned char *hash, size_t hlen,
396 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#endif /* MBEDTLS_DEPRECATED_REMOVED */
399#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100400
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200401/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000402 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200403 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000404 * \note If the bitlength of the message hash is larger than the
405 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000406 * defined in <em>Standards for Efficient Cryptography Group
407 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
408 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000409 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000410 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100411 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500412 * \param ctx The ECDSA context to use. This must be initialized
413 * and have a group and public key bound to it.
414 * \param hash The message hash that was signed. This must be a readable
415 * buffer of length \p size Bytes.
416 * \param hlen The size of the hash \p hash.
417 * \param sig The signature to read and verify. This must be a readable
418 * buffer of length \p slen Bytes.
419 * \param slen The size of \p sig in Bytes.
Rose Zadik817297f2018-03-27 11:30:14 +0100420 *
421 * \return \c 0 on success.
422 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100423 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
424 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100425 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
426 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200429 const unsigned char *hash, size_t hlen,
430 const unsigned char *sig, size_t slen );
431
432/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200433 * \brief This function reads and verifies an ECDSA signature,
434 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200435 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200436 * \see \c mbedtls_ecdsa_read_signature()
437 *
438 * \note This function is like \c mbedtls_ecdsa_read_signature()
439 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200440 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
441 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500442 * \param ctx The ECDSA context to use. This must be initialized
443 * and have a group and public key bound to it.
444 * \param hash The message hash that was signed. This must be a readable
445 * buffer of length \p size Bytes.
446 * \param hlen The size of the hash \p hash.
447 * \param sig The signature to read and verify. This must be a readable
448 * buffer of length \p slen Bytes.
449 * \param slen The size of \p sig in Bytes.
450 * \param rs_ctx The restart context to use. This may be \c NULL to disable
451 * restarting. If it is not \c NULL, it must point to an
452 * initialized restart context.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200453 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200454 * \return \c 0 on success.
455 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
456 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
457 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200458 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200459 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200460 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
461 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200462 */
463int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
464 const unsigned char *hash, size_t hlen,
465 const unsigned char *sig, size_t slen,
466 mbedtls_ecdsa_restart_ctx *rs_ctx );
467
468/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000469 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200470 *
Rose Zadik817297f2018-03-27 11:30:14 +0100471 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200472 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000473 * \param ctx The ECDSA context to store the keypair in.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500474 * This must be initialized.
Rose Zadikbff87d92018-01-25 21:58:53 +0000475 * \param gid The elliptic curve to use. One of the various
476 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500477 * \param f_rng The RNG function to use. This must not be \c NULL.
478 * \param p_rng The RNG context to be passed to \p f_rng. This may be
479 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200480 *
Rose Zadik817297f2018-03-27 11:30:14 +0100481 * \return \c 0 on success.
482 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200485 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
486
487/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500488 * \brief This function sets up an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200489 *
Rose Zadik817297f2018-03-27 11:30:14 +0100490 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200491 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500492 * \param ctx The ECDSA context to setup. This must be initialized.
493 * \param key The EC key to use. This must be initialized and hold
494 * a private-public key pair or a public key. In the former
495 * case, the ECDSA context may be used for signature creation
496 * and verification after this call. In the latter case, it
497 * may be used for signature verification.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200498 *
Rose Zadik817297f2018-03-27 11:30:14 +0100499 * \return \c 0 on success.
500 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200501 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500502int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
503 const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200504
505/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000506 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200507 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000508 * \param ctx The ECDSA context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500509 * This must not be \c NULL.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200512
513/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000514 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200515 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500516 * \param ctx The ECDSA context to free. This may be \c NULL,
517 * in which case this function does nothing. If it
518 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200521
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200522#if defined(MBEDTLS_ECP_RESTARTABLE)
523/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500524 * \brief Initialize a restart context.
525 *
526 * \param ctx The restart context to initialize.
527 * This must not be \c NULL.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200528 */
529void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
530
531/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500532 * \brief Free the components of a restart context.
533 *
534 * \param ctx The restart context to free. This may be \c NULL,
535 * in which case this function does nothing. If it
536 * is not \c NULL, it must be initialized.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200537 */
538void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
539#endif /* MBEDTLS_ECP_RESTARTABLE */
540
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100541#ifdef __cplusplus
542}
543#endif
544
Paul Bakker9af723c2014-05-01 13:03:14 +0200545#endif /* ecdsa.h */