blob: 6d99b974c79a0491878f8e6fd744ba0ba0715d89 [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
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020035#if !defined(MBEDTLS_CONFIG_FILE)
36#include "config.h"
37#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020041#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020042#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010043
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020044/*
Rose Zadikbff87d92018-01-25 21:58:53 +000045 * RFC-4492 page 20:
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020046 *
47 * Ecdsa-Sig-Value ::= SEQUENCE {
48 * r INTEGER,
49 * s INTEGER
50 * }
51 *
52 * Size is at most
53 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
54 * twice that + 1 (tag) + 2 (len) for the sequence
55 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
56 * and less than 124 (total len <= 255) for the sequence)
57 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if MBEDTLS_ECP_MAX_BYTES > 124
59#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020060#endif
Rose Zadikbff87d92018-01-25 21:58:53 +000061/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020063
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020064#ifdef __cplusplus
65extern "C" {
66#endif
67
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020068/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020069 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020070 *
71 * \warning Performing multiple operations concurrently on the same
72 * ECDSA context is not supported; objects of this type
73 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020076
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020077#if defined(MBEDTLS_ECP_RESTARTABLE)
78
79/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020080 * \brief Internal restart context for ecdsa_verify()
81 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020082 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020083 */
84typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
85
86/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020087 * \brief Internal restart context for ecdsa_sign()
88 *
89 * \note Opaque struct, defined in ecdsa.c
90 */
91typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
92
93#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
94/**
95 * \brief Internal restart context for ecdsa_sign_det()
96 *
97 * \note Opaque struct, defined in ecdsa.c
98 */
99typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
100#endif
101
102/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200103 * \brief General context for resuming ECDSA operations
104 */
105typedef struct
106{
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200107 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
108 shared administrative info */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200109 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200110 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
111#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
112 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
113#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200114} mbedtls_ecdsa_restart_ctx;
115
116#else /* MBEDTLS_ECP_RESTARTABLE */
117
118/* Now we can declare functions that take a pointer to that */
119typedef void mbedtls_ecdsa_restart_ctx;
120
121#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100122
123/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000124 * \brief This function computes the ECDSA signature of a
125 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100126 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000127 * \note The deterministic version is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100128 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000129 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100130 * bitlength of the group order, then the hash is truncated
131 * as defined in <em>Standards for Efficient Cryptography Group
132 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
133 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000134 *
Rose Zadik817297f2018-03-27 11:30:14 +0100135 * \see ecp.h
136 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000137 * \param grp The ECP group.
138 * \param r The first output integer.
139 * \param s The second output integer.
140 * \param d The private signing key.
141 * \param buf The message hash.
142 * \param blen The length of \p buf.
143 * \param f_rng The RNG function.
Rose Zadik817297f2018-03-27 11:30:14 +0100144 * \param p_rng The RNG context.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100145 *
Rose Zadik817297f2018-03-27 11:30:14 +0100146 * \return \c 0 on success.
147 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000148 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
151 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100152 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100155/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000156 * \brief This function computes the ECDSA signature of a
157 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100158 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000159 * For more information, see <em>RFC-6979: Deterministic
160 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
161 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100162 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000163 * \note If the bitlength of the message hash is larger than the
164 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100165 * defined in <em>Standards for Efficient Cryptography Group
166 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
167 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000168 *
Rose Zadik817297f2018-03-27 11:30:14 +0100169 * \see ecp.h
170 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000171 * \param grp The ECP group.
172 * \param r The first output integer.
173 * \param s The second output integer.
174 * \param d The private signing key.
175 * \param buf The message hash.
176 * \param blen The length of \p buf.
177 * \param md_alg The MD algorithm used to hash the message.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100178 *
Rose Zadik817297f2018-03-27 11:30:14 +0100179 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100180 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000181 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
184 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
185 mbedtls_md_type_t md_alg );
186#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100187
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100188/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000189 * \brief This function verifies the ECDSA signature of a
190 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100191 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000192 * \note If the bitlength of the message hash is larger than the
193 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100194 * defined in <em>Standards for Efficient Cryptography Group
195 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
196 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000197 *
Rose Zadik817297f2018-03-27 11:30:14 +0100198 * \see ecp.h
199 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000200 * \param grp The ECP group.
201 * \param buf The message hash.
202 * \param blen The length of \p buf.
203 * \param Q The public key to use for verification.
204 * \param r The first integer of the signature.
205 * \param s The second integer of the signature.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100206 *
Rose Zadik817297f2018-03-27 11:30:14 +0100207 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100208 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
209 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100210 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000211 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100214 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100216
217/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000218 * \brief This function computes the ECDSA signature and writes it
219 * to a buffer, serialized as defined in <em>RFC-4492:
220 * Elliptic Curve Cryptography (ECC) Cipher Suites for
221 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200222 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000223 * \warning It is not thread-safe to use the same context in
224 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200225 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000226 * \note The deterministic version is used if
227 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
228 * information, see <em>RFC-6979: Deterministic Usage
229 * of the Digital Signature Algorithm (DSA) and Elliptic
230 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200231 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000232 * \note The \p sig buffer must be at least twice as large as the
233 * size of the curve used, plus 9. For example, 73 Bytes if
234 * a 256-bit curve is used. A buffer length of
235 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200236 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000237 * \note If the bitlength of the message hash is larger than the
238 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000239 * defined in <em>Standards for Efficient Cryptography Group
240 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
241 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000242 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000243 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100244 *
245 * \param ctx The ECDSA context.
246 * \param md_alg The message digest that was used to hash the message.
247 * \param hash The message hash.
248 * \param hlen The length of the hash.
249 * \param sig The buffer that holds the signature.
250 * \param slen The length of the signature written.
251 * \param f_rng The RNG function.
252 * \param p_rng The RNG context.
253 *
254 * \return \c 0 on success.
255 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
256 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200259 const unsigned char *hash, size_t hlen,
260 unsigned char *sig, size_t *slen,
261 int (*f_rng)(void *, unsigned char *, size_t),
262 void *p_rng );
263
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200264/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200265 * \brief This function computes the ECDSA signature and writes it
266 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200267 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200268 * \see \c mbedtls_ecdsa_write_signature()
269 *
270 * \note This function is like \c mbedtls_ecdsa_write_signature()
271 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200272 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
273 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200274 * \param ctx The ECDSA context.
275 * \param md_alg The message digest that was used to hash the message.
276 * \param hash The message hash.
277 * \param hlen The length of the hash.
278 * \param sig The buffer that holds the signature.
279 * \param slen The length of the signature written.
280 * \param f_rng The RNG function.
281 * \param p_rng The RNG context.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200282 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200283 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200284 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200285 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200286 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200287 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
288 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200289 */
290int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
291 mbedtls_md_type_t md_alg,
292 const unsigned char *hash, size_t hlen,
293 unsigned char *sig, size_t *slen,
294 int (*f_rng)(void *, unsigned char *, size_t),
295 void *p_rng,
296 mbedtls_ecdsa_restart_ctx *rs_ctx );
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 Zadik817297f2018-03-27 11:30:14 +0100306 * \brief This function computes an ECDSA signature and writes
307 * it to a buffer, serialized as defined in <em>RFC-4492:
308 * Elliptic Curve Cryptography (ECC) Cipher Suites for
309 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100310 *
Rose Zadik817297f2018-03-27 11:30:14 +0100311 * The deterministic version is defined in <em>RFC-6979:
312 * Deterministic Usage of the Digital Signature Algorithm (DSA)
313 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200314 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000315 * \warning It is not thread-safe to use the same context in
316 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100317 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000318 * \note The \p sig buffer must be at least twice as large as the
319 * size of the curve used, plus 9. For example, 73 Bytes if a
320 * 256-bit curve is used. A buffer length of
321 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100322 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000323 * \note If the bitlength of the message hash is larger than the
324 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000325 * defined in <em>Standards for Efficient Cryptography Group
326 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
327 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000328 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000329 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100330 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100331 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
332 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100333 *
334 * \param ctx The ECDSA context.
Rose Zadik14d0d572018-04-16 16:09:30 +0100335 * \param hash The message hash.
Rose Zadik817297f2018-03-27 11:30:14 +0100336 * \param hlen The length of the hash.
337 * \param sig The buffer that holds the signature.
338 * \param slen The length of the signature written.
339 * \param md_alg The MD algorithm used to hash the message.
340 *
341 * \return \c 0 on success.
342 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
343 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100346 const unsigned char *hash, size_t hlen,
347 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
349#undef MBEDTLS_DEPRECATED
350#endif /* MBEDTLS_DEPRECATED_REMOVED */
351#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100352
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200353/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000354 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200355 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000356 * \note If the bitlength of the message hash is larger than the
357 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000358 * defined in <em>Standards for Efficient Cryptography Group
359 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
360 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000361 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000362 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100363 *
364 * \param ctx The ECDSA context.
365 * \param hash The message hash.
366 * \param hlen The size of the hash.
367 * \param sig The signature to read and verify.
368 * \param slen The size of \p sig.
369 *
370 * \return \c 0 on success.
371 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100372 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
373 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100374 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
375 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200378 const unsigned char *hash, size_t hlen,
379 const unsigned char *sig, size_t slen );
380
381/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200382 * \brief This function reads and verifies an ECDSA signature,
383 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200384 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200385 * \see \c mbedtls_ecdsa_read_signature()
386 *
387 * \note This function is like \c mbedtls_ecdsa_read_signature()
388 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200389 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
390 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200391 * \param ctx The ECDSA context.
392 * \param hash The message hash.
393 * \param hlen The size of the hash.
394 * \param sig The signature to read and verify.
395 * \param slen The size of \p sig.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200396 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200397 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200398 * \return \c 0 on success.
399 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
400 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
401 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200402 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200403 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200404 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
405 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200406 */
407int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
408 const unsigned char *hash, size_t hlen,
409 const unsigned char *sig, size_t slen,
410 mbedtls_ecdsa_restart_ctx *rs_ctx );
411
412/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000413 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200414 *
Rose Zadik817297f2018-03-27 11:30:14 +0100415 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200416 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000417 * \param ctx The ECDSA context to store the keypair in.
418 * \param gid The elliptic curve to use. One of the various
419 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
420 * \param f_rng The RNG function.
Rose Zadik817297f2018-03-27 11:30:14 +0100421 * \param p_rng The RNG context.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200422 *
Rose Zadik817297f2018-03-27 11:30:14 +0100423 * \return \c 0 on success.
424 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200427 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
428
429/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000430 * \brief This function sets an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200431 *
Rose Zadik817297f2018-03-27 11:30:14 +0100432 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200433 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000434 * \param ctx The ECDSA context to set.
435 * \param key The EC key to use.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200436 *
Rose Zadik817297f2018-03-27 11:30:14 +0100437 * \return \c 0 on success.
438 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200441
442/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000443 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200444 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000445 * \param ctx The ECDSA context to initialize.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200448
449/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000450 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200451 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000452 * \param ctx The ECDSA context to free.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200455
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200456#if defined(MBEDTLS_ECP_RESTARTABLE)
457/**
458 * \brief Initialize a restart context
459 */
460void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
461
462/**
463 * \brief Free the components of a restart context
464 */
465void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
466#endif /* MBEDTLS_ECP_RESTARTABLE */
467
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100468#ifdef __cplusplus
469}
470#endif
471
Paul Bakker9af723c2014-05-01 13:03:14 +0200472#endif /* ecdsa.h */